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) {
@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)");
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
@ -33,15 +33,6 @@ public class AnonymousMultiredditSubreddit {
this.path = path;
}
@NonNull
public String getUsername() {
return username;
}
public void setUsername(@NonNull String username) {
this.username = username;
}
@NonNull
public String getSubredditName() {
return subredditName;

View File

@ -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);
}

View File

@ -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);
});
}
}