Edit anonymous multireddit.

This commit is contained in:
Alex Ning 2021-09-28 20:05:47 +08:00
parent 426db4c069
commit 82db567fdc
22 changed files with 111 additions and 59 deletions

View File

@ -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"));
}
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() {
Snackbar.make(coordinatorLayout, R.string.cannot_fetch_multireddit, Snackbar.LENGTH_SHORT).show();
}
});
@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();
}
if (mAccessToken == null) {
EditMultiReddit.anonymousEditMultiReddit(mExecutor, new Handler(), mRedditDataRoomDatabase,
multiReddit, 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();
}
});
@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;
}

View File

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

View File

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

View File

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

View File

@ -505,7 +505,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Kann dieses Multireddit nicht erstellen"</string>
<string name="duplicate_multi_reddit">"Dieses Multireddit existiert bereits"</string>
<string name="edit_multi_reddit_failed">"Kann dieses Multireddit nicht bearbeiten"</string>
<string name="logged_out">"Du bist abgemeldet"</string>
<string name="delete_multi_reddit_success">"Löschen erfolgreich"</string>
<string name="delete_multi_reddit_failed">"Löschen fehlgeschlagen"</string>
<string name="delete_multi_reddit_dialog_message">"Bist du sicher?"</string>

View File

@ -516,7 +516,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Ocurrió un error creando este multireddit"</string>
<string name="duplicate_multi_reddit">"Este multireddit ya existe"</string>
<string name="edit_multi_reddit_failed">"Ocurrió un error al editar el multireddit"</string>
<string name="logged_out">"Sesión cerrada"</string>
<string name="delete_multi_reddit_success">"Borrado satisfactoriamente"</string>
<string name="delete_multi_reddit_failed">"Borrado fallido"</string>
<string name="delete_multi_reddit_dialog_message">"¿Seguro?"</string>

View File

@ -538,7 +538,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Impossible de créer ce multireddit"</string>
<string name="duplicate_multi_reddit">"Ce multireddit existe déjà"</string>
<string name="edit_multi_reddit_failed">"Impossible d'éditer ce multireddit"</string>
<string name="logged_out">"Vous êtes déconnectés"</string>
<string name="delete_multi_reddit_success">"Supprimé avec succès"</string>
<string name="delete_multi_reddit_failed">"Supression échouée"</string>
<string name="delete_multi_reddit_dialog_message">"Êtes vous sûr ?"</string>

View File

@ -528,7 +528,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"इस मल्टीरैडिट का निर्माण असफल "</string>
<string name="duplicate_multi_reddit">"यह मल्टीरैडिट पहले से उपलब्ध है "</string>
<string name="edit_multi_reddit_failed">"इस मल्टीरैडिट का संपादन असफल "</string>
<string name="logged_out">"आप लाॅग्ड आउट हैं"</string>
<string name="delete_multi_reddit_success">"सफलतापूर्वक मिटाई गई "</string>
<string name="delete_multi_reddit_failed">"मिटाना असफल "</string>
<string name="delete_multi_reddit_dialog_message">"क्या आप निश्चित हैं? "</string>

View File

@ -459,7 +459,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Nije moguće stvoriti ovaj multireddit"</string>
<string name="duplicate_multi_reddit">"Ovaj multireddit već postoji"</string>
<string name="edit_multi_reddit_failed">"Nije moguće urediti ovaj multireddit"</string>
<string name="logged_out">"Odjavljeni ste"</string>
<string name="delete_multi_reddit_success">"Brisanje uspješno"</string>
<string name="delete_multi_reddit_failed">"Brisanje nije uspjelo"</string>
<string name="delete_multi_reddit_dialog_message">"Jeste li sigurni?"</string>

View File

@ -483,7 +483,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Nem lehet létrehozni ezt a multiredditet"</string>
<string name="duplicate_multi_reddit">"Ez a multireddit már létezik"</string>
<string name="edit_multi_reddit_failed">"Nem lehet szerkeszteni ezt a multiredditet"</string>
<string name="logged_out">"Kijelentkeztél"</string>
<string name="delete_multi_reddit_success">"Törlés sikeres"</string>
<string name="delete_multi_reddit_failed">"Törlés nem sikerült"</string>
<string name="delete_multi_reddit_dialog_message">"Biztos vagy benne?"</string>

View File

@ -492,7 +492,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Impossibile creare questo multireddit"</string>
<string name="duplicate_multi_reddit">"Il multireddit esiste già"</string>
<string name="edit_multi_reddit_failed">"Impossibile modificare questo multireddit"</string>
<string name="logged_out">"Non sei connesso"</string>
<string name="delete_multi_reddit_success">"Cancellazione riuscita"</string>
<string name="delete_multi_reddit_failed">"Cancellazione fallita"</string>
<string name="delete_multi_reddit_dialog_message">"Sei sicuro?"</string>

View File

@ -530,7 +530,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"このカスタムフィードを作成できませんでした"</string>
<string name="duplicate_multi_reddit">"このカスタムフィードは既に存在しています"</string>
<string name="edit_multi_reddit_failed">"このカスタムフィードを編集できませんでした"</string>
<string name="logged_out">"ログアウト状態です"</string>
<string name="delete_multi_reddit_success">"削除が完了しました"</string>
<string name="delete_multi_reddit_failed">"削除できませんでした"</string>
<string name="delete_multi_reddit_dialog_message">"よろしいですか?"</string>

View File

