aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/SubscribedSubredditDatabase
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/SubscribedSubredditDatabase
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/SubscribedSubredditDatabase')
-rw-r--r--app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditDao.java21
-rw-r--r--app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditData.java38
-rw-r--r--app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRepository.java41
-rw-r--r--app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRoomDatabase.java26
-rw-r--r--app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java26
5 files changed, 152 insertions, 0 deletions
diff --git a/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditDao.java b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditDao.java
new file mode 100644
index 00000000..7837aa42
--- /dev/null
+++ b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditDao.java
@@ -0,0 +1,21 @@
+package SubscribedSubredditDatabase;
+
+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;
+
+import java.util.List;
+
+@Dao
+public interface SubscribedSubredditDao {
+ @Insert(onConflict = OnConflictStrategy.REPLACE)
+ void insert(SubscribedSubredditData subscribedSubredditData);
+
+ @Query("DELETE FROM subscribed_subreddits")
+ void deleteAllSubscribedSubreddits();
+
+ @Query("SELECT * from subscribed_subreddits ORDER BY name COLLATE NOCASE ASC")
+ LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits();
+}
diff --git a/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditData.java b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditData.java
new file mode 100644
index 00000000..c6812768
--- /dev/null
+++ b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditData.java
@@ -0,0 +1,38 @@
+package SubscribedSubredditDatabase;
+
+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 = "subscribed_subreddits")
+public class SubscribedSubredditData {
+
+ @PrimaryKey
+ @NonNull
+ @ColumnInfo(name = "id")
+ private String id;
+ @ColumnInfo(name = "name")
+ private String name;
+ @ColumnInfo(name = "icon")
+ private String iconUrl;
+
+ public SubscribedSubredditData(@NonNull String id, String name, String iconUrl) {
+ this.id = id;
+ this.name = name;
+ this.iconUrl = iconUrl;
+ }
+
+ @NonNull
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getIconUrl() {
+ return iconUrl;
+ }
+}
diff --git a/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRepository.java b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRepository.java
new file mode 100644
index 00000000..54b2ddfa
--- /dev/null
+++ b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRepository.java
@@ -0,0 +1,41 @@
+package SubscribedSubredditDatabase;
+
+import android.app.Application;
+import android.arch.lifecycle.LiveData;
+import android.os.AsyncTask;
+
+import java.util.List;
+
+public class SubscribedSubredditRepository {
+ private SubscribedSubredditDao mSubscribedSubredditDao;
+ private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
+
+ SubscribedSubredditRepository(Application application) {
+ SubscribedSubredditRoomDatabase db = SubscribedSubredditRoomDatabase.getDatabase(application);
+ mSubscribedSubredditDao = db.subscribedSubredditDao();
+ mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits();
+ }
+
+ LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() {
+ return mAllSubscribedSubreddits;
+ }
+
+ public void insert(SubscribedSubredditData subscribedSubredditData) {
+ new insertAsyncTask(mSubscribedSubredditDao).execute(subscribedSubredditData);
+ }
+
+ private static class insertAsyncTask extends AsyncTask<SubscribedSubredditData, Void, Void> {
+
+ private SubscribedSubredditDao mAsyncTaskDao;
+
+ insertAsyncTask(SubscribedSubredditDao dao) {
+ mAsyncTaskDao = dao;
+ }
+
+ @Override
+ protected Void doInBackground(final SubscribedSubredditData... params) {
+ mAsyncTaskDao.insert(params[0]);
+ return null;
+ }
+ }
+}
diff --git a/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRoomDatabase.java b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRoomDatabase.java
new file mode 100644
index 00000000..abde79b4
--- /dev/null
+++ b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRoomDatabase.java
@@ -0,0 +1,26 @@
+package SubscribedSubredditDatabase;
+
+import android.arch.persistence.room.Database;
+import android.arch.persistence.room.Room;
+import android.arch.persistence.room.RoomDatabase;
+import android.content.Context;
+
+@Database(entities = {SubscribedSubredditData.class}, version = 1)
+public abstract class SubscribedSubredditRoomDatabase extends RoomDatabase {
+ private static SubscribedSubredditRoomDatabase INSTANCE;
+
+ public abstract SubscribedSubredditDao subscribedSubredditDao();
+
+ public static SubscribedSubredditRoomDatabase getDatabase(final Context context) {
+ if(INSTANCE == null) {
+ synchronized (SubscribedSubredditRoomDatabase.class) {
+ if(INSTANCE == null) {
+ INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
+ SubscribedSubredditRoomDatabase.class, "subscribed_subreddits")
+ .build();
+ }
+ }
+ }
+ return INSTANCE;
+ }
+}
diff --git a/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java
new file mode 100644
index 00000000..8598dd55
--- /dev/null
+++ b/app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java
@@ -0,0 +1,26 @@
+package SubscribedSubredditDatabase;
+
+import android.app.Application;
+import android.arch.lifecycle.AndroidViewModel;
+import android.arch.lifecycle.LiveData;
+
+import java.util.List;
+
+public class SubscribedSubredditViewModel extends AndroidViewModel {
+ private SubscribedSubredditRepository mSubscribedSubredditRepository;
+ private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
+
+ public SubscribedSubredditViewModel(Application application) {
+ super(application);
+ mSubscribedSubredditRepository = new SubscribedSubredditRepository(application);
+ mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits();
+ }
+
+ public LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() {
+ return mAllSubscribedSubreddits;
+ }
+
+ public void insert(SubscribedSubredditData subscribedSubredditData) {
+ mSubscribedSubredditRepository.insert(subscribedSubredditData);
+ }
+}