added database and coupon and route daos
This commit is contained in:
@@ -1,29 +1,48 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Entity(tableName = "coupon")
|
||||
public class Coupon {
|
||||
|
||||
/**
|
||||
* fields need to be public for the database to be able to use them
|
||||
*/
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
@ColumnInfo(name = "code")
|
||||
private String code;
|
||||
|
||||
@ColumnInfo(name = "reward")
|
||||
@NonNull
|
||||
private String reward;
|
||||
|
||||
|
||||
public Coupon(String code, String reward) {
|
||||
public Coupon(@NonNull String code, @NotNull String reward) {
|
||||
this.code = code;
|
||||
this.reward = reward;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getReward() {
|
||||
return reward;
|
||||
}
|
||||
|
||||
public void setReward(String reward) {
|
||||
public void setCode(@NonNull String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public void setReward(@NonNull String reward) {
|
||||
this.reward = reward;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
package com.a1.nextlocation.data;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Route {
|
||||
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
private String name;
|
||||
|
||||
private List<Location> locations;
|
||||
|
||||
@ColumnInfo(name = "total_distance")
|
||||
private float totalDistance;
|
||||
|
||||
@ColumnInfo(name = "total_time")
|
||||
private int totalTime;
|
||||
|
||||
public Route(String name) {
|
||||
public Route(@NotNull String name) {
|
||||
|
||||
this.name = name;
|
||||
this.locations = new ArrayList<>();
|
||||
@@ -20,11 +36,12 @@ public class Route {
|
||||
this.locations.add(location);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public void setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -47,4 +64,12 @@ public class Route {
|
||||
return totalTime;
|
||||
}
|
||||
|
||||
public void setTotalDistance(float totalDistance) {
|
||||
this.totalDistance = totalDistance;
|
||||
}
|
||||
|
||||
public void setTotalTime(int totalTime) {
|
||||
this.totalTime = totalTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
31
app/src/main/java/com/a1/nextlocation/data/db/CouponDao.java
Normal file
31
app/src/main/java/com/a1/nextlocation/data/db/CouponDao.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface CouponDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insertAll(Coupon... coupons);
|
||||
|
||||
@Query("DELETE FROM coupon")
|
||||
void deleteAll();
|
||||
|
||||
@Query("SELECT * FROM coupon")
|
||||
LiveData<List<Coupon>> selectAll();
|
||||
|
||||
/*
|
||||
to add an observer to the livedata, you can use the example from https://medium.com/mindorks/using-room-database-with-livedata-android-jetpack-cbf89b677b47
|
||||
*/
|
||||
|
||||
@Query("SELECT * FROM coupon WHERE code = :code LIMIT 1")
|
||||
Coupon selectCouponByCode(String code);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CouponRepository {
|
||||
private CouponDao mCouponDao;
|
||||
private LiveData<List<Coupon>> mAllCoupons;
|
||||
|
||||
public CouponRepository(Context context) {
|
||||
Database db = Database.getDatabase(context);
|
||||
mCouponDao = db.couponDao();
|
||||
mAllCoupons = mCouponDao.selectAll();
|
||||
}
|
||||
|
||||
public LiveData<List<Coupon>> getAllCoupons() {
|
||||
return mAllCoupons;
|
||||
}
|
||||
|
||||
public Coupon getCoupon(String code) {
|
||||
return mCouponDao.selectCouponByCode(code);
|
||||
}
|
||||
}
|
||||
53
app/src/main/java/com/a1/nextlocation/data/db/Database.java
Normal file
53
app/src/main/java/com/a1/nextlocation/data/db/Database.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.DatabaseConfiguration;
|
||||
import androidx.room.InvalidationTracker;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
import androidx.sqlite.db.SupportSQLiteOpenHelper;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@androidx.room.Database(entities = {Coupon.class,Route.class},version = 1,exportSchema = false)
|
||||
public abstract class Database extends RoomDatabase {
|
||||
|
||||
public abstract RouteDao routeDao();
|
||||
public abstract CouponDao couponDao();
|
||||
|
||||
private static volatile Database INSTANCE;
|
||||
private static final int NUMBER_OF_THREADS = 4;
|
||||
static final ExecutorService databaseWriterExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
|
||||
|
||||
static Database getDatabase(final Context context) {
|
||||
if (INSTANCE == null){
|
||||
synchronized (Database.class) {
|
||||
if (INSTANCE == null) {
|
||||
|
||||
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
|
||||
Database.class,"next_location_db").addCallback(callback).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private static RoomDatabase.Callback callback = new RoomDatabase.Callback() {
|
||||
@Override
|
||||
public void onCreate(@NonNull SupportSQLiteDatabase db) {
|
||||
super.onCreate(db);
|
||||
|
||||
databaseWriterExecutor.execute(() -> {
|
||||
// TODO populate our database here
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
30
app/src/main/java/com/a1/nextlocation/data/db/RouteDao.java
Normal file
30
app/src/main/java/com/a1/nextlocation/data/db/RouteDao.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.a1.nextlocation.data.db;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.a1.nextlocation.data.Route;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface RouteDao {
|
||||
|
||||
@Insert
|
||||
void insertAll(Route... routes);
|
||||
|
||||
@Query("DELETE FROM route")
|
||||
void deleteAll();
|
||||
|
||||
@Query("SELECT * FROM route")
|
||||
LiveData<List<Route>> getAll();
|
||||
|
||||
@Update
|
||||
void update(Route route);
|
||||
|
||||
@Query("SELECT * FROM route where name = :name LIMIT 1")
|
||||
Route getRouteByName(String name);
|
||||
}
|
||||
Reference in New Issue
Block a user