From 3227e43fc4bc914f8bde1d06017ae16ced3fe278 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 29 Sep 2019 09:49:30 +0800 Subject: Change project structures. --- .../AccessTokenAuthenticator.java | 2 +- .../infinityforreddit/Account/Account.java | 76 +++++++++++++ .../infinityforreddit/Account/AccountDao.java | 55 ++++++++++ .../Account/AccountRepository.java | 42 ++++++++ .../Account/AccountViewModel.java | 52 +++++++++ .../AccountRecyclerViewAdapter.java | 2 +- .../infinityforreddit/AppComponent.java | 4 +- .../CheckIsFollowingUserAsyncTask.java | 4 +- .../CheckIsSubscribedToSubredditAsyncTask.java | 2 +- .../CommentAndPostRecyclerViewAdapter.java | 2 +- .../CustomView/AspectRatioGifImageView.java | 54 ++++++++++ .../infinityforreddit/FetchSubredditData.java | 4 +- .../infinityforreddit/FetchSubscribedThing.java | 8 +- .../infinityforreddit/FetchUserData.java | 4 +- .../FollowedUsersListingFragment.java | 2 +- .../FollowedUsersRecyclerViewAdapter.java | 2 +- .../GetCurrentAccountAsyncTask.java | 4 +- .../InsertSubscribedThingsAsyncTask.java | 12 +-- .../infinityforreddit/InsertUserDataAsyncTask.java | 4 +- .../LoadSubredditIconAsyncTask.java | 4 +- .../infinityforreddit/LoadUserDataAsyncTask.java | 4 +- .../infinityforreddit/MainActivity.java | 10 +- .../ParseAndInsertNewAccountAsyncTask.java | 4 +- .../infinityforreddit/ParseSubredditData.java | 2 +- .../infinityforreddit/ParseSubscribedThing.java | 6 +- .../infinityforreddit/ParseUserData.java | 2 +- .../infinityforreddit/PostRecyclerViewAdapter.java | 4 +- .../infinityforreddit/PullNotificationWorker.java | 3 +- .../infinityforreddit/RedditDataRoomDatabase.java | 20 ++-- .../Settings/AboutPreferenceFragment.java | 91 ++++++++++++++++ .../Settings/Acknowledgement.java | 27 +++++ .../Settings/AcknowledgementFragment.java | 106 ++++++++++++++++++ .../AcknowledgementRecyclerViewAdapter.java | 67 ++++++++++++ .../Settings/CreditsPreferenceFragment.java | 100 +++++++++++++++++ .../Settings/FontSizePreferenceFragment.java | 49 +++++++++ .../Settings/MainPreferenceFragment.java | 113 ++++++++++++++++++++ .../Settings/NotificationPreferenceFragment.java | 118 +++++++++++++++++++++ .../infinityforreddit/SettingsActivity.java | 4 +- .../SubredditDatabase/SubredditDao.java | 22 ++++ .../SubredditDatabase/SubredditData.java | 58 ++++++++++ .../SubredditDatabase/SubredditRepository.java | 40 +++++++ .../SubredditDatabase/SubredditViewModel.java | 50 +++++++++ .../SubredditListingDataSource.java | 2 +- .../SubredditListingRecyclerViewAdapter.java | 2 +- .../SubredditListingViewModel.java | 2 +- .../SubredditSelectionActivity.java | 6 +- .../infinityforreddit/SubredditSubscription.java | 4 +- .../SubscribedSubredditDao.java | 27 +++++ .../SubscribedSubredditData.java | 53 +++++++++ .../SubscribedSubredditRepository.java | 42 ++++++++ .../SubscribedSubredditViewModel.java | 50 +++++++++ .../SubscribedSubredditsListingFragment.java | 2 +- .../SubscribedSubredditsRecyclerViewAdapter.java | 2 +- .../SubscribedThingListingActivity.java | 6 +- .../SubscribedUserDatabase/SubscribedUserDao.java | 27 +++++ .../SubscribedUserDatabase/SubscribedUserData.java | 45 ++++++++ .../SubscribedUserRepository.java | 42 ++++++++ .../SubscribedUserViewModel.java | 50 +++++++++ .../infinityforreddit/SwitchAccountAsyncTask.java | 2 +- .../SwitchToAnonymousAccountAsyncTask.java | 2 +- .../infinityforreddit/User/UserDao.java | 22 ++++ .../infinityforreddit/User/UserData.java | 65 ++++++++++++ .../infinityforreddit/User/UserRepository.java | 40 +++++++ .../infinityforreddit/User/UserViewModel.java | 49 +++++++++ .../infinityforreddit/UserFollowing.java | 6 +- .../infinityforreddit/UserListingDataSource.java | 6 +- .../UserListingRecyclerViewAdapter.java | 4 +- .../infinityforreddit/UserListingViewModel.java | 2 +- .../ViewSubredditDetailActivity.java | 6 +- .../infinityforreddit/ViewUserDetailActivity.java | 8 +- 70 files changed, 1719 insertions(+), 94 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Account/Account.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/CustomView/AspectRatioGifImageView.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AboutPreferenceFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/Acknowledgement.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementRecyclerViewAdapter.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CreditsPreferenceFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/FontSizePreferenceFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserDao.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserRepository.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/User/UserViewModel.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index ff9ae1f5..96a1af0d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -10,7 +10,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import Account.Account; +import ml.docilealligator.infinityforreddit.Account.Account; import okhttp3.Authenticator; import okhttp3.Headers; import okhttp3.Request; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/Account.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/Account.java new file mode 100644 index 00000000..4cb202b5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/Account.java @@ -0,0 +1,76 @@ +package ml.docilealligator.infinityforreddit.Account; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "accounts") +public class Account { + @PrimaryKey + @NonNull + @ColumnInfo(name = "username") + private String username; + @ColumnInfo(name = "profile_image_url") + private String profileImageUrl; + @ColumnInfo(name = "banner_image_url") + private String bannerImageUrl; + @ColumnInfo(name = "karma") + private int karma; + @ColumnInfo(name = "access_token") + private String accessToken; + @ColumnInfo(name = "refresh_token") + private String refreshToken; + @ColumnInfo(name = "code") + private String code; + @ColumnInfo(name = "is_current_user") + private boolean isCurrentUser; + + public Account(@NonNull String username, String accessToken, String refreshToken, String code, + String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) { + this.username = username; + this.accessToken = accessToken; + this.refreshToken = refreshToken; + this.code = code; + this.profileImageUrl = profileImageUrl; + this.bannerImageUrl = bannerImageUrl; + this.karma = karma; + this.isCurrentUser = isCurrentUser; + } + + public String getUsername() { + return username; + } + + public String getProfileImageUrl() { + return profileImageUrl; + } + + public String getBannerImageUrl() { + return bannerImageUrl; + } + + public int getKarma() { + return karma; + } + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + public String getRefreshToken() { + return refreshToken; + } + + public String getCode() { + return code; + } + + public boolean isCurrentUser() { + return isCurrentUser; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountDao.java new file mode 100644 index 00000000..be3dcd9d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountDao.java @@ -0,0 +1,55 @@ +package ml.docilealligator.infinityforreddit.Account; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +@Dao +public interface AccountDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(Account account); + + @Query("SELECT * FROM accounts") + List getAllAccounts(); + + @Query("SELECT * FROM accounts WHERE is_current_user = 0") + List getAllNonCurrentAccounts(); + + @Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1") + void markAllAccountsNonCurrent(); + + @Query("DELETE FROM accounts WHERE is_current_user = 1") + void deleteCurrentAccount(); + + @Query("DELETE FROM accounts") + void deleteAllAccounts(); + + @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1") + LiveData getAccountLiveData(String username); + + @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1") + Account getAccountData(String username); + + @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") + Account getCurrentAccount(); + + @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") + LiveData getCurrentAccountLiveData(); + + @Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " + + "karma = :karma WHERE username = :username") + void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma); + + @Query("SELECT * FROM accounts WHERE is_current_user = 0 ORDER BY username COLLATE NOCASE ASC") + LiveData> getAccountsExceptCurrentAccountLiveData(); + + @Query("UPDATE accounts SET is_current_user = 1 WHERE username = :username") + void markAccountCurrent(String username); + + @Query("UPDATE accounts SET access_token = :accessToken WHERE username = :username") + void changeAccessToken(String username, String accessToken); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java new file mode 100644 index 00000000..69ad4088 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountRepository.java @@ -0,0 +1,42 @@ +package ml.docilealligator.infinityforreddit.Account; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class AccountRepository { + private AccountDao mAccountDao; + private LiveData> mAccountsExceptCurrentAccountLiveData; + + AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mAccountDao = redditDataRoomDatabase.accountDao(); + mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData(); + } + + public LiveData> getAccountsExceptCurrentAccountLiveData() { + return mAccountsExceptCurrentAccountLiveData; + } + + public void insert(Account Account) { + new InsertAsyncTask(mAccountDao).execute(Account); + } + + private static class InsertAsyncTask extends AsyncTask { + + private AccountDao mAsyncTaskDao; + + InsertAsyncTask(AccountDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final Account... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java new file mode 100644 index 00000000..55e805e8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Account/AccountViewModel.java @@ -0,0 +1,52 @@ +package ml.docilealligator.infinityforreddit.Account; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class AccountViewModel extends AndroidViewModel { + private AccountRepository mAccountRepository; + private LiveData> mAccountsExceptCurrentAccountLiveData; + + public AccountViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { + super(application); + mAccountRepository = new AccountRepository(redditDataRoomDatabase, id); + mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData(); + } + + public LiveData> getAccountsExceptCurrentAccountLiveData() { + return mAccountsExceptCurrentAccountLiveData; + } + + public void insert(Account userData) { + mAccountRepository.insert(userData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + @NonNull + private final Application mApplication; + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + private final String mUsername; + + public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mApplication = application; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mUsername = username; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new AccountViewModel(mApplication, mRedditDataRoomDatabase, mUsername); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java index 373c0849..b731be27 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountRecyclerViewAdapter.java @@ -15,7 +15,7 @@ import com.bumptech.glide.request.RequestOptions; import java.util.List; -import Account.Account; +import ml.docilealligator.infinityforreddit.Account.Account; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 1c134841..0dffadd8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit; import javax.inject.Singleton; -import Settings.MainPreferenceFragment; -import Settings.NotificationPreferenceFragment; +import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment; +import ml.docilealligator.infinityforreddit.Settings.NotificationPreferenceFragment; import dagger.Component; @Singleton diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsFollowingUserAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsFollowingUserAsyncTask.java index cc4f75af..b823db11 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsFollowingUserAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsFollowingUserAsyncTask.java @@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import SubscribedUserDatabase.SubscribedUserDao; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; public class CheckIsFollowingUserAsyncTask extends AsyncTask { private SubscribedUserDao subscribedUserDao; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsSubscribedToSubredditAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsSubscribedToSubredditAsyncTask.java index a1936ae0..a1f304e9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsSubscribedToSubredditAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CheckIsSubscribedToSubredditAsyncTask.java @@ -2,7 +2,7 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; class CheckIsSubscribedToSubredditAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 897b2e86..85b4d230 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -39,7 +39,7 @@ import com.santalu.aspectratioimageview.AspectRatioImageView; import java.util.ArrayList; import java.util.Locale; -import CustomView.AspectRatioGifImageView; +import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import butterknife.BindView; import butterknife.ButterKnife; import io.noties.markwon.AbstractMarkwonPlugin; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomView/AspectRatioGifImageView.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomView/AspectRatioGifImageView.java new file mode 100644 index 00000000..565ad200 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomView/AspectRatioGifImageView.java @@ -0,0 +1,54 @@ +package ml.docilealligator.infinityforreddit.CustomView; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; + +import pl.droidsonroids.gif.GifImageView; + +public class AspectRatioGifImageView extends GifImageView { + private float ratio; + + public final float getRatio() { + return this.ratio; + } + + public final void setRatio(float var1) { + this.ratio = var1; + } + + private final void init(Context context, AttributeSet attrs) { + if (attrs != null) { + TypedArray a = context.obtainStyledAttributes(attrs, com.santalu.aspectratioimageview.R.styleable.AspectRatioImageView); + this.ratio = a.getFloat(com.santalu.aspectratioimageview.R.styleable.AspectRatioImageView_ari_ratio, 1.0F); + a.recycle(); + } + + } + + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + int width = this.getMeasuredWidth(); + int height = this.getMeasuredHeight(); + if (width != 0 || height != 0) { + if (width > 0) { + height = (int)((float)width * this.ratio); + } else { + width = (int)((float)height / this.ratio); + } + + this.setMeasuredDimension(width, height); + } + } + + public AspectRatioGifImageView(Context context) { + super(context); + this.ratio = 1.0F; + } + + public AspectRatioGifImageView(Context context, AttributeSet attrs) { + super(context, attrs); + this.ratio = 1.0F; + this.init(context, attrs); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java index 11416e32..696a2dd6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java @@ -1,12 +1,10 @@ package ml.docilealligator.infinityforreddit; -import android.util.Log; - import androidx.annotation.NonNull; import java.util.ArrayList; -import SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java index 4500196a..d1a6977b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -1,14 +1,12 @@ package ml.docilealligator.infinityforreddit; -import android.util.Log; - import androidx.annotation.NonNull; import java.util.ArrayList; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java index 38c76536..e910e531 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java @@ -1,12 +1,10 @@ package ml.docilealligator.infinityforreddit; -import android.util.Log; - import androidx.annotation.NonNull; import java.util.ArrayList; -import User.UserData; +import ml.docilealligator.infinityforreddit.User.UserData; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java index 83a30838..cabbe6ed 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersListingFragment.java @@ -22,7 +22,7 @@ import com.bumptech.glide.RequestManager; import javax.inject.Inject; -import SubscribedUserDatabase.SubscribedUserViewModel; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserViewModel; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java index 6f83527b..44d02068 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FollowedUsersRecyclerViewAdapter.java @@ -16,7 +16,7 @@ import com.bumptech.glide.request.RequestOptions; import java.util.List; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import pl.droidsonroids.gif.GifImageView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/GetCurrentAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/GetCurrentAccountAsyncTask.java index ad740f92..752c596a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/GetCurrentAccountAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/GetCurrentAccountAsyncTask.java @@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import Account.Account; -import Account.AccountDao; +import ml.docilealligator.infinityforreddit.Account.Account; +import ml.docilealligator.infinityforreddit.Account.AccountDao; class GetCurrentAccountAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java index 22b79e85..2cb1e45e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java @@ -6,12 +6,12 @@ import androidx.annotation.Nullable; import java.util.List; -import SubredditDatabase.SubredditDao; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditDao; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserDao; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditDao; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; class InsertSubscribedThingsAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/InsertUserDataAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/InsertUserDataAsyncTask.java index 6db03920..90ea800b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/InsertUserDataAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/InsertUserDataAsyncTask.java @@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import User.UserData; -import User.UserDao; +import ml.docilealligator.infinityforreddit.User.UserData; +import ml.docilealligator.infinityforreddit.User.UserDao; public class InsertUserDataAsyncTask extends AsyncTask { private UserDao userDao; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java index c35c9f3f..0e8f384c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java @@ -4,8 +4,8 @@ import android.os.AsyncTask; import java.util.ArrayList; -import SubredditDatabase.SubredditDao; -import SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import retrofit2.Retrofit; class LoadSubredditIconAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java index 8ef07e73..b9dddf1d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadUserDataAsyncTask.java @@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import User.UserData; -import User.UserDao; +import ml.docilealligator.infinityforreddit.User.UserData; +import ml.docilealligator.infinityforreddit.User.UserDao; import retrofit2.Retrofit; public class LoadUserDataAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 5e3d4c54..dd18b59e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -57,11 +57,11 @@ import java.util.concurrent.TimeUnit; import javax.inject.Inject; import javax.inject.Named; -import Account.Account; -import Account.AccountViewModel; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.Account.Account; +import ml.docilealligator.infinityforreddit.Account.AccountViewModel; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertNewAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertNewAccountAsyncTask.java index f5a99d5d..48f15207 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertNewAccountAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseAndInsertNewAccountAsyncTask.java @@ -2,8 +2,8 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import Account.Account; -import Account.AccountDao; +import ml.docilealligator.infinityforreddit.Account.Account; +import ml.docilealligator.infinityforreddit.Account.AccountDao; class ParseAndInsertNewAccountAsyncTask extends AsyncTask { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index ccded2ec..70079aa5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -8,7 +8,7 @@ import org.json.JSONObject; import java.util.ArrayList; -import SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; class ParseSubredditData { interface ParseSubredditDataListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index b02b646a..cac5242f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -8,9 +8,9 @@ import org.json.JSONObject; import java.util.ArrayList; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; class ParseSubscribedThing { interface ParseSubscribedSubredditsListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java index 8b9e225b..f99fd33a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseUserData.java @@ -8,7 +8,7 @@ import org.json.JSONObject; import java.util.ArrayList; -import User.UserData; +import ml.docilealligator.infinityforreddit.User.UserData; public class ParseUserData { interface ParseUserDataListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 7b8d3fec..f6dc6970 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -38,8 +38,8 @@ import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; -import CustomView.AspectRatioGifImageView; -import User.UserDao; +import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; +import ml.docilealligator.infinityforreddit.User.UserDao; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.BlurTransformation; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index ef0281f5..7fcb5088 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -5,7 +5,6 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; -import android.util.Log; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; @@ -26,7 +25,7 @@ import java.util.Map; import javax.inject.Inject; import javax.inject.Named; -import Account.Account; +import ml.docilealligator.infinityforreddit.Account.Account; import retrofit2.Call; import retrofit2.Response; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 4ba61434..3b65103e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -6,16 +6,16 @@ import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; -import Account.Account; -import Account.AccountDao; -import SubredditDatabase.SubredditDao; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditDao; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserDao; -import SubscribedUserDatabase.SubscribedUserData; -import User.UserDao; -import User.UserData; +import ml.docilealligator.infinityforreddit.Account.Account; +import ml.docilealligator.infinityforreddit.Account.AccountDao; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditDao; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.User.UserDao; +import ml.docilealligator.infinityforreddit.User.UserData; @Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, SubscribedUserData.class}, version = 1) public abstract class RedditDataRoomDatabase extends RoomDatabase { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AboutPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AboutPreferenceFragment.java new file mode 100644 index 00000000..03b904d3 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AboutPreferenceFragment.java @@ -0,0 +1,91 @@ +package ml.docilealligator.infinityforreddit.Settings; + + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.widget.Toast; + +import androidx.preference.Preference; +import androidx.preference.PreferenceFragmentCompat; + +import ml.docilealligator.infinityforreddit.LinkResolverActivity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; + +/** + * A simple {@link PreferenceFragmentCompat} subclass. + */ +public class AboutPreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.about_preferences, rootKey); + + Preference openSourcePreference = findPreference(SharedPreferencesUtils.OPEN_SOURCE_KEY); + Preference reviewPreference = findPreference(SharedPreferencesUtils.RATE_KEY); + Preference emailPreference = findPreference(SharedPreferencesUtils.EMAIL_KEY); + Preference redditAccountPreference = findPreference(SharedPreferencesUtils.REDDIT_ACCOUNT_KEY); + Preference subredditPreference = findPreference(SharedPreferencesUtils.SUBREDDIT_KEY); + + Activity activity = getActivity(); + + if(activity != null) { + if(openSourcePreference != null) { + openSourcePreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://github.com/Docile-Alligator/Infinity-For-Reddit")); + startActivity(intent); + return true; + }); + } + + if(reviewPreference != null) { + reviewPreference.setOnPreferenceClickListener(preference -> { + Intent playStoreIntent = new Intent(Intent.ACTION_VIEW); + playStoreIntent.setData(Uri.parse("market://details?id=ml.docilealligator.infinityforreddit")); + if(playStoreIntent.resolveActivity(activity.getPackageManager()) != null) { + activity.startActivity(playStoreIntent); + } else { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforreddit")); + startActivity(intent); + } + return true; + }); + } + + if(emailPreference != null) { + emailPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(Intent.ACTION_SENDTO); + intent.setData(Uri.parse("mailto:docilealligator.app@gmail.com")); + if(intent.resolveActivity(activity.getPackageManager()) != null) { + startActivity(intent); + } else { + Toast.makeText(activity, R.string.no_email_client, Toast.LENGTH_SHORT).show(); + } + return true; + }); + } + + if(redditAccountPreference != null) { + redditAccountPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.reddit.com/user/Hostilenemy")); + startActivity(intent); + return true; + }); + } + + if(subredditPreference != null) { + subredditPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.reddit.com/r/Infinity_For_Reddit")); + startActivity(intent); + return true; + }); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/Acknowledgement.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/Acknowledgement.java new file mode 100644 index 00000000..9291b53c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/Acknowledgement.java @@ -0,0 +1,27 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.net.Uri; + +class Acknowledgement { + private String name; + private String introduction; + private Uri link; + + Acknowledgement(String name, String introduction, Uri link) { + this.name = name; + this.introduction = introduction; + this.link = link; + } + + public String getName() { + return name; + } + + public String getIntroduction() { + return introduction; + } + + public Uri getLink() { + return link; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementFragment.java new file mode 100644 index 00000000..4f730d85 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementFragment.java @@ -0,0 +1,106 @@ +package ml.docilealligator.infinityforreddit.Settings; + + +import android.app.Activity; +import android.net.Uri; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class AcknowledgementFragment extends Fragment { + + + public AcknowledgementFragment() { + // Required empty public constructor + } + + @BindView(R.id.recycler_view_acknowledgement_fragment) RecyclerView recyclerView; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_acknowledgement, container, false); + ButterKnife.bind(this, rootView); + + Activity activity = getActivity(); + + ArrayList acknowledgements = new ArrayList<>(); + acknowledgements.add(new Acknowledgement("ExoPlayer", + "An application level media player for Android", + Uri.parse("https://github.com/google/ExoPlayer"))); + acknowledgements.add(new Acknowledgement("GestureViews", + "ImageView and FrameLayout with gestures control and position animation", + Uri.parse("https://github.com/alexvasilkov/GestureViews"))); + acknowledgements.add(new Acknowledgement("Glide", + "A fast and efficient open source media management and image loading framework for Android", + Uri.parse("https://github.com/bumptech/glide"))); + acknowledgements.add(new Acknowledgement("Swipe", + "Detects swipe events on Android with listener and RxJava Observable", + Uri.parse("https://github.com/pwittchen/swipe"))); + acknowledgements.add(new Acknowledgement("RxAndroid", + "Android specific bindings for RxJava 2", + Uri.parse("https://github.com/ReactiveX/RxAndroid"))); + acknowledgements.add(new Acknowledgement("RxJava", + "Reactive extensions for the JVM", + Uri.parse("https://github.com/ReactiveX/RxJava"))); + acknowledgements.add(new Acknowledgement("Retrofit", + "Type-safe HTTP client for Android and Java by Square, Inc.", + Uri.parse("https://github.com/square/retrofit"))); + acknowledgements.add(new Acknowledgement("Dagger", + "A fast dependency injector for Java and Android.", + Uri.parse("https://github.com/google/dagger"))); + acknowledgements.add(new Acknowledgement("Butter Knife", + "Field and method binding for Android views", + Uri.parse("https://github.com/JakeWharton/butterknife"))); + acknowledgements.add(new Acknowledgement("Aspect Ratio ImageView", + "A simple imageview which scales the width or height aspect with the given ratio", + Uri.parse("https://github.com/santalu/aspect-ratio-imageview"))); + acknowledgements.add(new Acknowledgement("MaterialLoadingProgressBar", + "A styled ProgressBar", + Uri.parse("https://github.com/lsjwzh/MaterialLoadingProgressBar"))); + acknowledgements.add(new Acknowledgement("Markwon", + "A markdown library for Android", + Uri.parse("https://github.com/noties/Markwon"))); + acknowledgements.add(new Acknowledgement("android-gif-drawable", + "Views and Drawable for animated GIFs in Android.", + Uri.parse("https://github.com/koral--/android-gif-drawable"))); + acknowledgements.add(new Acknowledgement("SimpleSearchView", + "A simple SearchView for Android based on Material Design", + Uri.parse("https://github.com/Ferfalk/SimpleSearchView"))); + acknowledgements.add(new Acknowledgement("EventBus", + "A publish/subscribe event bus for Android and Java", + Uri.parse("https://github.com/greenrobot/EventBus"))); + acknowledgements.add(new Acknowledgement("Customized and Expandable TextView", + "Simple library to change the Textview as rectangle, circle and square shapes", + Uri.parse("https://github.com/Rajagopalr3/CustomizedTextView"))); + acknowledgements.add(new Acknowledgement("Rounded Bottom Sheet", + "Bottom sheet with rounded corners", + Uri.parse("https://github.com/Deishelon/RoundedBottomSheet"))); + acknowledgements.add(new Acknowledgement("Bridge", + "A library for avoiding TransactionTooLargeException during state saving and restoration", + Uri.parse("https://github.com/livefront/bridge"))); + acknowledgements.add(new Acknowledgement("Android-State", + "A utility library for Android to save objects in a Bundle without any boilerplate", + Uri.parse("https://github.com/evernote/android-state"))); + + AcknowledgementRecyclerViewAdapter adapter = new AcknowledgementRecyclerViewAdapter(activity, acknowledgements); + recyclerView.setLayoutManager(new LinearLayoutManager(activity)); + recyclerView.setAdapter(adapter); + + return rootView; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementRecyclerViewAdapter.java new file mode 100644 index 00000000..6c3975ab --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AcknowledgementRecyclerViewAdapter.java @@ -0,0 +1,67 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.content.Context; +import android.content.Intent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.LinkResolverActivity; +import ml.docilealligator.infinityforreddit.R; + +class AcknowledgementRecyclerViewAdapter extends RecyclerView.Adapter { + private ArrayList acknowledgements; + private Context context; + + AcknowledgementRecyclerViewAdapter(Context context, ArrayList acknowledgements) { + this.context = context; + this.acknowledgements = acknowledgements; + } + + @NonNull + @Override + public AcknowledgementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new AcknowledgementViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_acknowledgement, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull AcknowledgementViewHolder holder, int position) { + Acknowledgement acknowledgement = acknowledgements.get(holder.getAdapterPosition()); + if(acknowledgement != null) { + holder.nameTextView.setText(acknowledgement.getName()); + holder.introductionTextView.setText(acknowledgement.getIntroduction()); + holder.itemView.setOnClickListener(view -> { + if(context != null) { + Intent intent = new Intent(context, LinkResolverActivity.class); + intent.setData(acknowledgement.getLink()); + context.startActivity(intent); + } + }); + } + } + + @Override + public int getItemCount() { + return acknowledgements == null ? 0 : acknowledgements.size(); + } + + class AcknowledgementViewHolder extends RecyclerView.ViewHolder { + View itemView; + @BindView(R.id.name_text_view_item_acknowledgement) TextView nameTextView; + @BindView(R.id.introduction_text_view_item_acknowledgement) TextView introductionTextView; + + AcknowledgementViewHolder(@NonNull View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + this.itemView = itemView; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CreditsPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CreditsPreferenceFragment.java new file mode 100644 index 00000000..bbd98d13 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CreditsPreferenceFragment.java @@ -0,0 +1,100 @@ +package ml.docilealligator.infinityforreddit.Settings; + + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; + +import androidx.preference.Preference; +import androidx.preference.PreferenceFragmentCompat; + +import ml.docilealligator.infinityforreddit.LinkResolverActivity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; + +/** + * A simple {@link PreferenceFragmentCompat} subclass. + */ +public class CreditsPreferenceFragment extends PreferenceFragmentCompat { + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.credits_preferences, rootKey); + + Preference iconForegroundPreference = findPreference(SharedPreferencesUtils.ICON_FOREGROUND_KEY); + Preference iconBackgroundPreference = findPreference(SharedPreferencesUtils.ICON_BACKGROUND_KEY); + Preference errorImagePreference = findPreference(SharedPreferencesUtils.ERROR_IMAGE_KEY); + Preference gildedIconPreference = findPreference(SharedPreferencesUtils.GILDED_ICON_KEY); + Preference crosspostIconPreference = findPreference(SharedPreferencesUtils.CROSSPOST_ICON_KEY); + Preference thumbtackIconPreference = findPreference(SharedPreferencesUtils.THUMBTACK_ICON_KEY); + Preference materialIconsPreference = findPreference(SharedPreferencesUtils.MATERIAL_ICONS_KEY); + + Activity activity = getActivity(); + + if(activity != null) { + if(iconForegroundPreference != null) { + iconForegroundPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.freepik.com/free-photos-vectors/technology")); + startActivity(intent); + return true; + }); + } + + if(iconBackgroundPreference != null) { + iconBackgroundPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.freepik.com/free-photos-vectors/background")); + startActivity(intent); + return true; + }); + } + + if(errorImagePreference != null) { + errorImagePreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.freepik.com/free-photos-vectors/technology")); + startActivity(intent); + return true; + }); + } + + if(gildedIconPreference != null) { + gildedIconPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://br.flaticon.com/icone-gratis/medalha_1007239")); + startActivity(intent); + return true; + }); + } + + if(crosspostIconPreference != null) { + crosspostIconPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.flaticon.com/free-icon/crossed-arrows_2291")); + startActivity(intent); + return true; + }); + } + + if(thumbtackIconPreference != null) { + thumbtackIconPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://www.flaticon.com/free-icon/tack-save-button_61845#term=thumbtack&page=1&position=3")); + startActivity(intent); + return true; + }); + } + + if(materialIconsPreference != null) { + materialIconsPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, LinkResolverActivity.class); + intent.setData(Uri.parse("https://material.io/resources/icons/")); + startActivity(intent); + return true; + }); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/FontSizePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/FontSizePreferenceFragment.java new file mode 100644 index 00000000..c1f89842 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/FontSizePreferenceFragment.java @@ -0,0 +1,49 @@ +package ml.docilealligator.infinityforreddit.Settings; + +import android.app.Activity; +import android.os.Bundle; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; + +import org.greenrobot.eventbus.EventBus; + +import ml.docilealligator.infinityforreddit.ChangeFontSizeEvent; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; + +public class FontSizePreferenceFragment extends PreferenceFragmentCompat { + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.font_size_preferences, rootKey); + + Activity activity = getActivity(); + if(activity != null) { + ListPreference fontSizePreference = findPreference(SharedPreferencesUtils.FONT_SIZE_KEY); + ListPreference titleFontSizePreference = findPreference(SharedPreferencesUtils.TITLE_FONT_SIZE_KEY); + ListPreference contentFontSizePreference = findPreference(SharedPreferencesUtils.CONTENT_FONT_SIZE_KEY); + + if(fontSizePreference != null) { + fontSizePreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeFontSizeEvent()); + activity.recreate(); + return true; + }); + } + + if(titleFontSizePreference != null) { + titleFontSizePreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeFontSizeEvent()); + return true; + }); + } + + if(contentFontSizePreference != null) { + contentFontSizePreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeFontSizeEvent()); + return true; + }); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java new file mode 100644 index 00000000..7f72da36 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java @@ -0,0 +1,113 @@ +package ml.docilealligator.infinityforreddit.Settings; + + +import android.app.Activity; +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Bundle; + +import androidx.appcompat.app.AppCompatDelegate; +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; + +import org.greenrobot.eventbus.EventBus; + +import javax.inject.Inject; + +import ml.docilealligator.infinityforreddit.ChangeNSFWBlurEvent; +import ml.docilealligator.infinityforreddit.ChangeNSFWEvent; +import ml.docilealligator.infinityforreddit.ChangeSpoilerBlurEvent; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; + +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; + +/** + * A simple {@link PreferenceFragmentCompat} subclass. + */ +public class MainPreferenceFragment extends PreferenceFragmentCompat { + + @Inject + SharedPreferences sharedPreferences; + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.main_preferences, rootKey); + + Activity activity = getActivity(); + if(activity != null) { + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + + SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); + SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); + SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_KEY); + ListPreference themePreference = findPreference(SharedPreferencesUtils.THEME_KEY); + + if(nsfwSwitch != null) { + nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); + if(blurNSFWSwitch != null) { + blurNSFWSwitch.setVisible((Boolean) newValue); + } + return true; + }); + } + + if(blurNSFWSwitch != null) { + boolean nsfwEnabled = sharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false); + + if(nsfwEnabled) { + blurNSFWSwitch.setVisible(true); + } else { + blurNSFWSwitch.setVisible(false); + } + + blurNSFWSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeNSFWBlurEvent((Boolean) newValue)); + return true; + }); + } + + if(blurSpoilerSwitch != null) { + blurSpoilerSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeSpoilerBlurEvent((Boolean) newValue)); + return true; + }); + } + + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + + if(themePreference != null) { + if(systemDefault) { + themePreference.setEntries(R.array.settings_theme_q); + } else { + themePreference.setEntries(R.array.settings_theme); + } + + themePreference.setOnPreferenceChangeListener((preference, newValue) -> { + int option = Integer.parseInt((String) newValue); + switch (option) { + case 0: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_NO); + break; + case 1: + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_YES); + break; + case 2: + if(systemDefault) { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM); + } else { + AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_AUTO_BATTERY); + } + } + return true; + }); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java new file mode 100644 index 00000000..51d895bd --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/NotificationPreferenceFragment.java @@ -0,0 +1,118 @@ +package ml.docilealligator.infinityforreddit.Settings; + + +import android.app.Activity; +import android.content.SharedPreferences; +import android.os.Bundle; + +import androidx.fragment.app.Fragment; +import androidx.preference.ListPreference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreference; +import androidx.work.Constraints; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.NetworkType; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; + +import java.util.concurrent.TimeUnit; + +import javax.inject.Inject; + +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.PullNotificationWorker; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.SharedPreferencesUtils; + +/** + * A simple {@link Fragment} subclass. + */ +public class NotificationPreferenceFragment extends PreferenceFragmentCompat { + + private boolean enableNotification; + private long notificationInterval; + + private WorkManager workManager; + + @Inject + SharedPreferences sharedPreferences; + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.notification_preferences, rootKey); + + Activity activity = getActivity(); + + if(activity != null) { + workManager = WorkManager.getInstance(activity); + + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + + SwitchPreference enableNotificationSwitchPreference = findPreference(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY); + ListPreference notificationIntervalListPreference = findPreference(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY); + + enableNotification = sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true); + notificationInterval = Long.parseLong(sharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1")); + + if(enableNotification) { + if (notificationIntervalListPreference != null) { + notificationIntervalListPreference.setVisible(true); + } + } + + if(enableNotificationSwitchPreference != null) { + enableNotificationSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { + enableNotification = ((Boolean) newValue); + if(notificationIntervalListPreference != null) { + notificationIntervalListPreference.setVisible(enableNotification); + } + + if(enableNotification) { + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + PeriodicWorkRequest pullNotificationRequest = + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + notificationInterval, TimeUnit.HOURS) + .setConstraints(constraints) + .setInitialDelay(notificationInterval, TimeUnit.HOURS) + .build(); + + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + } else { + workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + } + return true; + }); + } + + if(notificationIntervalListPreference != null) { + notificationIntervalListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + notificationInterval = Long.parseLong((String) newValue); + + if(enableNotification) { + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + PeriodicWorkRequest pullNotificationRequest = + new PeriodicWorkRequest.Builder(PullNotificationWorker.class, + notificationInterval, TimeUnit.HOURS) + .setConstraints(constraints) + .setInitialDelay(notificationInterval, TimeUnit.HOURS) + .build(); + + workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, + ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + } else { + workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG); + } + + return true; + }); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SettingsActivity.java index 4f91157d..cf84d82f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SettingsActivity.java @@ -19,8 +19,8 @@ import androidx.preference.PreferenceFragmentCompat; import javax.inject.Inject; -import Settings.AboutPreferenceFragment; -import Settings.MainPreferenceFragment; +import ml.docilealligator.infinityforreddit.Settings.AboutPreferenceFragment; +import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java new file mode 100644 index 00000000..bb058295 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditDao.java @@ -0,0 +1,22 @@ +package ml.docilealligator.infinityforreddit.SubredditDatabase; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.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 name = :namePrefixed COLLATE NOCASE LIMIT 1") + LiveData getSubredditLiveDataByName(String namePrefixed); + + @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1") + SubredditData getSubredditData(String namePrefixed); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java new file mode 100644 index 00000000..2bca8963 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java @@ -0,0 +1,58 @@ +package ml.docilealligator.infinityforreddit.SubredditDatabase; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "subreddits") +public class SubredditData { + @PrimaryKey + @NonNull + @ColumnInfo(name = "id") + private String id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + @ColumnInfo(name = "banner") + 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/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java new file mode 100644 index 00000000..2978fdca --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditRepository.java @@ -0,0 +1,40 @@ +package ml.docilealligator.infinityforreddit.SubredditDatabase; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubredditRepository { + private SubredditDao mSubredditDao; + private LiveData mSubredditLiveData; + + SubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName) { + mSubredditDao = redditDataRoomDatabase.subredditDao(); + mSubredditLiveData = mSubredditDao.getSubredditLiveDataByName(subredditName); + } + + LiveData getSubredditLiveData() { + return mSubredditLiveData; + } + + public void insert(SubredditData subredditData) { + new 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/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java new file mode 100644 index 00000000..7a5db908 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditViewModel.java @@ -0,0 +1,50 @@ +package ml.docilealligator.infinityforreddit.SubredditDatabase; + +import android.app.Application; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; +import androidx.annotation.NonNull; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubredditViewModel extends AndroidViewModel { + private SubredditRepository mSubredditRepository; + private LiveData mSubredditLiveData; + + public SubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { + super(application); + mSubredditRepository = new SubredditRepository(redditDataRoomDatabase, id); + 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 RedditDataRoomDatabase mRedditDataRoomDatabase; + private final String mSubredditName; + + public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String subredditname) { + mApplication = application; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mSubredditName = subredditname; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + //noinspection unchecked + return (T) new SubredditViewModel(mApplication, mRedditDataRoomDatabase, mSubredditName); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java index c3dd6c1c..aa534e34 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingDataSource.java @@ -6,7 +6,7 @@ import androidx.paging.PageKeyedDataSource; import java.util.ArrayList; -import SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import retrofit2.Retrofit; public class SubredditListingDataSource extends PageKeyedDataSource { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java index b5eb208c..e3669e72 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingRecyclerViewAdapter.java @@ -20,7 +20,7 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; -import SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java index 1412bd22..4c721763 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditListingViewModel.java @@ -9,7 +9,7 @@ import androidx.lifecycle.ViewModelProvider; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; -import SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import retrofit2.Retrofit; public class SubredditListingViewModel extends ViewModel { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 2103994a..16054228 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -31,9 +31,9 @@ import java.util.ArrayList; import javax.inject.Inject; import javax.inject.Named; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import butterknife.BindView; import butterknife.ButterKnife; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java index 2cc01113..349c6aed 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java @@ -7,8 +7,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java new file mode 100644 index 00000000..6caf7703 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java @@ -0,0 +1,27 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.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 WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllSubscribedSubreddits(String accountName); + + @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName AND username = :accountName COLLATE NOCASE LIMIT 1") + SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName); + + @Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName AND username = :accountName") + void deleteSubscribedSubreddit(String subredditName, String accountName); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java new file mode 100644 index 00000000..36128826 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java @@ -0,0 +1,53 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; + +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.room.PrimaryKey; +import androidx.annotation.NonNull; + +import ml.docilealligator.infinityforreddit.Account.Account; + +@Entity(tableName = "subscribed_subreddits", foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username", + childColumns = "username", onDelete = ForeignKey.CASCADE)) +public class SubscribedSubredditData { + + @PrimaryKey + @NonNull + @ColumnInfo(name = "id") + private String id; + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + @ColumnInfo(name = "username") + private String username; + + public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, String username) { + this.id = id; + this.name = name; + this.iconUrl = iconUrl; + this.username = username; + } + + @NonNull + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getUsername() { + return username; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java new file mode 100644 index 00000000..f10f93b8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java @@ -0,0 +1,42 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubscribedSubredditRepository { + private SubscribedSubredditDao mSubscribedSubredditDao; + private LiveData> mAllSubscribedSubreddits; + + SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao(); + mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName); + } + + 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/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java new file mode 100644 index 00000000..c1965641 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java @@ -0,0 +1,50 @@ +package ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubscribedSubredditViewModel extends AndroidViewModel { + private SubscribedSubredditRepository mSubscribedSubredditRepository; + private LiveData> mAllSubscribedSubreddits; + + public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + super(application); + mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName); + mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); + } + + public LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedSubreddits; + } + + public void insert(SubscribedSubredditData subscribedSubredditData) { + mSubscribedSubredditRepository.insert(subscribedSubredditData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Application mApplication; + private RedditDataRoomDatabase mRedditDataRoomDatabase; + private String mAccountName; + + public Factory(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + this.mApplication = application; + this.mRedditDataRoomDatabase = redditDataRoomDatabase; + this.mAccountName = accountName; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + return (T) new SubscribedSubredditViewModel(mApplication, mRedditDataRoomDatabase, mAccountName); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index 150cd509..d5954501 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -23,7 +23,7 @@ import com.bumptech.glide.RequestManager; import javax.inject.Inject; -import SubscribedSubredditDatabase.SubscribedSubredditViewModel; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditViewModel; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java index e3cc97b6..897b811d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java @@ -16,7 +16,7 @@ import com.bumptech.glide.request.RequestOptions; import java.util.List; -import SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import pl.droidsonroids.gif.GifImageView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java index eb569eaa..f45b5f32 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java @@ -31,9 +31,9 @@ import java.util.ArrayList; import javax.inject.Inject; import javax.inject.Named; -import SubredditDatabase.SubredditData; -import SubscribedSubredditDatabase.SubscribedSubredditData; -import SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import butterknife.BindView; import butterknife.ButterKnife; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java new file mode 100644 index 00000000..4143e7c7 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java @@ -0,0 +1,27 @@ +package ml.docilealligator.infinityforreddit.SubscribedUserDatabase; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +@Dao +public interface SubscribedUserDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(SubscribedUserData subscribedUserData); + + @Query("DELETE FROM subscribed_users") + void deleteAllSubscribedUsers(); + + @Query("SELECT * FROM subscribed_users WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllSubscribedUsers(String accountName); + + @Query("SELECT * FROM subscribed_users WHERE name = :name AND username = :accountName COLLATE NOCASE LIMIT 1") + SubscribedUserData getSubscribedUser(String name, String accountName); + + @Query("DELETE FROM subscribed_users WHERE name = :name AND username = :accountName") + void deleteSubscribedUser(String name, String accountName); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java new file mode 100644 index 00000000..0af2416a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java @@ -0,0 +1,45 @@ +package ml.docilealligator.infinityforreddit.SubscribedUserDatabase; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.room.PrimaryKey; + +import ml.docilealligator.infinityforreddit.Account.Account; + +@Entity(tableName = "subscribed_users", foreignKeys = @ForeignKey(entity = Account.class, parentColumns = "username", + childColumns = "username", onDelete = ForeignKey.CASCADE)) +public class SubscribedUserData { + @PrimaryKey + @NonNull + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + @ColumnInfo(name = "username") + private String username; + + public SubscribedUserData(@NonNull String name, String iconUrl, String username) { + this.name = name; + this.iconUrl = iconUrl; + this.username = username; + } + + @NonNull + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getUsername() { + return username; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java new file mode 100644 index 00000000..7d295037 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java @@ -0,0 +1,42 @@ +package ml.docilealligator.infinityforreddit.SubscribedUserDatabase; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubscribedUserRepository { + private SubscribedUserDao mSubscribedUserDao; + private LiveData> mAllSubscribedUsers; + + SubscribedUserRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao(); + mAllSubscribedUsers = mSubscribedUserDao.getAllSubscribedUsers(accountName); + } + + LiveData> getAllSubscribedSubreddits() { + return mAllSubscribedUsers; + } + + public void insert(SubscribedUserData subscribedUserData) { + new SubscribedUserRepository.insertAsyncTask(mSubscribedUserDao).execute(subscribedUserData); + } + + private static class insertAsyncTask extends AsyncTask { + + private SubscribedUserDao mAsyncTaskDao; + + insertAsyncTask(SubscribedUserDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final SubscribedUserData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java new file mode 100644 index 00000000..b659fbc5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java @@ -0,0 +1,50 @@ +package ml.docilealligator.infinityforreddit.SubscribedUserDatabase; + +import android.app.Application; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SubscribedUserViewModel extends AndroidViewModel { + private SubscribedUserRepository mSubscribedUserRepository; + private LiveData> mAllSubscribedUsers; + + public SubscribedUserViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + super(application); + mSubscribedUserRepository = new SubscribedUserRepository(redditDataRoomDatabase, accountName); + mAllSubscribedUsers = mSubscribedUserRepository.getAllSubscribedSubreddits(); + } + + public LiveData> getAllSubscribedUsers() { + return mAllSubscribedUsers; + } + + public void insert(SubscribedUserData subscribedUserData) { + mSubscribedUserRepository.insert(subscribedUserData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Application mApplication; + private RedditDataRoomDatabase mRedditDataRoomDatabase; + private String mAccountName; + + public Factory(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { + mApplication = application; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mAccountName = accountName; + } + + @NonNull + @Override + public T create(@NonNull Class modelClass) { + return (T) new SubscribedUserViewModel(mApplication, mRedditDataRoomDatabase, mAccountName); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java index 516d0b55..65801464 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountAsyncTask.java @@ -2,7 +2,7 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import Account.Account; +import ml.docilealligator.infinityforreddit.Account.Account; class SwitchAccountAsyncTask extends AsyncTask { interface SwitchAccountAsyncTaskListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java index af65d5f8..588131dd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchToAnonymousAccountAsyncTask.java @@ -2,7 +2,7 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import Account.AccountDao; +import ml.docilealligator.infinityforreddit.Account.AccountDao; class SwitchToAnonymousAccountAsyncTask extends AsyncTask { interface SwitchToAnonymousAccountAsyncTaskListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserDao.java new file mode 100644 index 00000000..b7221c26 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserDao.java @@ -0,0 +1,22 @@ +package ml.docilealligator.infinityforreddit.User; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +@Dao +public interface UserDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(UserData userData); + + @Query("DELETE FROM users") + void deleteAllUsers(); + + @Query("SELECT * FROM users WHERE name = :userName COLLATE NOCASE LIMIT 1") + LiveData getUserLiveData(String userName); + + @Query("SELECT * FROM users WHERE name = :userName COLLATE NOCASE LIMIT 1") + UserData getUserData(String userName); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserData.java new file mode 100644 index 00000000..f207685e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserData.java @@ -0,0 +1,65 @@ +package ml.docilealligator.infinityforreddit.User; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "users") +public class UserData { + @PrimaryKey + @NonNull + @ColumnInfo(name = "name") + private String name; + @ColumnInfo(name = "icon") + private String iconUrl; + @ColumnInfo(name = "banner") + private String banner; + @ColumnInfo(name = "karma") + private int karma; + @ColumnInfo(name = "is_gold") + private boolean isGold; + @ColumnInfo(name = "is_friend") + private boolean isFriend; + @ColumnInfo(name = "can_be_followed") + private boolean canBeFollowed; + + public UserData(@NonNull String name, String iconUrl, String banner, int karma, boolean isGold, boolean isFriend, boolean canBeFollowed) { + this.name = name; + this.iconUrl = iconUrl; + this.banner = banner; + this.karma = karma; + this.isGold = isGold; + this.isFriend = isFriend; + this.canBeFollowed = canBeFollowed; + } + + @NonNull + public String getName() { + return name; + } + + public String getIconUrl() { + return iconUrl; + } + + public String getBanner() { + return banner; + } + + public int getKarma() { + return karma; + } + + public boolean isGold() { + return isGold; + } + + public boolean isFriend() { + return isFriend; + } + + public boolean isCanBeFollowed() { + return canBeFollowed; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserRepository.java new file mode 100644 index 00000000..af0b5bb4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserRepository.java @@ -0,0 +1,40 @@ +package ml.docilealligator.infinityforreddit.User; + +import android.os.AsyncTask; + +import androidx.lifecycle.LiveData; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class UserRepository { + private UserDao mUserDao; + private LiveData mUserLiveData; + + UserRepository(RedditDataRoomDatabase redditDataRoomDatabase, String userName) { + mUserDao = redditDataRoomDatabase.userDao(); + mUserLiveData = mUserDao.getUserLiveData(userName); + } + + LiveData getUserLiveData() { + return mUserLiveData; + } + + public void insert(UserData userData) { + new InsertAsyncTask(mUserDao).execute(userData); + } + + private static class InsertAsyncTask extends AsyncTask { + + private UserDao mAsyncTaskDao; + + InsertAsyncTask(UserDao dao) { + mAsyncTaskDao = dao; + } + + @Override + protected Void doInBackground(final UserData... params) { + mAsyncTaskDao.insert(params[0]); + return null; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserViewModel.java new file mode 100644 index 00000000..5f5a51e6 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/User/UserViewModel.java @@ -0,0 +1,49 @@ +package ml.docilealligator.infinityforreddit.User; + +import android.app.Application; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; +import androidx.annotation.NonNull; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class UserViewModel extends AndroidViewModel { + private UserRepository mSubredditRepository; + private LiveData mUserLiveData; + + public UserViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { + super(application); + mSubredditRepository = new UserRepository(redditDataRoomDatabase, id); + mUserLiveData = mSubredditRepository.getUserLiveData(); + } + + public LiveData getUserLiveData() { + return mUserLiveData; + } + + public void insert(UserData userData) { + mSubredditRepository.insert(userData); + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + @NonNull + private final Application mApplication; + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + private final String mUsername; + + public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String username) { + mApplication = application; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mUsername = username; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new UserViewModel(mApplication, mRedditDataRoomDatabase, mUsername); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java index 1b25c415..201e2ce0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java @@ -7,9 +7,9 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import SubscribedUserDatabase.SubscribedUserDao; -import SubscribedUserDatabase.SubscribedUserData; -import User.UserData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; +import ml.docilealligator.infinityforreddit.User.UserData; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java index bcad2452..ac2eb0ae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingDataSource.java @@ -6,7 +6,7 @@ import androidx.paging.PageKeyedDataSource; import java.util.ArrayList; -import User.UserData; +import ml.docilealligator.infinityforreddit.User.UserData; import retrofit2.Retrofit; public class UserListingDataSource extends PageKeyedDataSource { @@ -63,7 +63,7 @@ public class UserListingDataSource extends PageKeyedDataSource @Override public void onFetchUserListingDataFailed() { - initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving User list")); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving ml.docilealligator.infinityforreddit.User list")); } }); } @@ -92,7 +92,7 @@ public class UserListingDataSource extends PageKeyedDataSource @Override public void onFetchUserListingDataFailed() { - paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving User list")); + paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error retrieving ml.docilealligator.infinityforreddit.User list")); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingRecyclerViewAdapter.java index 415a6b41..2a8f53df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingRecyclerViewAdapter.java @@ -21,8 +21,8 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; -import SubscribedUserDatabase.SubscribedUserDao; -import User.UserData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.User.UserData; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java index d25463e5..03c72ec2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserListingViewModel.java @@ -9,7 +9,7 @@ import androidx.lifecycle.ViewModelProvider; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; -import User.UserData; +import ml.docilealligator.infinityforreddit.User.UserData; import retrofit2.Retrofit; public class UserListingViewModel extends ViewModel { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index ba24aae8..24a292a2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -39,9 +39,9 @@ import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; -import SubredditDatabase.SubredditDao; -import SubredditDatabase.SubredditData; -import SubredditDatabase.SubredditViewModel; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index fae233fb..32657fd7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -43,10 +43,10 @@ import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; -import SubscribedUserDatabase.SubscribedUserDao; -import User.UserDao; -import User.UserData; -import User.UserViewModel; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; +import ml.docilealligator.infinityforreddit.User.UserDao; +import ml.docilealligator.infinityforreddit.User.UserData; +import ml.docilealligator.infinityforreddit.User.UserViewModel; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; -- cgit v1.2.3