Create multireddits in anonymous mode.

This commit is contained in:
Alex Ning 2021-09-26 20:35:03 +08:00
parent e3457069a4
commit ad99238ed9
6 changed files with 65 additions and 33 deletions

View File

@ -347,7 +347,8 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
private static final Migration MIGRATION_20_21 = new Migration(20, 21) { private static final Migration MIGRATION_20_21 = new Migration(20, 21) {
@Override @Override
public void migrate(@NonNull SupportSQLiteDatabase database) { 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, " + "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)"); "FOREIGN KEY(username) REFERENCES multi_reddits(username) ON DELETE CASCADE ON UPDATE CASCADE)");
} }

View File

@ -4,6 +4,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -11,7 +12,6 @@ import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Switch; import android.widget.Switch;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -23,6 +23,7 @@ import com.google.android.material.snackbar.Snackbar;
import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.Slidr;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Executor;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -77,6 +78,8 @@ public class CreateMultiRedditActivity extends BaseActivity {
SharedPreferences mCurrentAccountSharedPreferences; SharedPreferences mCurrentAccountSharedPreferences;
@Inject @Inject
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private String mAccessToken; private String mAccessToken;
private String mAccountName; private String mAccountName;
private ArrayList<String> mSubreddits; private ArrayList<String> mSubreddits;
@ -106,13 +109,7 @@ public class CreateMultiRedditActivity extends BaseActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "-");
if (mAccessToken == null) {
Toast.makeText(this, R.string.logged_out, Toast.LENGTH_SHORT).show();
finish();
return;
}
if (savedInstanceState != null) { if (savedInstanceState != null) {
mSubreddits = savedInstanceState.getStringArrayList(SELECTED_SUBREDDITS_STATE); mSubreddits = savedInstanceState.getStringArrayList(SELECTED_SUBREDDITS_STATE);
@ -139,20 +136,17 @@ public class CreateMultiRedditActivity extends BaseActivity {
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { int itemId = item.getItemId();
case android.R.id.home: if (itemId == android.R.id.home) {
finish(); finish();
return true; return true;
case R.id.action_save_create_multi_reddit_activity: } else if (itemId == 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("")) { if (nameEditText.getText() == null || nameEditText.getText().toString().equals("")) {
Snackbar.make(coordinatorLayout, R.string.no_multi_reddit_name, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.no_multi_reddit_name, Snackbar.LENGTH_SHORT).show();
return true; return true;
} }
if (mAccessToken != null) {
String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(), String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(),
visibilitySwitch.isChecked(), mSubreddits).createJSONModel(); visibilitySwitch.isChecked(), mSubreddits).createJSONModel();
CreateMultiReddit.createMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, 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; return false;
} }

View File

@ -403,7 +403,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
default: { default: {
MultiRedditListingFragment fragment = new MultiRedditListingFragment(); MultiRedditListingFragment fragment = new MultiRedditListingFragment();
Bundle bundle = new Bundle(); 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); bundle.putString(MultiRedditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName);
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;

View File

@ -14,7 +14,7 @@ public class AnonymousMultiredditSubreddit {
private String path; private String path;
@NonNull @NonNull
@ColumnInfo(name = "username") @ColumnInfo(name = "username")
private String username = "-"; public String username = "-";
@NonNull @NonNull
@ColumnInfo(name = "subreddit_name") @ColumnInfo(name = "subreddit_name")
private String subredditName; private String subredditName;
@ -33,15 +33,6 @@ public class AnonymousMultiredditSubreddit {
this.path = path; this.path = path;
} }
@NonNull
public String getUsername() {
return username;
}
public void setUsername(@NonNull String username) {
this.username = username;
}
@NonNull @NonNull
public String getSubredditName() { public String getSubredditName() {
return subredditName; return subredditName;

View File

@ -13,6 +13,9 @@ public interface AnonymousMultiredditSubredditDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(AnonymousMultiredditSubreddit anonymousMultiredditSubreddit); 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") @Query("SELECT * FROM anonymous_multireddit_subreddits WHERE path = :path ORDER BY subreddit_name COLLATE NOCASE ASC")
LiveData<List<AnonymousMultiredditSubreddit>> getAllAnonymousMultiRedditSubreddits(String path); LiveData<List<AnonymousMultiredditSubreddit>> getAllAnonymousMultiRedditSubreddits(String path);
} }

View File

@ -1,12 +1,18 @@
package ml.docilealligator.infinityforreddit.multireddit; package ml.docilealligator.infinityforreddit.multireddit;
import android.os.Handler;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; 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);
});
}
} }