aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java112
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java20
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FetchMultiRedditInfo.java20
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java4
4 files changed, 113 insertions, 43 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java
index 87ae213b..46668abe 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java
@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
+import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@@ -13,7 +14,6 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
-import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -24,6 +24,8 @@ import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.snackbar.Snackbar;
import com.r0adkll.slidr.Slidr;
+import java.util.concurrent.Executor;
+
import javax.inject.Inject;
import javax.inject.Named;
@@ -84,6 +86,8 @@ public class EditMultiRedditActivity extends BaseActivity {
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
+ @Inject
+ Executor mExecutor;
private String mAccessToken;
private String mAccountName;
private MultiReddit multiReddit;
@@ -114,12 +118,7 @@ public class EditMultiRedditActivity 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();
- }
+ mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "-");
if (savedInstanceState != null) {
multiReddit = savedInstanceState.getParcelable(MULTI_REDDIT_STATE);
@@ -133,22 +132,41 @@ public class EditMultiRedditActivity extends BaseActivity {
private void bindView() {
if (multiReddit == null) {
- FetchMultiRedditInfo.fetchMultiRedditInfo(mRetrofit, mAccessToken, multipath, new FetchMultiRedditInfo.FetchMultiRedditInfoListener() {
- @Override
- public void success(MultiReddit multiReddit) {
- EditMultiRedditActivity.this.multiReddit = multiReddit;
- progressBar.setVisibility(View.GONE);
- linearLayout.setVisibility(View.VISIBLE);
- nameEditText.setText(multiReddit.getDisplayName());
- descriptionEditText.setText(multiReddit.getDescription());
- visibilitySwitch.setChecked(!multiReddit.getVisibility().equals("public"));
- }
-
- @Override
- public void failed() {
- Snackbar.make(coordinatorLayout, R.string.cannot_fetch_multireddit, Snackbar.LENGTH_SHORT).show();
- }
- });
+ if (mAccessToken == null) {
+ FetchMultiRedditInfo.anonymousFetchMultiRedditInfo(mExecutor, new Handler(),
+ mRedditDataRoomDatabase, multipath, new FetchMultiRedditInfo.FetchMultiRedditInfoListener() {
+ @Override
+ public void success(MultiReddit multiReddit) {
+ EditMultiRedditActivity.this.multiReddit = multiReddit;
+ progressBar.setVisibility(View.GONE);
+ linearLayout.setVisibility(View.VISIBLE);
+ nameEditText.setText(multiReddit.getDisplayName());
+ descriptionEditText.setText(multiReddit.getDescription());
+ }
+
+ @Override
+ public void failed() {
+ //Will not be called
+ }
+ });
+ } else {
+ FetchMultiRedditInfo.fetchMultiRedditInfo(mRetrofit, mAccessToken, multipath, new FetchMultiRedditInfo.FetchMultiRedditInfoListener() {
+ @Override
+ public void success(MultiReddit multiReddit) {
+ EditMultiRedditActivity.this.multiReddit = multiReddit;
+ progressBar.setVisibility(View.GONE);
+ linearLayout.setVisibility(View.VISIBLE);
+ nameEditText.setText(multiReddit.getDisplayName());
+ descriptionEditText.setText(multiReddit.getDescription());
+ visibilitySwitch.setChecked(!multiReddit.getVisibility().equals("public"));
+ }
+
+ @Override
+ public void failed() {
+ Snackbar.make(coordinatorLayout, R.string.cannot_fetch_multireddit, Snackbar.LENGTH_SHORT).show();
+ }
+ });
+ }
} else {
progressBar.setVisibility(View.GONE);
linearLayout.setVisibility(View.VISIBLE);
@@ -180,29 +198,41 @@ public class EditMultiRedditActivity extends BaseActivity {
finish();
return true;
} else if (itemId == R.id.action_save_edit_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;
}
- String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(),
- visibilitySwitch.isChecked(), multiReddit.getSubreddits()).createJSONModel();
- EditMultiReddit.editMultiReddit(mRetrofit, mAccessToken, multiReddit.getPath(),
- jsonModel, new EditMultiReddit.EditMultiRedditListener() {
- @Override
- public void success() {
- finish();
- }
-
- @Override
- public void failed() {
- Snackbar.make(coordinatorLayout, R.string.edit_multi_reddit_failed, Snackbar.LENGTH_SHORT).show();
- }
- });
+ if (mAccessToken == null) {
+ EditMultiReddit.anonymousEditMultiReddit(mExecutor, new Handler(), mRedditDataRoomDatabase,
+ multiReddit, new EditMultiReddit.EditMultiRedditListener() {
+ @Override
+ public void success() {
+ finish();
+ }
+
+ @Override
+ public void failed() {
+ //Will not be called
+ }
+ });
+ } else {
+ String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(),
+ visibilitySwitch.isChecked(), multiReddit.getSubreddits()).createJSONModel();
+ EditMultiReddit.editMultiReddit(mRetrofit, mAccessToken, multiReddit.getPath(),
+ jsonModel, new EditMultiReddit.EditMultiRedditListener() {
+ @Override
+ public void success() {
+ finish();
+ }
+
+ @Override
+ public void failed() {
+ Snackbar.make(coordinatorLayout, R.string.edit_multi_reddit_failed, Snackbar.LENGTH_SHORT).show();
+ }
+ });
+ }
+ return true;
}
return false;
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java
index 36303b90..6efa6487 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java
@@ -1,10 +1,15 @@
package ml.docilealligator.infinityforreddit.multireddit;
+import android.os.Handler;
+
import androidx.annotation.NonNull;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.Executor;
+import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import retrofit2.Call;
@@ -40,4 +45,19 @@ public class EditMultiReddit {
}
});
}
+
+ public static void anonymousEditMultiReddit(Executor executor, Handler handler,
+ RedditDataRoomDatabase redditDataRoomDatabase,
+ MultiReddit multiReddit,
+ EditMultiRedditListener editMultiRedditListener) {
+ executor.execute(() -> {
+ redditDataRoomDatabase.multiRedditDao().insert(multiReddit);
+ ArrayList<AnonymousMultiredditSubreddit> anonymousMultiredditSubreddits = new ArrayList<>();
+ for (String s : multiReddit.getSubreddits()) {
+ anonymousMultiredditSubreddits.add(new AnonymousMultiredditSubreddit(multiReddit.getPath(), s));
+ }
+ redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(anonymousMultiredditSubreddits);
+ handler.post(editMultiRedditListener::success);
+ });
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FetchMultiRedditInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FetchMultiRedditInfo.java
index 97d2a2d1..f867d530 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FetchMultiRedditInfo.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FetchMultiRedditInfo.java
@@ -1,6 +1,7 @@
package ml.docilealligator.infinityforreddit.multireddit;
import android.os.AsyncTask;
+import android.os.Handler;
import androidx.annotation.NonNull;
@@ -9,7 +10,9 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
+import java.util.concurrent.Executor;
+import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
@@ -44,6 +47,23 @@ public class FetchMultiRedditInfo {
});
}
+ public static void anonymousFetchMultiRedditInfo(Executor executor, Handler handler,
+ RedditDataRoomDatabase redditDataRoomDatabase,
+ String multipath,
+ FetchMultiRedditInfoListener fetchMultiRedditInfoListener) {
+ executor.execute(() -> {
+ MultiReddit multiReddit = redditDataRoomDatabase.multiRedditDao().getMultiReddit(multipath, "-");
+ ArrayList<AnonymousMultiredditSubreddit> anonymousMultiredditSubreddits =
+ (ArrayList<AnonymousMultiredditSubreddit>) redditDataRoomDatabase.anonymousMultiredditSubredditDao().getAllAnonymousMultiRedditSubreddits(multipath);
+ ArrayList<String> subredditNames = new ArrayList<>();
+ for (AnonymousMultiredditSubreddit a : anonymousMultiredditSubreddits) {
+ subredditNames.add(a.getSubredditName());
+ }
+ multiReddit.setSubreddits(subredditNames);
+ handler.post(() -> fetchMultiRedditInfoListener.success(multiReddit));
+ });
+ }
+
private static class ParseMultiRedditInfoAsyncTask extends AsyncTask<Void, Void, Void> {
private String response;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java
index 69721319..a5099e85 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java
@@ -22,8 +22,8 @@ public interface MultiRedditDao {
@Query("SELECT * FROM multi_reddits WHERE username = :username AND is_favorite ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllFavoriteMultiReddits(String username);
- @Query("SELECT * FROM multi_reddits WHERE name = :name AND username = :username COLLATE NOCASE LIMIT 1")
- MultiReddit getMultiReddit(String name, String username);
+ @Query("SELECT * FROM multi_reddits WHERE path = :path AND username = :username COLLATE NOCASE LIMIT 1")
+ MultiReddit getMultiReddit(String path, String username);
@Query("DELETE FROM multi_reddits WHERE name = :name AND username = :username")
void deleteMultiReddit(String name, String username);