From 807ddf5a6e595d2368579008bb8132296be54ea2 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 27 Dec 2018 13:54:19 +0800 Subject: Move several classes to other packages. --- .../SubscribedSubredditDao.java | 21 +++++++++++ .../SubscribedSubredditData.java | 38 ++++++++++++++++++++ .../SubscribedSubredditRepository.java | 41 ++++++++++++++++++++++ .../SubscribedSubredditRoomDatabase.java | 26 ++++++++++++++ .../SubscribedSubredditViewModel.java | 26 ++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditDao.java create mode 100644 app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditData.java create mode 100644 app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRepository.java create mode 100644 app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditRoomDatabase.java create mode 100644 app/src/main/java/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java (limited to 'app/src/main/java/SubscribedSubredditDatabase') 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> 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> mAllSubscribedSubreddits; + + SubscribedSubredditRepository(Application application) { + SubscribedSubredditRoomDatabase db = SubscribedSubredditRoomDatabase.getDatabase(application); + mSubscribedSubredditDao = db.subscribedSubredditDao(); + mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(); + } + + LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + new insertAsyncTask(mSubscribedSubredditDao).execute(subscribedSubredditData); + } + + private static class insertAsyncTask extends AsyncTask { + + 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> mAllSubscribedSubreddits; + + public SubscribedSubredditViewModel(Application application) { + super(application); + mSubscribedSubredditRepository = new SubscribedSubredditRepository(application); + mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); + } + + public LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + mSubscribedSubredditRepository.insert(subscribedSubredditData); + } +} -- cgit v1.2.3