aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/SubredditDatabase
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2018-12-27 05:54:19 +0000
committerAlex Ning <chineseperson5@gmail.com>2018-12-27 05:54:19 +0000
commit807ddf5a6e595d2368579008bb8132296be54ea2 (patch)
treec1f4fc8c398768f949dd656cd37c9e72d912eddd /app/src/main/java/SubredditDatabase
parent5603df75469929ee122edd93d9d55774375182b0 (diff)
downloadinfinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.tar
infinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.tar.gz
infinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.tar.bz2
infinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.tar.lz
infinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.tar.xz
infinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.tar.zst
infinity-for-reddit-807ddf5a6e595d2368579008bb8132296be54ea2.zip
Move several classes to other packages.
Diffstat (limited to 'app/src/main/java/SubredditDatabase')
-rw-r--r--app/src/main/java/SubredditDatabase/SubredditDao.java25
-rw-r--r--app/src/main/java/SubredditDatabase/SubredditData.java63
-rw-r--r--app/src/main/java/SubredditDatabase/SubredditRepository.java43
-rw-r--r--app/src/main/java/SubredditDatabase/SubredditRoomDatabase.java26
-rw-r--r--app/src/main/java/SubredditDatabase/SubredditViewModel.java48
5 files changed, 205 insertions, 0 deletions
diff --git a/app/src/main/java/SubredditDatabase/SubredditDao.java b/app/src/main/java/SubredditDatabase/SubredditDao.java
new file mode 100644
index 00000000..9b2fcabb
--- /dev/null
+++ b/app/src/main/java/SubredditDatabase/SubredditDao.java
@@ -0,0 +1,25 @@
+package SubredditDatabase;
+
+import android.arch.lifecycle.LiveData;
+import android.arch.persistence.room.Dao;
+import android.arch.persistence.room.Insert;
+import android.arch.persistence.room.OnConflictStrategy;
+import android.arch.persistence.room.Query;
+
+@Dao
+public interface SubredditDao {
+ @Insert(onConflict = OnConflictStrategy.REPLACE)
+ void insert(SubredditData SubredditData);
+
+ @Query("DELETE FROM subreddits")
+ void deleteAllSubreddits();
+
+ @Query("SELECT * from subreddits WHERE id = :id")
+ LiveData<SubredditData> getSubredditLiveDataById(String id);
+
+ @Query("SELECT * from subreddits WHERE name = :namePrefixed")
+ LiveData<SubredditData> getSubredditLiveDataByNamePrefixed(String namePrefixed);
+
+ @Query("SELECT * from subreddits WHERE name = :namePrefixed")
+ SubredditData getSubredditData(String namePrefixed);
+}
diff --git a/app/src/main/java/SubredditDatabase/SubredditData.java b/app/src/main/java/SubredditDatabase/SubredditData.java
new file mode 100644
index 00000000..bbd9f7d8
--- /dev/null
+++ b/app/src/main/java/SubredditDatabase/SubredditData.java
@@ -0,0 +1,63 @@
+package SubredditDatabase;
+
+import android.arch.persistence.room.ColumnInfo;
+import android.arch.persistence.room.Entity;
+import android.arch.persistence.room.PrimaryKey;
+import android.support.annotation.NonNull;
+
+@Entity(tableName = "subreddits")
+public class SubredditData {
+ @PrimaryKey
+ @NonNull
+ @ColumnInfo(name = "id")
+ private String id;
+
+ @ColumnInfo(name = "name")
+ private String name;
+
+ @ColumnInfo(name = "icon_url")
+ private String iconUrl;
+
+ @ColumnInfo(name = "banner_url")
+ private String bannerUrl;
+
+ @ColumnInfo(name = "description")
+ private String description;
+
+ @ColumnInfo(name = "subscribers_count")
+ private int nSubscribers;
+
+ public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, String description, int nSubscribers) {
+ this.id = id;
+ this.name = name;
+ this.iconUrl = iconUrl;
+ this.bannerUrl = bannerUrl;
+ this.description = description;
+ this.nSubscribers = nSubscribers;
+ }
+
+ @NonNull
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getIconUrl() {
+ return iconUrl;
+ }
+
+ public String getBannerUrl() {
+ return bannerUrl;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public int getNSubscribers() {
+ return nSubscribers;
+ }
+}
diff --git a/app/src/main/java/SubredditDatabase/SubredditRepository.java b/app/src/main/java/SubredditDatabase/SubredditRepository.java
new file mode 100644
index 00000000..20eb617a
--- /dev/null
+++ b/app/src/main/java/SubredditDatabase/SubredditRepository.java
@@ -0,0 +1,43 @@
+package SubredditDatabase;
+
+import android.app.Application;
+import android.arch.lifecycle.LiveData;
+import android.os.AsyncTask;
+
+public class SubredditRepository {
+ private SubredditDao mSubredditDao;
+ private LiveData<SubredditData> mSubredditLiveData;
+
+ SubredditRepository(Application application, String value, boolean isId) {
+ SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application);
+ mSubredditDao = db.subredditDao();
+ if(isId) {
+ mSubredditLiveData = mSubredditDao.getSubredditLiveDataById(value);
+ } else {
+ mSubredditLiveData = mSubredditDao.getSubredditLiveDataByNamePrefixed(value);
+ }
+ }
+
+ LiveData<SubredditData> getSubredditLiveData() {
+ return mSubredditLiveData;
+ }
+
+ public void insert(SubredditData subredditData) {
+ new SubredditRepository.insertAsyncTask(mSubredditDao).execute(subredditData);
+ }
+
+ private static class insertAsyncTask extends AsyncTask<SubredditData, Void, Void> {
+
+ private SubredditDao mAsyncTaskDao;
+
+ insertAsyncTask(SubredditDao dao) {
+ mAsyncTaskDao = dao;
+ }
+
+ @Override
+ protected Void doInBackground(final SubredditData... params) {
+ mAsyncTaskDao.insert(params[0]);
+ return null;
+ }
+ }
+}
diff --git a/app/src/main/java/SubredditDatabase/SubredditRoomDatabase.java b/app/src/main/java/SubredditDatabase/SubredditRoomDatabase.java
new file mode 100644
index 00000000..764a3741
--- /dev/null
+++ b/app/src/main/java/SubredditDatabase/SubredditRoomDatabase.java
@@ -0,0 +1,26 @@
+package SubredditDatabase;
+
+import android.arch.persistence.room.Database;
+import android.arch.persistence.room.Room;
+import android.arch.persistence.room.RoomDatabase;
+import android.content.Context;
+
+@Database(entities = {SubredditData.class}, version = 1)
+public abstract class SubredditRoomDatabase extends RoomDatabase{
+ private static SubredditRoomDatabase INSTANCE;
+
+ public abstract SubredditDao subredditDao();
+
+ public static SubredditRoomDatabase getDatabase(final Context context) {
+ if(INSTANCE == null) {
+ synchronized (SubredditRoomDatabase.class) {
+ if(INSTANCE == null) {
+ INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
+ SubredditRoomDatabase.class, "subreddits")
+ .build();
+ }
+ }
+ }
+ return INSTANCE;
+ }
+}
diff --git a/app/src/main/java/SubredditDatabase/SubredditViewModel.java b/app/src/main/java/SubredditDatabase/SubredditViewModel.java
new file mode 100644
index 00000000..10135401
--- /dev/null
+++ b/app/src/main/java/SubredditDatabase/SubredditViewModel.java
@@ -0,0 +1,48 @@
+package SubredditDatabase;
+
+import android.app.Application;
+import android.arch.lifecycle.AndroidViewModel;
+import android.arch.lifecycle.LiveData;
+import android.arch.lifecycle.ViewModel;
+import android.arch.lifecycle.ViewModelProvider;
+import android.support.annotation.NonNull;
+
+public class SubredditViewModel extends AndroidViewModel {
+ private SubredditRepository mSubredditRepository;
+ private LiveData<SubredditData> mSubredditLiveData;
+
+ SubredditViewModel(Application application, String id, boolean isId) {
+ super(application);
+ mSubredditRepository = new SubredditRepository(application, id, isId);
+ mSubredditLiveData = mSubredditRepository.getSubredditLiveData();
+ }
+
+ public LiveData<SubredditData> getSubredditLiveData() {
+ return mSubredditLiveData;
+ }
+
+ public void insert(SubredditData subredditData) {
+ mSubredditRepository.insert(subredditData);
+ }
+
+ public static class Factory extends ViewModelProvider.NewInstanceFactory {
+
+ @NonNull
+ private final Application mApplication;
+
+ private final String value;
+ private final boolean isId;
+
+ public Factory(@NonNull Application application, String value, boolean isId) {
+ mApplication = application;
+ this.value = value;
+ this.isId = isId;
+ }
+
+ @Override
+ public <T extends ViewModel> T create(Class<T> modelClass) {
+ //noinspection unchecked
+ return (T) new SubredditViewModel(mApplication, value, isId);
+ }
+ }
+}