From 38be0ba01f6dd08c0be1a830e0d0389405949a56 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 7 Jan 2019 19:03:49 +0800 Subject: New feature: Subscribe or unsubscribe to a subreddit. Allow clear text traffic in Android Pie. Rewrite some code in lambda. --- .../main/java/SubredditDatabase/SubredditDao.java | 5 +--- .../main/java/SubredditDatabase/SubredditData.java | 35 ++++------------------ .../SubredditDatabase/SubredditRepository.java | 9 ++---- .../java/SubredditDatabase/SubredditViewModel.java | 14 ++++----- 4 files changed, 15 insertions(+), 48 deletions(-) (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 index e7b564f7..ac291f9a 100644 --- a/app/src/main/java/SubredditDatabase/SubredditDao.java +++ b/app/src/main/java/SubredditDatabase/SubredditDao.java @@ -14,11 +14,8 @@ public interface SubredditDao { @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); + LiveData getSubredditLiveDataByName(String namePrefixed); @Query("SELECT * from subreddits WHERE name = :namePrefixed LIMIT 1") SubredditData getSubredditData(String namePrefixed); diff --git a/app/src/main/java/SubredditDatabase/SubredditData.java b/app/src/main/java/SubredditDatabase/SubredditData.java index bbd9f7d8..973eda18 100644 --- a/app/src/main/java/SubredditDatabase/SubredditData.java +++ b/app/src/main/java/SubredditDatabase/SubredditData.java @@ -2,23 +2,13 @@ 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; +import SubscribedSubredditDatabase.SubscribedSubredditData; - @ColumnInfo(name = "banner_url") +@Entity(tableName = "subreddits") +public class SubredditData extends SubscribedSubredditData { + @ColumnInfo(name = "banner") private String bannerUrl; @ColumnInfo(name = "description") @@ -28,27 +18,12 @@ public class SubredditData { 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; + super(id, name, 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; } diff --git a/app/src/main/java/SubredditDatabase/SubredditRepository.java b/app/src/main/java/SubredditDatabase/SubredditRepository.java index 20eb617a..495f6e3d 100644 --- a/app/src/main/java/SubredditDatabase/SubredditRepository.java +++ b/app/src/main/java/SubredditDatabase/SubredditRepository.java @@ -8,14 +8,11 @@ public class SubredditRepository { private SubredditDao mSubredditDao; private LiveData mSubredditLiveData; - SubredditRepository(Application application, String value, boolean isId) { + SubredditRepository(Application application, String subredditName) { SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application); mSubredditDao = db.subredditDao(); - if(isId) { - mSubredditLiveData = mSubredditDao.getSubredditLiveDataById(value); - } else { - mSubredditLiveData = mSubredditDao.getSubredditLiveDataByNamePrefixed(value); - } + + mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName); } LiveData getSubredditLiveData() { diff --git a/app/src/main/java/SubredditDatabase/SubredditViewModel.java b/app/src/main/java/SubredditDatabase/SubredditViewModel.java index 10135401..a9d176b3 100644 --- a/app/src/main/java/SubredditDatabase/SubredditViewModel.java +++ b/app/src/main/java/SubredditDatabase/SubredditViewModel.java @@ -11,9 +11,9 @@ public class SubredditViewModel extends AndroidViewModel { private SubredditRepository mSubredditRepository; private LiveData mSubredditLiveData; - SubredditViewModel(Application application, String id, boolean isId) { + SubredditViewModel(Application application, String id) { super(application); - mSubredditRepository = new SubredditRepository(application, id, isId); + mSubredditRepository = new SubredditRepository(application, id); mSubredditLiveData = mSubredditRepository.getSubredditLiveData(); } @@ -30,19 +30,17 @@ public class SubredditViewModel extends AndroidViewModel { @NonNull private final Application mApplication; - private final String value; - private final boolean isId; + private final String subredditName; - public Factory(@NonNull Application application, String value, boolean isId) { + public Factory(@NonNull Application application, String subredditName) { mApplication = application; - this.value = value; - this.isId = isId; + this.subredditName = subredditName; } @Override public T create(Class modelClass) { //noinspection unchecked - return (T) new SubredditViewModel(mApplication, value, isId); + return (T) new SubredditViewModel(mApplication, subredditName); } } } -- cgit v1.2.3