@ -485,7 +485,6 @@ Tik om opnieuw te proberen."</string>
<string name="create_multi_reddit_failed">"Kan deze multireddit niet maken"</string>
<string name="duplicate_multi_reddit">"Deze multireddit bestaat al"</string>
<string name="edit_multi_reddit_failed">"Kan deze multireddit niet bewerken"</string>
<string name="logged_out">"Je bent uitgelogd"</string>
<string name="delete_multi_reddit_success">"Succesvol verwijderd"</string>
<string name="delete_multi_reddit_failed">"Verwijderen mislukt"</string>
<string name="delete_multi_reddit_dialog_message">"Weet je het zeker?"</string>

View File

@ -484,7 +484,6 @@ Nie można zsynchronizować subskrypcji"</string>
<string name="create_multi_reddit_failed">"Nie można stworzyć tego multireddita"</string>
<string name="duplicate_multi_reddit">"Ten multireddit już istnieje"</string>
<string name="edit_multi_reddit_failed">"Nie można edytować tego multireddita"</string>
<string name="logged_out">"Jesteś wylogowany"</string>
<string name="delete_multi_reddit_success">"Usunięto pomyślnie"</string>
<string name="delete_multi_reddit_failed">"Nie udało się usunąć"</string>
<string name="delete_multi_reddit_dialog_message">"Jesteś pewny?"</string>

View File

@ -491,7 +491,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Não foi possível criar este multireddit"</string>
<string name="duplicate_multi_reddit">"Este multireddit já existe"</string>
<string name="edit_multi_reddit_failed">"Não foi possível editar este multireddit"</string>
<string name="logged_out">"Você está deslogado"</string>
<string name="delete_multi_reddit_success">"Excluído com sucesso"</string>
<string name="delete_multi_reddit_failed">"Falha ao excluir"</string>
<string name="delete_multi_reddit_dialog_message">"Tem certeza?"</string>

View File

@ -484,7 +484,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Não é possível criar este multireddit"</string>
<string name="duplicate_multi_reddit">"Este multireddit já existe"</string>
<string name="edit_multi_reddit_failed">"Não é possível editar este multireddit"</string>
<string name="logged_out">"Não tem sessão iniciada"</string>
<string name="delete_multi_reddit_success">"Eliminado com sucesso"</string>
<string name="delete_multi_reddit_failed">"Falha ao eliminar"</string>
<string name="delete_multi_reddit_dialog_message">"Tem a certeza?"</string>

View File

@ -487,7 +487,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Невозможно создать мультиреддит"</string>
<string name="duplicate_multi_reddit">"Мультиреддит уже существует"</string>
<string name="edit_multi_reddit_failed">"Не удалось изменить мультиреддит"</string>
<string name="logged_out">"Вы вышли из системы"</string>
<string name="delete_multi_reddit_success">"Успешно удалено"</string>
<string name="delete_multi_reddit_failed">"Ошибка удаления"</string>
<string name="delete_multi_reddit_dialog_message">"Вы уверены?"</string>

View File

@ -492,7 +492,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Bu multireddit oluşturulamıyor"</string>
<string name="duplicate_multi_reddit">"Bu multireddit zaten var"</string>
<string name="edit_multi_reddit_failed">"Bu multireddit düzenlenemez"</string>
<string name="logged_out">ıkış yaptınız"</string>
<string name="delete_multi_reddit_success">"Başarıyla silindi"</string>
<string name="delete_multi_reddit_failed">"Silinemedi"</string>
<string name="delete_multi_reddit_dialog_message">"Emin misiniz?"</string>

View File

@ -696,7 +696,6 @@
<string name="create_multi_reddit_failed">Не можливо створити цей мультиредит</string>
<string name="duplicate_multi_reddit">Такий мультиредит уже існує</string>
<string name="edit_multi_reddit_failed">Не можливо змінити цей мультиредит</string>
<string name="logged_out">You are logged out</string>
<string name="delete_multi_reddit_success">Успішно видалено</string>
<string name="delete_multi_reddit_failed">Не вдалося видалити</string>
<string name="delete_multi_reddit_dialog_message">Ви впевнені?</string>

View File

@ -486,7 +486,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"Không thể tạo multireddit này"</string>
<string name="duplicate_multi_reddit">"Multireddit này đã tồn tại"</string>
<string name="edit_multi_reddit_failed">"Không thể chỉnh sửa multireddit này"</string>
<string name="logged_out">"Bạn đã đăng xuất"</string>
<string name="delete_multi_reddit_success">"Xoá thành công"</string>
<string name="delete_multi_reddit_failed">"Xoá thất bại"</string>
<string name="delete_multi_reddit_dialog_message">"Bạn có chắc không?"</string>

View File

@ -491,7 +491,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd
<string name="create_multi_reddit_failed">"无法创建此聚合版块"</string>
<string name="duplicate_multi_reddit">"此聚合版块已存在"</string>
<string name="edit_multi_reddit_failed">"无法编辑此聚合版块"</string>
<string name="logged_out">"您已退出"</string>
<string name="delete_multi_reddit_success">"删除成功"</string>
<string name="delete_multi_reddit_failed">"删除失败"</string>
<string name="delete_multi_reddit_dialog_message">"您确定吗?"</string>

View File

@ -700,7 +700,6 @@
<string name="create_multi_reddit_failed">Cannot create this multireddit</string>
<string name="duplicate_multi_reddit">This multireddit already exists</string>
<string name="edit_multi_reddit_failed">Cannot edit this multireddit</string>
<string name="logged_out">You are logged out</string>
<string name="delete_multi_reddit_success">Delete successfully</string>
<string name="delete_multi_reddit_failed">Delete failed</string>
<string name="delete_multi_reddit_dialog_message">Are you sure?</string>