diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java index 47c0f280..220984c9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java @@ -20,6 +20,7 @@ import androidx.fragment.app.Fragment; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import javax.inject.Inject; import javax.inject.Named; @@ -34,6 +35,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MultiReddit.DeleteMultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; @@ -260,6 +262,31 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT case R.id.action_change_post_layout_view_multi_reddit_detail_activity: postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); return true; + case R.id.action_delte_view_multi_reddit_detail_activity: + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.delete) + .setMessage(R.string.delete_multi_reddit_dialog_message) + .setPositiveButton(R.string.delete, (dialogInterface, i) + -> { + DeleteMultiReddit.deleteMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, + mAccessToken, mAccountName, multiPath, new DeleteMultiReddit.DeleteMultiRedditListener() { + @Override + public void success() { + Toast.makeText(ViewMultiRedditDetailActivity.this, + R.string.delete_multi_reddit_success, Toast.LENGTH_SHORT).show(); + finish(); + } + + @Override + public void failed() { + Toast.makeText(ViewMultiRedditDetailActivity.this, + R.string.delete_multi_reddit_failed, Toast.LENGTH_SHORT).show(); + } + }); + }) + .setNegativeButton(R.string.cancel, null) + .show(); + return true; } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteMultiredditInDatabaseAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteMultiredditInDatabaseAsyncTask.java new file mode 100644 index 00000000..14805c9c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteMultiredditInDatabaseAsyncTask.java @@ -0,0 +1,37 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeleteMultiredditInDatabaseAsyncTask extends AsyncTask { + public interface DeleteMultiredditInDatabaseAsyncTaskListener { + void success(); + } + + private RedditDataRoomDatabase redditDataRoomDatabase; + private String accountName; + private String multipath; + private DeleteMultiredditInDatabaseAsyncTaskListener deleteMultiredditInDatabaseAsyncTaskListener; + + public DeleteMultiredditInDatabaseAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + String accountName, String multipath, + DeleteMultiredditInDatabaseAsyncTaskListener deleteMultiredditInDatabaseAsyncTaskListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.accountName = accountName; + this.multipath = multipath; + this.deleteMultiredditInDatabaseAsyncTaskListener = deleteMultiredditInDatabaseAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + redditDataRoomDatabase.multiRedditDao().deleteMultiReddit(multipath, accountName); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteMultiredditInDatabaseAsyncTaskListener.success(); + } +} 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 ec741466..aa6bf468 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java @@ -1,7 +1,5 @@ package ml.docilealligator.infinityforreddit.MultiReddit; -import android.util.Log; - import androidx.annotation.NonNull; import java.util.HashMap; @@ -36,7 +34,6 @@ public class CreateMultiReddit { new ParseMultiReddit.ParseMultiRedditListener() { @Override public void success() { - Log.i("asfasfas", response.body()); createMultiRedditListener.success(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java new file mode 100644 index 00000000..070f6944 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java @@ -0,0 +1,41 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import androidx.annotation.NonNull; + +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteMultiredditInDatabaseAsyncTask; +import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class DeleteMultiReddit { + public interface DeleteMultiRedditListener { + void success(); + void failed(); + } + + public static void deleteMultiReddit(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, String accountName, String multipath, + DeleteMultiRedditListener deleteMultiRedditListener) { + oauthRetrofit.create(RedditAPI.class).deleteMultiReddit(RedditUtils.getOAuthHeader(accessToken), + multipath).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new DeleteMultiredditInDatabaseAsyncTask(redditDataRoomDatabase, accountName, multipath, + deleteMultiRedditListener::success).execute(); + } else { + deleteMultiRedditListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + deleteMultiRedditListener.failed(); + } + }); + } +} 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 7d3654c8..198e610e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditDao.java @@ -25,8 +25,8 @@ public interface MultiRedditDao { @Query("SELECT * FROM multi_reddits WHERE name = :name AND username = :username COLLATE NOCASE LIMIT 1") MultiReddit getMultiReddit(String name, String username); - @Query("DELETE FROM multi_reddits WHERE name = :name AND username = :username") - void deleteMultiReddit(String name, String username); + @Query("DELETE FROM multi_reddits WHERE path = :multipath AND username = :username") + void deleteMultiReddit(String multipath, String username); @Query("DELETE FROM multi_reddits WHERE username = :username") void deleteAllUserMultiReddits(String username); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java index 63964ecc..6d0d3740 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java @@ -81,29 +81,6 @@ public class ParseMultiReddit { if (!parseFailed) { for (int i = 0; i < arrayResponse.length(); i++) { try { - /*String displayName = singleMultiReddit.getString(JSONUtils.DISPLAY_NAME); - String name = singleMultiReddit.getString(JSONUtils.NAME_KEY); - String description = singleMultiReddit.getString(JSONUtils.DESCRIPTION_HTML_KEY); - int nSubscribers = singleMultiReddit.getInt(JSONUtils.NUM_SUBSCRIBERS_KEY); - String copiedFrom = singleMultiReddit.getString(JSONUtils.COPIED_FROM_KEY); - String iconUrl = singleMultiReddit.getString(JSONUtils.ICON_URL_KEY); - long createdUTC = singleMultiReddit.getLong(JSONUtils.CREATED_UTC_KEY); - String visibility = singleMultiReddit.getString(JSONUtils.VISIBILITY_KEY); - boolean over18 = singleMultiReddit.getBoolean(JSONUtils.OVER_18_KEY); - String path = singleMultiReddit.getString(JSONUtils.PATH_KEY); - String owner = singleMultiReddit.getString(JSONUtils.OWNER_KEY); - boolean isSubscriber = singleMultiReddit.getBoolean(JSONUtils.IS_SUBSCRIBER_KEY); - boolean isFavorited = singleMultiReddit.getBoolean(JSONUtils.IS_FAVORITED_KEY); - - JSONArray subredditsArray = singleMultiReddit.getJSONArray(JSONUtils.SUBREDDITS_KEY); - ArrayList subreddits = new ArrayList<>(); - for (int j = 0; j < subredditsArray.length(); j++) { - subreddits.add(subredditsArray.getJSONObject(j).getString(JSONUtils.NAME_KEY)); - } - - MultiReddit multiReddit = new MultiReddit(path, displayName, name, description, copiedFrom, - iconUrl, visibility, owner, nSubscribers, createdUTC, over18, isSubscriber, - isFavorited, subreddits);*/ multiReddits.add(parseMultiReddit(arrayResponse.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY))); } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 19ca23c4..e25fba92 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -5,6 +5,7 @@ import java.util.Map; import okhttp3.MultipartBody; import okhttp3.RequestBody; import retrofit2.Call; +import retrofit2.http.DELETE; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; @@ -292,4 +293,7 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/multi/multipath") Call createMultiReddit(@HeaderMap Map headers, @FieldMap Map params); + + @DELETE("/api/multi/multipath") + Call deleteMultiReddit(@HeaderMap Map headers, @Query("multipath") String multipath); } diff --git a/app/src/main/res/menu/view_multi_reddit_detail_activity.xml b/app/src/main/res/menu/view_multi_reddit_detail_activity.xml index 0c88dc70..c8005d25 100644 --- a/app/src/main/res/menu/view_multi_reddit_detail_activity.xml +++ b/app/src/main/res/menu/view_multi_reddit_detail_activity.xml @@ -34,4 +34,10 @@ android:orderInCategory="5" android:title="@string/action_change_post_layout" app:showAsAction="never" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9e8351f5..7224c2d2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,7 @@ Change Post Layout View Sidebar Save + Delete Multireddit Error occurred when parsing the JSON response Error Retrieving the token @@ -440,4 +441,7 @@ This multireddit already exists You are logged out %1$s selected + Delete successfully + Delete failed + Are you sure?