From 805ce260a26a9c66712a21597ecf99a76c07c585 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 12 Feb 2020 18:11:03 +0800 Subject: Creating multireddit without specifying subreddits is available. --- app/build.gradle | 7 +- app/src/main/AndroidManifest.xml | 12 +- .../Activity/CreateMultiRedditActivity.java | 153 ++++++++++++++++++++ .../Activity/MultiRedditListingActivity.java | 24 ++- .../Activity/ViewMultiRedditDetailActivity.java | 2 +- .../infinityforreddit/AppComponent.java | 3 + .../MultiReddit/CreateMultiReddit.java | 59 ++++++++ .../MultiReddit/FetchMultiReddit.java | 48 ++++++ .../MultiReddit/GetMultiReddit.java | 128 ---------------- .../MultiReddit/MultiRedditJSONModel.java | 47 ++++++ .../MultiReddit/ParseMultiReddit.java | 161 +++++++++++++++++++++ .../infinityforreddit/RedditAPI.java | 4 + .../infinityforreddit/Utils/RedditUtils.java | 1 + .../res/drawable-night/ic_add_menu_item_24dp.xml | 9 ++ .../main/res/drawable/ic_add_menu_item_24dp.xml | 9 ++ .../res/layout/activity_create_multi_reddit.xml | 99 +++++++++++++ .../res/layout/activity_multi_reddit_listing.xml | 10 ++ .../main/res/menu/create_multi_reddit_activity.xml | 10 ++ app/src/main/res/values/strings.xml | 13 +- 19 files changed, 660 insertions(+), 139 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiReddit.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/GetMultiReddit.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java create mode 100644 app/src/main/res/drawable-night/ic_add_menu_item_24dp.xml create mode 100644 app/src/main/res/drawable/ic_add_menu_item_24dp.xml create mode 100644 app/src/main/res/layout/activity_create_multi_reddit.xml create mode 100644 app/src/main/res/menu/create_multi_reddit_activity.xml (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 2ee33eb3..b474a42c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,7 +42,7 @@ dependencies { androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.google.android.exoplayer:exoplayer:2.10.4' - implementation 'androidx.browser:browser:1.0.0' + implementation 'androidx.browser:browser:1.2.0' implementation 'com.alexvasilkov:gesture-views:2.5.2' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.github.bumptech.glide:glide:4.10.0' @@ -64,7 +64,7 @@ dependencies { implementation 'com.jakewharton:butterknife:10.2.0' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0' implementation 'com.github.santalu:aspect-ratio-imageview:1.0.6' - implementation 'androidx.paging:paging-runtime:2.1.0' + implementation 'androidx.paging:paging-runtime:2.1.1' implementation 'com.lsjwzh:materialloadingprogressbar:0.5.8-RELEASE' implementation 'io.noties.markwon:core:4.2.0' implementation 'io.noties.markwon:linkify:4.2.0' @@ -79,9 +79,10 @@ dependencies { implementation 'com.github.livefront:bridge:v1.2.0' implementation 'com.evernote:android-state:1.4.1' annotationProcessor 'com.evernote:android-state-processor:1.4.1' - implementation 'androidx.work:work-runtime:2.2.0' + implementation 'androidx.work:work-runtime:2.3.0' implementation 'androidx.preference:preference:1.1.0' implementation 'com.nex3z:flow-layout:1.3.0' implementation 'com.r0adkll:slidableactivity:2.1.0' implementation 'com.atlassian.commonmark:commonmark:0.13.1' + implementation 'com.google.code.gson:gson:2.8.6' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cc71e66d..3d9e1a53 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,7 +21,13 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> - + diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java new file mode 100644 index 00000000..31d85a0a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CreateMultiRedditActivity.java @@ -0,0 +1,153 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.Switch; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.google.android.material.snackbar.Snackbar; + +import java.util.ArrayList; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.MultiReddit.CreateMultiReddit; +import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditJSONModel; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import retrofit2.Retrofit; + +public class CreateMultiRedditActivity extends BaseActivity { + + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; + private static final String ACCOUNT_NAME_STATE = "ANS"; + @BindView(R.id.coordinator_layout_create_multi_reddit_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.toolbar_create_multi_reddit_activity) + Toolbar toolbar; + @BindView(R.id.multi_reddit_name_edit_text_create_multi_reddit_activity) + EditText nameEditText; + @BindView(R.id.description_edit_text_create_multi_reddit_activity) + EditText descriptionEditText; + @BindView(R.id.visibility_wrapper_linear_layout_create_multi_reddit_activity) + LinearLayout visibilityLinearLayout; + @BindView(R.id.visibility_switch_create_multi_reddit_activity) + Switch visibilitySwitch; + @BindView(R.id.select_subreddit_text_view_create_multi_reddit_activity) + TextView selectSubredditTextView; + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + SharedPreferences mSharedPreferences; + private boolean mNullAccessToken = false; + private String mAccessToken; + private String mAccountName; + private ArrayList subredditsName; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_create_multi_reddit); + + ButterKnife.bind(this); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + getCurrentAccountAndBindView(); + } + + private void getCurrentAccountAndBindView() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if (account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + mAccountName = account.getUsername(); + } + bindView(); + }).execute(); + } + + private void bindView() { + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.create_multi_reddit_activity, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_create_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; + } + subredditsName = new ArrayList<>(); + /*subredditsName.add("funny"); + subredditsName.add("Infinity_For_Reddit");*/ + String jsonModel = new MultiRedditJSONModel(nameEditText.getText().toString(), descriptionEditText.getText().toString(), + visibilitySwitch.isChecked(), subredditsName).createJSONModel(); + CreateMultiReddit.createMultiReddit(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + "/user/" + mAccountName + "/m/" + nameEditText.getText().toString(), + jsonModel, new CreateMultiReddit.CreateMultiRedditListener() { + @Override + public void success() { + finish(); + } + + @Override + public void failed(int errorCode) { + if (errorCode == 409) { + Snackbar.make(coordinatorLayout, R.string.duplicate_multi_reddit, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, R.string.create_multi_reddit_failed, Snackbar.LENGTH_SHORT).show(); + } + } + }); + } + return false; + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + outState.putString(ACCOUNT_NAME_STATE, mAccountName); + } + + @Override + public SharedPreferences getSharedPreferences() { + return mSharedPreferences; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java index 7acda3c9..5ac8e9de 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MultiRedditListingActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; @@ -26,6 +27,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import java.util.ArrayList; @@ -39,7 +41,7 @@ import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.InsertMultiRedditAsyncTask; import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.MultiReddit.GetMultiReddit; +import ml.docilealligator.infinityforreddit.MultiReddit.FetchMultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiReddit; import ml.docilealligator.infinityforreddit.MultiReddit.MultiRedditViewModel; import ml.docilealligator.infinityforreddit.R; @@ -68,6 +70,8 @@ public class MultiRedditListingActivity extends BaseActivity { ImageView mErrorImageView; @BindView(R.id.fetch_multi_reddit_listing_info_text_view_multi_reddit_listing_activity) TextView mErrorTextView; + @BindView(R.id.fab_multi_reddit_listing_activity) + FloatingActionButton fab; @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @@ -181,10 +185,26 @@ public class MultiRedditListingActivity extends BaseActivity { mGlide = Glide.with(this); + fab.setOnClickListener(view -> { + Intent intent = new Intent(MultiRedditListingActivity.this, CreateMultiRedditActivity.class); + startActivity(intent); + }); + mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); MultiRedditListingRecyclerViewAdapter adapter = new MultiRedditListingRecyclerViewAdapter(this, mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, mAccountName); mRecyclerView.setAdapter(adapter); + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (dy > 0) { + fab.hide(); + } else { + fab.show(); + } + } + }); mMultiRedditViewModel = new ViewModelProvider(this, new MultiRedditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)) @@ -215,7 +235,7 @@ public class MultiRedditListingActivity extends BaseActivity { private void loadMultiReddits() { mSwipeRefreshLayout.setRefreshing(true); - GetMultiReddit.getMyMultiReddits(mOauthRetrofit, mAccessToken, new GetMultiReddit.GetMultiRedditListener() { + FetchMultiReddit.fetchMyMultiReddits(mOauthRetrofit, mAccessToken, new FetchMultiReddit.FetchMultiRedditListener() { @Override public void success(ArrayList multiReddits) { new InsertMultiRedditAsyncTask(mRedditDataRoomDatabase, multiReddits, mAccountName, () -> { 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 b5bca5b5..47c0f280 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewMultiRedditDetailActivity.java @@ -134,7 +134,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT MultiReddit multiReddit = getIntent().getParcelableExtra(EXTRA_MULTIREDDIT_DATA); if (multiReddit == null) { - Toast.makeText(this, R.string.multi_reddit_listing_activity, Toast.LENGTH_SHORT).show(); + Toast.makeText(this, R.string.multi_reddit_listing_activity_label, Toast.LENGTH_SHORT).show(); finish(); return; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index e9a8adb2..124f1d64 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -6,6 +6,7 @@ import dagger.Component; import ml.docilealligator.infinityforreddit.Activity.AccountPostsActivity; import ml.docilealligator.infinityforreddit.Activity.AccountSavedThingActivity; import ml.docilealligator.infinityforreddit.Activity.CommentActivity; +import ml.docilealligator.infinityforreddit.Activity.CreateMultiRedditActivity; import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity; import ml.docilealligator.infinityforreddit.Activity.EditPostActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; @@ -131,4 +132,6 @@ public interface AppComponent { void inject(ViewVideoActivity viewVideoActivity); void inject(GesturesAndButtonsPreferenceFragment gesturesAndButtonsPreferenceFragment); + + void inject(CreateMultiRedditActivity createMultiRedditActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java new file mode 100644 index 00000000..ec741466 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java @@ -0,0 +1,59 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import android.util.Log; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +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 CreateMultiReddit { + public interface CreateMultiRedditListener { + void success(); + void failed(int errorType); + } + + public static void createMultiReddit(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, String multipath, String model, + CreateMultiRedditListener createMultiRedditListener) { + Map params = new HashMap<>(); + params.put(RedditUtils.MULTIPATH_KEY, multipath); + params.put(RedditUtils.MODEL_KEY, model); + oauthRetrofit.create(RedditAPI.class).createMultiReddit(RedditUtils.getOAuthHeader(accessToken), + params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ParseMultiReddit.parseAndSaveMultiReddit(response.body(), redditDataRoomDatabase, + new ParseMultiReddit.ParseMultiRedditListener() { + @Override + public void success() { + Log.i("asfasfas", response.body()); + createMultiRedditListener.success(); + } + + @Override + public void failed() { + createMultiRedditListener.failed(1); + } + }); + } else { + createMultiRedditListener.failed(response.code()); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + createMultiRedditListener.failed(0); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiReddit.java new file mode 100644 index 00000000..16f36098 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiReddit.java @@ -0,0 +1,48 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import androidx.annotation.NonNull; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchMultiReddit { + public interface FetchMultiRedditListener { + void success(ArrayList multiReddits); + void failed(); + } + + public static void fetchMyMultiReddits(Retrofit oauthRetrofit, String accessToken, FetchMultiRedditListener fetchMultiRedditListener) { + oauthRetrofit.create(RedditAPI.class) + .getMyMultiReddits(RedditUtils.getOAuthHeader(accessToken)).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ParseMultiReddit.parseMultiRedditsList(response.body(), new ParseMultiReddit.ParseMultiRedditsListListener() { + @Override + public void success(ArrayList multiReddits) { + fetchMultiRedditListener.success(multiReddits); + } + + @Override + public void failed() { + fetchMultiRedditListener.failed(); + } + }); + } else { + fetchMultiRedditListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchMultiRedditListener.failed(); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/GetMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/GetMultiReddit.java deleted file mode 100644 index bf90d4ab..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/GetMultiReddit.java +++ /dev/null @@ -1,128 +0,0 @@ -package ml.docilealligator.infinityforreddit.MultiReddit; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -public class GetMultiReddit { - public interface GetMultiRedditListener { - void success(ArrayList multiReddits); - void failed(); - } - - public static void getMyMultiReddits(Retrofit oauthRetrofit, String accessToken, GetMultiRedditListener getMultiRedditListener) { - oauthRetrofit.create(RedditAPI.class) - .getMyMultiReddits(RedditUtils.getOAuthHeader(accessToken)).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - new ParseMultiRedditsListAsyncTask(response.body(), new ParseMultiRedditsListAsyncTask.ParseMultiRedditsListAsyncTaskListener() { - @Override - public void success(ArrayList multiReddits) { - getMultiRedditListener.success(multiReddits); - } - - @Override - public void failed() { - getMultiRedditListener.failed(); - } - }).execute(); - } else { - getMultiRedditListener.failed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - getMultiRedditListener.failed(); - } - }); - } - - private static class ParseMultiRedditsListAsyncTask extends AsyncTask { - private JSONArray arrayResponse; - private boolean parseFailed; - private ArrayList multiReddits; - private ParseMultiRedditsListAsyncTaskListener parseMultiRedditsListAsyncTaskListener; - - interface ParseMultiRedditsListAsyncTaskListener { - void success(ArrayList multiReddits); - void failed(); - } - - ParseMultiRedditsListAsyncTask(String response, - ParseMultiRedditsListAsyncTaskListener parseMultiRedditsListAsyncTaskListener) { - this.parseMultiRedditsListAsyncTaskListener = parseMultiRedditsListAsyncTaskListener; - try { - arrayResponse = new JSONArray(response); - multiReddits = new ArrayList<>(); - parseFailed = false; - } catch (JSONException e) { - e.printStackTrace(); - parseFailed = true; - } - } - - @Override - protected Void doInBackground(Void... voids) { - if (!parseFailed) { - for (int i = 0; i < arrayResponse.length(); i++) { - try { - JSONObject singleMultiReddit = arrayResponse.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - 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(multiReddit); - } catch (JSONException e) { - e.printStackTrace(); - } - } - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - if (!parseFailed) { - parseMultiRedditsListAsyncTaskListener.success(multiReddits); - } else { - parseMultiRedditsListAsyncTaskListener.failed(); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java new file mode 100644 index 00000000..812319e2 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/MultiRedditJSONModel.java @@ -0,0 +1,47 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import com.google.gson.Gson; + +import java.util.ArrayList; + +public class MultiRedditJSONModel { + private String display_name; + private String description_md; + private String visibility; + private SubredditInMultiReddit[] subreddits; + + public MultiRedditJSONModel() {} + + public MultiRedditJSONModel(String display_name, String description_md, boolean isPrivate, ArrayList subreddits) { + this.display_name = display_name; + this.description_md = description_md; + if (isPrivate) { + visibility = "private"; + } else { + visibility = "public"; + } + + if (subreddits != null) { + this.subreddits = new SubredditInMultiReddit[subreddits.size()]; + for (int i = 0; i < subreddits.size(); i++) { + SubredditInMultiReddit subredditInMultiReddit = new SubredditInMultiReddit(subreddits.get(i)); + this.subreddits[i] = subredditInMultiReddit; + } + } + } + + public String createJSONModel() { + Gson gson = new Gson(); + return gson.toJson(this); + } + + class SubredditInMultiReddit { + String name; + + SubredditInMultiReddit() {} + + SubredditInMultiReddit(String subredditName) { + name = subredditName; + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java new file mode 100644 index 00000000..63964ecc --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/ParseMultiReddit.java @@ -0,0 +1,161 @@ +package ml.docilealligator.infinityforreddit.MultiReddit; + +import android.os.AsyncTask; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; + +public class ParseMultiReddit { + interface ParseMultiRedditsListListener { + void success(ArrayList multiReddits); + void failed(); + } + + interface ParseMultiRedditListener { + void success(); + void failed(); + } + + public static void parseMultiRedditsList(String response, ParseMultiRedditsListListener parseMultiRedditsListListener) { + new ParseMultiRedditsListAsyncTask(response, parseMultiRedditsListListener).execute(); + } + + public static void parseAndSaveMultiReddit(String response, RedditDataRoomDatabase redditDataRoomDatabase, + ParseMultiRedditListener parseMultiRedditListener) { + new ParseAndSaveMultiRedditAsyncTask(response, redditDataRoomDatabase, parseMultiRedditListener).execute(); + } + + private static MultiReddit parseMultiReddit(JSONObject singleMultiRedditJSON) throws JSONException { + String displayName = singleMultiRedditJSON.getString(JSONUtils.DISPLAY_NAME); + String name = singleMultiRedditJSON.getString(JSONUtils.NAME_KEY); + String description = singleMultiRedditJSON.getString(JSONUtils.DESCRIPTION_HTML_KEY); + int nSubscribers = singleMultiRedditJSON.getInt(JSONUtils.NUM_SUBSCRIBERS_KEY); + String copiedFrom = singleMultiRedditJSON.getString(JSONUtils.COPIED_FROM_KEY); + String iconUrl = singleMultiRedditJSON.getString(JSONUtils.ICON_URL_KEY); + long createdUTC = singleMultiRedditJSON.getLong(JSONUtils.CREATED_UTC_KEY); + String visibility = singleMultiRedditJSON.getString(JSONUtils.VISIBILITY_KEY); + boolean over18 = singleMultiRedditJSON.getBoolean(JSONUtils.OVER_18_KEY); + String path = singleMultiRedditJSON.getString(JSONUtils.PATH_KEY); + String owner = singleMultiRedditJSON.getString(JSONUtils.OWNER_KEY); + boolean isSubscriber = singleMultiRedditJSON.getBoolean(JSONUtils.IS_SUBSCRIBER_KEY); + boolean isFavorited = singleMultiRedditJSON.getBoolean(JSONUtils.IS_FAVORITED_KEY); + + JSONArray subredditsArray = singleMultiRedditJSON.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)); + } + + return new MultiReddit(path, displayName, name, description, copiedFrom, + iconUrl, visibility, owner, nSubscribers, createdUTC, over18, isSubscriber, + isFavorited, subreddits); + } + + private static class ParseMultiRedditsListAsyncTask extends AsyncTask { + private JSONArray arrayResponse; + private boolean parseFailed; + private ArrayList multiReddits; + private ParseMultiRedditsListListener parseMultiRedditsListListener; + + ParseMultiRedditsListAsyncTask(String response, + ParseMultiRedditsListListener parseMultiRedditsListListener) { + this.parseMultiRedditsListListener = parseMultiRedditsListListener; + try { + arrayResponse = new JSONArray(response); + multiReddits = new ArrayList<>(); + parseFailed = false; + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + } + + @Override + protected Void doInBackground(Void... voids) { + 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(); + } + } + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (!parseFailed) { + parseMultiRedditsListListener.success(multiReddits); + } else { + parseMultiRedditsListListener.failed(); + } + } + } + + private static class ParseAndSaveMultiRedditAsyncTask extends AsyncTask { + private String response; + private RedditDataRoomDatabase redditDataRoomDatabase; + private MultiReddit multiReddit; + private ParseMultiRedditListener parseMultiRedditListener; + + ParseAndSaveMultiRedditAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase, + ParseMultiRedditListener parseMultiRedditListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.parseMultiRedditListener = parseMultiRedditListener; + this.response = response; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + multiReddit = parseMultiReddit(new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY)); + redditDataRoomDatabase.multiRedditDao().insert(multiReddit); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (multiReddit != null) { + parseMultiRedditListener.success(); + } else { + parseMultiRedditListener.failed(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 848e8807..19ca23c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -288,4 +288,8 @@ public interface RedditAPI { @GET("{multipath}.json?raw_json=1") Call getMultiRedditPostsOauth(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after, @HeaderMap Map headers); + + @FormUrlEncoded + @POST("/api/multi/multipath") + Call createMultiReddit(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/RedditUtils.java index 28faaf6d..2e7c93c8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/RedditUtils.java @@ -82,6 +82,7 @@ public class RedditUtils { public static final String MAKE_FAVORITE_KEY = "make_favorite"; public static final String MULTIPATH_KEY = "multipath"; + public static final String MODEL_KEY = "model"; public static Map getHttpBasicAuthHeader() { Map params = new HashMap<>(); diff --git a/app/src/main/res/drawable-night/ic_add_menu_item_24dp.xml b/app/src/main/res/drawable-night/ic_add_menu_item_24dp.xml new file mode 100644 index 00000000..ed95d188 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_add_menu_item_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_add_menu_item_24dp.xml b/app/src/main/res/drawable/ic_add_menu_item_24dp.xml new file mode 100644 index 00000000..b6814bc2 --- /dev/null +++ b/app/src/main/res/drawable/ic_add_menu_item_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_create_multi_reddit.xml b/app/src/main/res/layout/activity_create_multi_reddit.xml new file mode 100644 index 00000000..c35b8bf4 --- /dev/null +++ b/app/src/main/res/layout/activity_create_multi_reddit.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_multi_reddit_listing.xml b/app/src/main/res/layout/activity_multi_reddit_listing.xml index 322f4b30..1669a8c8 100644 --- a/app/src/main/res/layout/activity_multi_reddit_listing.xml +++ b/app/src/main/res/layout/activity_multi_reddit_listing.xml @@ -70,4 +70,14 @@ + + \ No newline at end of file diff --git a/app/src/main/res/menu/create_multi_reddit_activity.xml b/app/src/main/res/menu/create_multi_reddit_activity.xml new file mode 100644 index 00000000..c2ae2455 --- /dev/null +++ b/app/src/main/res/menu/create_multi_reddit_activity.xml @@ -0,0 +1,10 @@ + + + + \ 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 6d0667a7..0a57087b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,8 +15,9 @@ Edit Comment Inbox Settings - Saved - Multireddit + Saved + Multireddit + Create Multireddit Open navigation drawer Close navigation drawer @@ -427,4 +428,12 @@ Light Theme Dark Theme + + Name (Max 50 Characters) + Description + Private + Select Subreddit + Where is the name? + Cannot create this multireddit + This multireddit already exists -- cgit v1.2.3