added database and coupon and route daos
This commit is contained in:
@@ -1,29 +1,48 @@
|
|||||||
package com.a1.nextlocation.data;
|
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 {
|
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;
|
private String code;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "reward")
|
||||||
|
@NonNull
|
||||||
private String reward;
|
private String reward;
|
||||||
|
|
||||||
|
|
||||||
public Coupon(String code, String reward) {
|
public Coupon(@NonNull String code, @NotNull String reward) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.reward = reward;
|
this.reward = reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCode(String code) {
|
@NonNull
|
||||||
this.code = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getReward() {
|
public String getReward() {
|
||||||
return reward;
|
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;
|
this.reward = reward;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,31 @@
|
|||||||
package com.a1.nextlocation.data;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
public class Route {
|
public class Route {
|
||||||
|
|
||||||
|
@PrimaryKey
|
||||||
|
@NonNull
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private List<Location> locations;
|
private List<Location> locations;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "total_distance")
|
||||||
private float totalDistance;
|
private float totalDistance;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "total_time")
|
||||||
private int totalTime;
|
private int totalTime;
|
||||||
|
|
||||||
public Route(String name) {
|
public Route(@NotNull String name) {
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.locations = new ArrayList<>();
|
this.locations = new ArrayList<>();
|
||||||
@@ -20,11 +36,12 @@ public class Route {
|
|||||||
this.locations.add(location);
|
this.locations.add(location);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(@NotNull String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,4 +64,12 @@ public class Route {
|
|||||||
return totalTime;
|
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