diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-09-26 12:35:03 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-09-26 12:35:03 +0000 |
commit | ad99238ed98d95cdc6c9100d907623aaddf59d2d (patch) | |
tree | 582e50379748091e1bb97bc531e018fa7c6df34b | |
parent | e3457069a425e6bf6bb0a55755c8fe1776ed6ad4 (diff) | |
download | infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.tar infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.tar.gz infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.tar.bz2 infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.tar.lz infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.tar.xz infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.tar.zst infinity-for-reddit-ad99238ed98d95cdc6c9100d907623aaddf59d2d.zip |
Create multireddits in anonymous mode.
Diffstat (limited to '')
6 files changed, 65 insertions, 33 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 6d4bb146..6df86581 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -347,7 +347,8 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { private static final Migration MIGRATION_20_21 = new Migration(20, 21) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { - database.execSQL("CREATE TABLE anonymous_multireddit_subreddits (path TEXT NOT NULL, username TEXT NOT NULL, subreddit_name TEXT NOT NULL, " + + database.execSQL("CREATE TABLE anonymous_multireddit_subreddits (path TEXT NOT NULL, " + + "username TEXT NOT NULL, subreddit_name TEXT NOT NULL, " + "PRIMARY KEY(path, username, subreddit_name), FOREIGN KEY(path) REFERENCES multi_reddits(path) ON DELETE CASCADE ON UPDATE CASCADE, " + "FOREIGN KEY(username) REFERENCES multi_reddits(username) ON DELETE CASCADE ON UPDATE CASCADE)"); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java index c9928e5c..f040b5e6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -11,7 +12,6 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -23,6 +23,7 @@ import com.google.android.material.snackbar.Snackbar; import com.r0adkll.slidr.Slidr; import java.util.ArrayList; +import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; @@ -77,6 +78,8 @@ public class CreateMultiRedditActivity extends BaseActivity { SharedPreferences mCurrentAccountSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; + @Inject + Executor mExecutor; private String mAccessToken; private String mAccountName; private ArrayList<String> mSubreddits; @@ -106,13 +109,7 @@ public class CreateMultiRedditActivity extends BaseActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); - mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); - - if (mAccessToken == null) { - Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show(); - finish(); - return; - } + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "-"); if (savedInstanceState != null) { mSubreddits = savedInstanceState.getStringArrayList(SELECTED_SUBREDDITS_STATE); @@ -139,20 +136,17 @@ public class CreateMultiRedditActivity extends BaseActivity { @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + finish(); + return true; + } else if (itemId == R.id.action_save_create_multi_reddit_activity) { + if (nameEditText.getText() == null || nameEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.no_multi_reddit_name, Snackbar.LENGTH_SHORT).show(); return true; - case R.id.action_save_create_multi_reddit_activity: - if (mAccountName == null || mAccessToken == null) { - Snackbar.make(coordinatorLayout, R.string.something_went_wrong, Snackbar.LENGTH_SHORT).show(); - return true; - } - if (nameEditText.getText() == null || nameEditText.getText().toString().equals("")) { - Snackbar.make(coordinatorLayout, R.string.no_multi_reddit_name, Snackbar.LENGTH_SHORT).show(); - return true; - } + } + if (mAccessToken != null) { String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(), visibilitySwitch.isChecked(), mSubreddits).createJSONModel(); CreateMultiReddit.createMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, @@ -172,6 +166,22 @@ public class CreateMultiRedditActivity extends BaseActivity { } } }); + } else { + CreateMultiReddit.anonymousCreateMultiReddit(mExecutor, new Handler(), mRedditDataRoomDatabase, + "/user/" + mAccountName + "/m/" + nameEditText.getText().toString(), + nameEditText.getText().toString(), descriptionEditText.getText().toString(), + mSubreddits, new CreateMultiReddit.CreateMultiRedditListener() { + @Override + public void success() { + finish(); + } + + @Override + public void failed(int errorType) { + //Will not be called + } + }); + } } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java index e5f0d1e2..9b9b7e1a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -403,7 +403,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti default: { MultiRedditListingFragment fragment = new MultiRedditListingFragment(); Bundle bundle = new Bundle(); - bundle.putString(MultiRedditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(MultiRedditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken == null ? "-" : mAccountName); bundle.putString(MultiRedditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); fragment.setArguments(bundle); return fragment; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubreddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubreddit.java index 069d36f6..1a8e2bf3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubreddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubreddit.java @@ -14,7 +14,7 @@ public class AnonymousMultiredditSubreddit { private String path; @NonNull @ColumnInfo(name = "username") - private String username = "-"; + public String username = "-"; @NonNull @ColumnInfo(name = "subreddit_name") private String subredditName; @@ -34,15 +34,6 @@ public class AnonymousMultiredditSubreddit { } @NonNull - public String getUsername() { - return username; - } - - public void setUsername(@NonNull String username) { - this.username = username; - } - - @NonNull public String getSubredditName() { return subredditName; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java index 8728349c..0117ca46 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java @@ -13,6 +13,9 @@ public interface AnonymousMultiredditSubredditDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(AnonymousMultiredditSubreddit anonymousMultiredditSubreddit); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List<AnonymousMultiredditSubreddit> anonymousMultiredditSubreddits); + @Query("SELECT * FROM anonymous_multireddit_subreddits WHERE path = :path ORDER BY subreddit_name COLLATE NOCASE ASC") LiveData<List<AnonymousMultiredditSubreddit>> getAllAnonymousMultiRedditSubreddits(String path); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java index 186d8574..8eb52a66 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java @@ -1,12 +1,18 @@ package ml.docilealligator.infinityforreddit.multireddit; +import android.os.Handler; + import androidx.annotation.NonNull; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.concurrent.Executor; -import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.account.Account; +import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; @@ -53,4 +59,25 @@ public class CreateMultiReddit { } }); } + + public static void anonymousCreateMultiReddit(Executor executor, Handler handler, + RedditDataRoomDatabase redditDataRoomDatabase, + String multipath, String name, String description, + List<String> subreddits, + CreateMultiRedditListener createMultiRedditListener) { + executor.execute(() -> { + if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { + redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); + } + redditDataRoomDatabase.multiRedditDao().insert(new MultiReddit(multipath, name, name, description, + null, null, null, "-", 0, System.currentTimeMillis(), true, false, false)); + List<AnonymousMultiredditSubreddit> anonymousMultiredditSubreddits = new ArrayList<>(); + for (String s : subreddits) { + anonymousMultiredditSubreddits.add(new AnonymousMultiredditSubreddit(multipath, s)); + } + redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(anonymousMultiredditSubreddits); + + handler.post(createMultiRedditListener::success); + }); + } } |