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. --- .../main/java/SubredditDatabase/SubredditDao.java | 25 +++++++++ .../main/java/SubredditDatabase/SubredditData.java | 63 ++++++++++++++++++++++ .../SubredditDatabase/SubredditRepository.java | 43 +++++++++++++++ .../SubredditDatabase/SubredditRoomDatabase.java | 26 +++++++++ .../java/SubredditDatabase/SubredditViewModel.java | 48 +++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 app/src/main/java/SubredditDatabase/SubredditDao.java create mode 100644 app/src/main/java/SubredditDatabase/SubredditData.java create mode 100644 app/src/main/java/SubredditDatabase/SubredditRepository.java create mode 100644 app/src/main/java/SubredditDatabase/SubredditRoomDatabase.java create mode 100644 app/src/main/java/SubredditDatabase/SubredditViewModel.java (limited to 'app/src/main/java/SubredditDatabase') 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 getSubredditLiveDataById(String id); + + @Query("SELECT * from subreddits WHERE name = :namePrefixed") + LiveData 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 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 getSubredditLiveData() { + return mSubredditLiveData; + } + + public void insert(SubredditData subredditData) { + new SubredditRepository.insertAsyncTask(mSubredditDao).execute(subredditData); + } + + private static class insertAsyncTask extends AsyncTask { + + 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 mSubredditLiveData; + + SubredditViewModel(Application application, String id, boolean isId) { + super(application); + mSubredditRepository = new SubredditRepository(application, id, isId); + mSubredditLiveData = mSubredditRepository.getSubredditLiveData(); + } + + public LiveData 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 create(Class modelClass) { + //noinspection unchecked + return (T) new SubredditViewModel(mApplication, value, isId); + } + } +} -- cgit v1.2.3