added all daos
This commit is contained in:
@@ -35,6 +35,8 @@ dependencies {
|
|||||||
implementation "androidx.room:room-runtime:$room_version"
|
implementation "androidx.room:room-runtime:$room_version"
|
||||||
annotationProcessor "androidx.room:room-compiler:$room_version"
|
annotationProcessor "androidx.room:room-compiler:$room_version"
|
||||||
|
|
||||||
|
//gson
|
||||||
|
implementation 'com.google.code.gson:gson:2.8.6'
|
||||||
|
|
||||||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||||
implementation 'com.google.android.material:material:1.2.1'
|
implementation 'com.google.android.material:material:1.2.1'
|
||||||
|
|||||||
@@ -1,23 +1,34 @@
|
|||||||
package com.a1.nextlocation.data;
|
package com.a1.nextlocation.data;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.room.ColumnInfo;
|
||||||
import androidx.room.Entity;
|
import androidx.room.Entity;
|
||||||
import androidx.room.Ignore;
|
import androidx.room.Ignore;
|
||||||
import androidx.room.PrimaryKey;
|
import androidx.room.PrimaryKey;
|
||||||
|
import androidx.room.TypeConverters;
|
||||||
|
|
||||||
|
import com.a1.nextlocation.data.db.CouponListTypeConverter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity(tableName = "userdata")
|
||||||
public class Data {
|
public class Data {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PrimaryKey
|
@PrimaryKey
|
||||||
@NonNull
|
@NonNull
|
||||||
|
@ColumnInfo(name = "distance_traveled")
|
||||||
private float distanceTraveled;
|
private float distanceTraveled;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "locations_visited")
|
||||||
private int locationsVisited;
|
private int locationsVisited;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "total_time")
|
||||||
private int totalTime;
|
private int totalTime;
|
||||||
|
|
||||||
|
@TypeConverters(CouponListTypeConverter.class)
|
||||||
private List<Coupon> couponList;
|
private List<Coupon> couponList;
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
@@ -34,6 +45,7 @@ public class Data {
|
|||||||
this.distanceTraveled = 0;
|
this.distanceTraveled = 0;
|
||||||
this.locationsVisited = 0;
|
this.locationsVisited = 0;
|
||||||
this.totalTime = 0;
|
this.totalTime = 0;
|
||||||
|
couponList = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.room.ColumnInfo;
|
import androidx.room.ColumnInfo;
|
||||||
import androidx.room.Entity;
|
import androidx.room.Entity;
|
||||||
import androidx.room.PrimaryKey;
|
import androidx.room.PrimaryKey;
|
||||||
|
import androidx.room.TypeConverters;
|
||||||
|
|
||||||
|
import com.a1.nextlocation.data.db.LocationListTypeConverter;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -17,6 +20,7 @@ public class Route {
|
|||||||
@NonNull
|
@NonNull
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@TypeConverters(LocationListTypeConverter.class)
|
||||||
private List<Location> locations;
|
private List<Location> locations;
|
||||||
|
|
||||||
@ColumnInfo(name = "total_distance")
|
@ColumnInfo(name = "total_distance")
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.a1.nextlocation.data.db;
|
||||||
|
|
||||||
|
import androidx.room.TypeConverter;
|
||||||
|
|
||||||
|
import com.a1.nextlocation.data.Coupon;
|
||||||
|
import com.a1.nextlocation.data.Route;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CouponListTypeConverter {
|
||||||
|
private static Gson gson = new Gson();
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
public static List<Coupon> toRoutesList(String data) {
|
||||||
|
if (data == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
Type listType = new TypeToken<List<Coupon>>() {}.getType();
|
||||||
|
|
||||||
|
return gson.fromJson(data,listType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
public static String fromRoutesList(List<Coupon> list) {
|
||||||
|
return gson.toJson(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,9 +8,11 @@ import androidx.room.RoomDatabase;
|
|||||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||||
|
|
||||||
import com.a1.nextlocation.data.Coupon;
|
import com.a1.nextlocation.data.Coupon;
|
||||||
|
import com.a1.nextlocation.data.Data;
|
||||||
import com.a1.nextlocation.data.Location;
|
import com.a1.nextlocation.data.Location;
|
||||||
import com.a1.nextlocation.data.Route;
|
import com.a1.nextlocation.data.Route;
|
||||||
import com.a1.nextlocation.data.db.dao.CouponDao;
|
import com.a1.nextlocation.data.db.dao.CouponDao;
|
||||||
|
import com.a1.nextlocation.data.db.dao.DataDao;
|
||||||
import com.a1.nextlocation.data.db.dao.LocationDao;
|
import com.a1.nextlocation.data.db.dao.LocationDao;
|
||||||
import com.a1.nextlocation.data.db.dao.RouteDao;
|
import com.a1.nextlocation.data.db.dao.RouteDao;
|
||||||
|
|
||||||
@@ -20,12 +22,13 @@ import java.util.concurrent.Executors;
|
|||||||
/**
|
/**
|
||||||
* @author Sem
|
* @author Sem
|
||||||
*/
|
*/
|
||||||
@androidx.room.Database(entities = {Coupon.class,Route.class, Location.class},version = 1,exportSchema = false)
|
@androidx.room.Database(entities = {Coupon.class,Route.class, Location.class, Data.class},version = 1,exportSchema = false)
|
||||||
public abstract class Database extends RoomDatabase {
|
public abstract class Database extends RoomDatabase {
|
||||||
|
|
||||||
public abstract RouteDao routeDao();
|
public abstract RouteDao routeDao();
|
||||||
public abstract CouponDao couponDao();
|
public abstract CouponDao couponDao();
|
||||||
public abstract LocationDao locationDao();
|
public abstract LocationDao locationDao();
|
||||||
|
public abstract DataDao dataDao();
|
||||||
|
|
||||||
private static volatile Database INSTANCE;
|
private static volatile Database INSTANCE;
|
||||||
private static final int NUMBER_OF_THREADS = 4;
|
private static final int NUMBER_OF_THREADS = 4;
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.a1.nextlocation.data.db;
|
||||||
|
|
||||||
|
import androidx.room.TypeConverter;
|
||||||
|
|
||||||
|
import com.a1.nextlocation.data.Location;
|
||||||
|
import com.a1.nextlocation.data.Route;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class LocationListTypeConverter {
|
||||||
|
private static Gson gson = new Gson();
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
public static List<Location> toLocationList(String data) {
|
||||||
|
if (data == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
Type listType = new TypeToken<List<Location>>() {}.getType();
|
||||||
|
|
||||||
|
return gson.fromJson(data,listType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
public static String fromLocationList(List<Location> list) {
|
||||||
|
return gson.toJson(list);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.a1.nextlocation.data.db.dao;
|
||||||
|
|
||||||
|
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.Data;
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
public interface DataDao {
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
void insertAll(Data... datas);
|
||||||
|
|
||||||
|
@Update
|
||||||
|
void update(Data data);
|
||||||
|
|
||||||
|
@Query("DELETE FROM userdata")
|
||||||
|
void delete();
|
||||||
|
|
||||||
|
@Query("SELECT * FROM userdata LIMIT 1")
|
||||||
|
Data getData();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.a1.nextlocation.data.db.repositories;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.a1.nextlocation.data.Data;
|
||||||
|
import com.a1.nextlocation.data.db.Database;
|
||||||
|
import com.a1.nextlocation.data.db.dao.DataDao;
|
||||||
|
|
||||||
|
public class DataRepository {
|
||||||
|
private DataDao mDataDao;
|
||||||
|
private Data data;
|
||||||
|
|
||||||
|
public DataRepository(Context context) {
|
||||||
|
Database db = Database.getDatabase(context);
|
||||||
|
mDataDao = db.dataDao();
|
||||||
|
data = mDataDao.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Data getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
#Tue Dec 08 10:31:45 CET 2020
|
#Mon Dec 14 14:48:57 CET 2020
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
|
||||||
|
|||||||
Reference in New Issue
Block a user