aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-03-09 15:38:11 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-03-09 15:38:11 +0000
commite43adf966c44f436b35f1445fd75ca6f15523a33 (patch)
treeeb57f8b7c4ff78c3307dbfd466d53faac5834fc9 /app
parent559dbf02d9c0ea8c8336760071a35dc446ac4073 (diff)
downloadinfinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.tar
infinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.tar.gz
infinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.tar.bz2
infinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.tar.lz
infinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.tar.xz
infinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.tar.zst
infinity-for-reddit-e43adf966c44f436b35f1445fd75ca6f15523a33.zip
Anonymous subreddit subscription in SubredditListingRecyclerViewAdapter.
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java45
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java5
4 files changed, 39 insertions, 17 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java
index fcd824a1..721dea6f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java
@@ -16,6 +16,9 @@ public interface AccountDao {
@Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')")
boolean isAnonymousAccountInserted();
+ @Query("INSERT INTO accounts(username, karma, is_current_user) VALUES('-', 0, 0)")
+ void insertAnonymousAccount();
+
@Query("SELECT * FROM accounts WHERE username != '-'")
List<Account> getAllAccounts();
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java
index 9e515c8b..f7891958 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java
@@ -155,21 +155,38 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
public void isNotSubscribed() {
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
- SubredditSubscription.subscribeToSubreddit(executor, new Handler(),
- oauthRetrofit, retrofit, accessToken, subredditData.getName(),
- accountName, redditDataRoomDatabase,
- new SubredditSubscription.SubredditSubscriptionListener() {
- @Override
- public void onSubredditSubscriptionSuccess() {
- ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE);
- Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show();
- }
+ if (accessToken != null) {
+ SubredditSubscription.subscribeToSubreddit(executor, new Handler(),
+ oauthRetrofit, retrofit, accessToken, subredditData.getName(),
+ accountName, redditDataRoomDatabase,
+ new SubredditSubscription.SubredditSubscriptionListener() {
+ @Override
+ public void onSubredditSubscriptionSuccess() {
+ ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE);
+ Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show();
+ }
- @Override
- public void onSubredditSubscriptionFail() {
- Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show();
- }
- });
+ @Override
+ public void onSubredditSubscriptionFail() {
+ Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show();
+ }
+ });
+ } else {
+ SubredditSubscription.anonymousSubscribeToSubreddit(executor, new Handler(),
+ retrofit, redditDataRoomDatabase, subredditData.getName(),
+ new SubredditSubscription.SubredditSubscriptionListener() {
+ @Override
+ public void onSubredditSubscriptionSuccess() {
+ ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE);
+ Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show();
+ }
+
+ @Override
+ public void onSubredditSubscriptionFail() {
+ Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
});
}
});
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java
index fed411cc..d977f159 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java
@@ -13,6 +13,9 @@ public class CheckIsSubscribedToSubreddit {
String subredditName, String accountName,
CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) {
executor.execute(() -> {
+ if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
+ redditDataRoomDatabase.accountDao().insertAnonymousAccount();
+ }
SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName);
handler.post(() -> {
if (subscribedSubredditData != null) {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java
index 296e319b..7302dd6a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java
@@ -9,7 +9,6 @@ import java.util.Map;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
-import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
@@ -130,7 +129,7 @@ public class SubredditSubscription {
subredditData.getIconUrl(), accountName, false);
if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
- redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false));
+ redditDataRoomDatabase.accountDao().insertAnonymousAccount();
}
}
redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
@@ -145,7 +144,7 @@ public class SubredditSubscription {
executor.execute(() -> {
if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
- redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false));
+ redditDataRoomDatabase.accountDao().insertAnonymousAccount();
}
}
redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);