diff options
Diffstat (limited to 'app/src/main/java')
11 files changed, 499 insertions, 131 deletions
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<String> 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<MultiReddit> 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<String, String> 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<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<String> 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<MultiReddit> multiReddits); + void failed(); + } + + public static void fetchMyMultiReddits(Retrofit oauthRetrofit, String accessToken, FetchMultiRedditListener fetchMultiRedditListener) { + oauthRetrofit.create(RedditAPI.class) + .getMyMultiReddits(RedditUtils.getOAuthHeader(accessToken)).enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + ParseMultiReddit.parseMultiRedditsList(response.body(), new ParseMultiReddit.ParseMultiRedditsListListener() { + @Override + public void success(ArrayList<MultiReddit> multiReddits) { + fetchMultiRedditListener.success(multiReddits); + } + + @Override + public void failed() { + fetchMultiRedditListener.failed(); + } + }); + } else { + fetchMultiRedditListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> 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<MultiReddit> multiReddits); - void failed(); - } - - public static void getMyMultiReddits(Retrofit oauthRetrofit, String accessToken, GetMultiRedditListener getMultiRedditListener) { - oauthRetrofit.create(RedditAPI.class) - .getMyMultiReddits(RedditUtils.getOAuthHeader(accessToken)).enqueue(new Callback<String>() { - @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - new ParseMultiRedditsListAsyncTask(response.body(), new ParseMultiRedditsListAsyncTask.ParseMultiRedditsListAsyncTaskListener() { - @Override - public void success(ArrayList<MultiReddit> multiReddits) { - getMultiRedditListener.success(multiReddits); - } - - @Override - public void failed() { - getMultiRedditListener.failed(); - } - }).execute(); - } else { - getMultiRedditListener.failed(); - } - } - - @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { - getMultiRedditListener.failed(); - } - }); - } - - private static class ParseMultiRedditsListAsyncTask extends AsyncTask<Void, Void, Void> { - private JSONArray arrayResponse; - private boolean parseFailed; - private ArrayList<MultiReddit> multiReddits; - private ParseMultiRedditsListAsyncTaskListener parseMultiRedditsListAsyncTaskListener; - - interface ParseMultiRedditsListAsyncTaskListener { - void success(ArrayList<MultiReddit> 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<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(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<String> 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<MultiReddit> 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<String> 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<Void, Void, Void> { + private JSONArray arrayResponse; + private boolean parseFailed; + private ArrayList<MultiReddit> 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<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))); + } 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<Void, Void, Void> { + 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<String> getMultiRedditPostsOauth(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after, @HeaderMap Map<String, String> headers); + + @FormUrlEncoded + @POST("/api/multi/multipath") + Call<String> createMultiReddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> 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<String, String> getHttpBasicAuthHeader() { Map<String, String> params = new HashMap<>(); |