Deleting multireddit is now available.

This commit is contained in:
Alex Ning 2020-02-18 19:28:35 +08:00
parent bc043c442f
commit a28d022cde
9 changed files with 121 additions and 28 deletions

View File

@ -20,6 +20,7 @@ import androidx.fragment.app.Fragment;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -34,6 +35,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SortTimeBottomSheetFragment
import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MultiReddit.DeleteMultiReddit;
import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit;
import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.Post.PostDataSource;
import ml.docilealligator.infinityforreddit.R; 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: case R.id.action_change_post_layout_view_multi_reddit_detail_activity:
postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag());
return true; 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; return false;
} }

View File

@ -0,0 +1,37 @@
package ml.docilealligator.infinityforreddit.AsyncTask;
import android.os.AsyncTask;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class DeleteMultiredditInDatabaseAsyncTask extends AsyncTask<Void, Void, Void> {
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();
}
}

View File

@ -1,7 +1,5 @@
package ml.docilealligator.infinityforreddit.MultiReddit; package ml.docilealligator.infinityforreddit.MultiReddit;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.HashMap; import java.util.HashMap;
@ -36,7 +34,6 @@ public class CreateMultiReddit {
new ParseMultiReddit.ParseMultiRedditListener() { new ParseMultiReddit.ParseMultiRedditListener() {
@Override @Override
public void success() { public void success() {
Log.i("asfasfas", response.body());
createMultiRedditListener.success(); createMultiRedditListener.success();
} }

View File

@ -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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new DeleteMultiredditInDatabaseAsyncTask(redditDataRoomDatabase, accountName, multipath,
deleteMultiRedditListener::success).execute();
} else {
deleteMultiRedditListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
deleteMultiRedditListener.failed();
}
});
}
}

View File

@ -25,8 +25,8 @@ public interface MultiRedditDao {
@Query("SELECT * FROM multi_reddits WHERE name = :name AND username = :username COLLATE NOCASE LIMIT 1") @Query("SELECT * FROM multi_reddits WHERE name = :name AND username = :username COLLATE NOCASE LIMIT 1")
MultiReddit getMultiReddit(String name, String username); MultiReddit getMultiReddit(String name, String username);
@Query("DELETE FROM multi_reddits WHERE name = :name AND username = :username") @Query("DELETE FROM multi_reddits WHERE path = :multipath AND username = :username")
void deleteMultiReddit(String name, String username); void deleteMultiReddit(String multipath, String username);
@Query("DELETE FROM multi_reddits WHERE username = :username") @Query("DELETE FROM multi_reddits WHERE username = :username")
void deleteAllUserMultiReddits(String username); void deleteAllUserMultiReddits(String username);

View File

@ -81,29 +81,6 @@ public class ParseMultiReddit {
if (!parseFailed) { if (!parseFailed) {
for (int i = 0; i < arrayResponse.length(); i++) { for (int i = 0; i < arrayResponse.length(); i++) {
try { 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<String> 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))); multiReddits.add(parseMultiReddit(arrayResponse.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY)));
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -5,6 +5,7 @@ import java.util.Map;
import okhttp3.MultipartBody; import okhttp3.MultipartBody;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.DELETE;
import retrofit2.http.FieldMap; import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded; import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET; import retrofit2.http.GET;
@ -292,4 +293,7 @@ public interface RedditAPI {
@FormUrlEncoded @FormUrlEncoded
@POST("/api/multi/multipath") @POST("/api/multi/multipath")
Call<String> createMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); Call<String> createMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@DELETE("/api/multi/multipath")
Call<String> deleteMultiReddit(@HeaderMap Map<String, String> headers, @Query("multipath") String multipath);
} }

View File

@ -34,4 +34,10 @@
android:orderInCategory="5" android:orderInCategory="5"
android:title="@string/action_change_post_layout" android:title="@string/action_change_post_layout"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_delte_view_multi_reddit_detail_activity"
android:orderInCategory="6"
android:title="@string/action_delete_multi_reddit"
app:showAsAction="never" />
</menu> </menu>

View File

@ -47,6 +47,7 @@
<string name="action_change_post_layout">Change Post Layout</string> <string name="action_change_post_layout">Change Post Layout</string>
<string name="action_view_side_bar">View Sidebar</string> <string name="action_view_side_bar">View Sidebar</string>
<string name="action_save">Save</string> <string name="action_save">Save</string>
<string name="action_delete_multi_reddit">Delete Multireddit</string>
<string name="parse_json_response_error">Error occurred when parsing the JSON response</string> <string name="parse_json_response_error">Error occurred when parsing the JSON response</string>
<string name="retrieve_token_error">Error Retrieving the token</string> <string name="retrieve_token_error">Error Retrieving the token</string>
@ -440,4 +441,7 @@
<string name="duplicate_multi_reddit">This multireddit already exists</string> <string name="duplicate_multi_reddit">This multireddit already exists</string>
<string name="logged_out">You are logged out</string> <string name="logged_out">You are logged out</string>
<string name="subreddit_selected">%1$s selected</string> <string name="subreddit_selected">%1$s selected</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>
</resources> </resources>