From 6e5eab371265c6959adc4d7ca4d3c0e60573d42b Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:25:26 -0400 Subject: Continue implementing online custom themes. Fetch OnlineCustomThemeMetadata instead of CustomTheme. --- .../activities/CustomThemeListingActivity.java | 37 +++++++++++++++ .../activities/CustomizeThemeActivity.java | 51 +++++++++++++++++---- ...nlineCustomThemeListingRecyclerViewAdapter.java | 29 ++++++------ .../apis/OnlineCustomThemeAPI.java | 4 ++ .../CustomThemeOptionsBottomSheetFragment.java | 17 ++++--- .../customtheme/CustomThemeViewModel.java | 4 +- .../customtheme/OnlineCustomThemeMetadata.java | 52 ++++++++++++++++++++++ .../customtheme/OnlineCustomThemePagingSource.java | 27 ++++++----- .../customtheme/OnlineCustomThemeRepository.java | 6 +-- .../fragments/CustomThemeListingFragment.java | 2 +- .../main/res/layout/activity_customize_theme.xml | 7 +++ app/src/main/res/values/strings.xml | 1 + 12 files changed, 191 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeMetadata.java (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java index a106fe8d..ceaa27cf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java @@ -38,6 +38,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.apis.OnlineCustomThemeAPI; import ml.docilealligator.infinityforreddit.asynctasks.ChangeThemeName; import ml.docilealligator.infinityforreddit.asynctasks.DeleteTheme; import ml.docilealligator.infinityforreddit.asynctasks.GetCustomTheme; @@ -46,18 +47,26 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.CreateThemeBott import ml.docilealligator.infinityforreddit.bottomsheetfragments.CustomThemeOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.customtheme.OnlineCustomThemeMetadata; import ml.docilealligator.infinityforreddit.databinding.ActivityCustomThemeListingBinding; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.fragments.CustomThemeListingFragment; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; public class CustomThemeListingActivity extends BaseActivity implements CustomThemeOptionsBottomSheetFragment.CustomThemeOptionsBottomSheetFragmentListener, CreateThemeBottomSheetFragment.SelectBaseThemeBottomSheetFragmentListener, RecyclerViewContentScrollingInterface { + @Inject + @Named("online_custom_themes") + Retrofit onlineCustomThemesRetrofit; @Inject @Named("default") SharedPreferences sharedPreferences; @@ -262,6 +271,34 @@ public class CustomThemeListingActivity extends BaseActivity implements } } + @Override + public void shareTheme(OnlineCustomThemeMetadata onlineCustomThemeMetadata) { + onlineCustomThemesRetrofit.create(OnlineCustomThemeAPI.class) + .getCustomTheme(onlineCustomThemeMetadata.name, onlineCustomThemeMetadata.username) + .enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + if (clipboard != null) { + ClipData clip = ClipData.newPlainText("simple text", response.body()); + clipboard.setPrimaryClip(clip); + Snackbar.make(binding.coordinatorLayoutCustomThemeListingActivity, R.string.theme_copied, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(binding.coordinatorLayoutCustomThemeListingActivity, R.string.copy_theme_faied, Snackbar.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(CustomThemeListingActivity.this, response.message(), Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + Toast.makeText(CustomThemeListingActivity.this, R.string.cannot_download_theme_data, Toast.LENGTH_SHORT).show(); + } + }); + } + @Subscribe public void onRecreateActivityEvent(RecreateActivityEvent recreateActivityEvent) { ActivityCompat.recreate(this); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java index 88362d87..7b7a7acb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java @@ -2,11 +2,13 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.Intent; 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; import android.widget.Toast; import androidx.annotation.NonNull; @@ -26,14 +28,20 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.CustomizeThemeRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.apis.OnlineCustomThemeAPI; import ml.docilealligator.infinityforreddit.asynctasks.GetCustomTheme; import ml.docilealligator.infinityforreddit.asynctasks.InsertCustomTheme; import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeSettingsItem; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.customtheme.OnlineCustomThemeMetadata; import ml.docilealligator.infinityforreddit.databinding.ActivityCustomizeThemeBinding; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; public class CustomizeThemeActivity extends BaseActivity { @@ -42,12 +50,15 @@ public class CustomizeThemeActivity extends BaseActivity { public static final int EXTRA_DARK_THEME = CustomThemeSharedPreferencesUtils.DARK; public static final int EXTRA_AMOLED_THEME = CustomThemeSharedPreferencesUtils.AMOLED; public static final String EXTRA_THEME_NAME = "ETN"; - public static final String EXTRA_CUSTOM_THEME = "ECT"; + public static final String EXTRA_ONLINE_CUSTOM_THEME_METADATA = "EOCTM"; public static final String EXTRA_IS_PREDEFIINED_THEME = "EIPT"; public static final String EXTRA_CREATE_THEME = "ECT"; private static final String CUSTOM_THEME_SETTINGS_ITEMS_STATE = "CTSIS"; private static final String THEME_NAME_STATE = "TNS"; + @Inject + @Named("online_custom_themes") + Retrofit onlineCustomThemesRetrofit; @Inject @Named("default") SharedPreferences sharedPreferences; @@ -71,7 +82,7 @@ public class CustomizeThemeActivity extends BaseActivity { Executor mExecutor; private String themeName; - private CustomTheme customTheme; + private OnlineCustomThemeMetadata onlineCustomThemeMetadata; private boolean isPredefinedTheme; private ArrayList customThemeSettingsItems; private CustomizeThemeRecyclerViewAdapter adapter; @@ -144,7 +155,7 @@ public class CustomizeThemeActivity extends BaseActivity { } else { isPredefinedTheme = getIntent().getBooleanExtra(EXTRA_IS_PREDEFIINED_THEME, false); themeName = getIntent().getStringExtra(EXTRA_THEME_NAME); - customTheme = getIntent().getParcelableExtra(EXTRA_CUSTOM_THEME); + onlineCustomThemeMetadata = getIntent().getParcelableExtra(EXTRA_ONLINE_CUSTOM_THEME_METADATA); adapter = new CustomizeThemeRecyclerViewAdapter(this, customThemeWrapper, themeName); binding.recyclerViewCustomizeThemeActivity.setAdapter(adapter); @@ -158,11 +169,34 @@ public class CustomizeThemeActivity extends BaseActivity { binding.recyclerViewCustomizeThemeActivity.setAdapter(adapter); adapter.setCustomThemeSettingsItem(customThemeSettingsItems); } else { - if (customTheme != null) { - customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( - CustomizeThemeActivity.this, customTheme, androidVersion); - - adapter.setCustomThemeSettingsItem(customThemeSettingsItems); + if (onlineCustomThemeMetadata != null) { + binding.progressBarCustomizeThemeActivity.setVisibility(View.VISIBLE); + onlineCustomThemesRetrofit.create(OnlineCustomThemeAPI.class) + .getCustomTheme(onlineCustomThemeMetadata.name, onlineCustomThemeMetadata.username) + .enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( + CustomizeThemeActivity.this, + CustomTheme.fromJson(response.body()), + androidVersion); + + adapter.setCustomThemeSettingsItem(customThemeSettingsItems); + + binding.progressBarCustomizeThemeActivity.setVisibility(View.GONE); + } else { + Toast.makeText(CustomizeThemeActivity.this, response.message(), Toast.LENGTH_SHORT).show(); + finish(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + Toast.makeText(CustomizeThemeActivity.this, R.string.cannot_download_theme_data, Toast.LENGTH_SHORT).show(); + finish(); + } + }); } else { GetCustomTheme.getCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase, themeName, customTheme -> { @@ -261,5 +295,6 @@ public class CustomizeThemeActivity extends BaseActivity { protected void applyCustomTheme() { applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutCustomizeThemeActivity, binding.collapsingToolbarLayoutCustomizeThemeActivity, binding.toolbarCustomizeThemeActivity); binding.coordinatorCustomizeThemeActivity.setBackgroundColor(customThemeWrapper.getBackgroundColor()); + binding.progressBarCustomizeThemeActivity.setIndeterminateTintList(ColorStateList.valueOf(customThemeWrapper.getColorAccent())); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/OnlineCustomThemeListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/OnlineCustomThemeListingRecyclerViewAdapter.java index 47b3a55c..27bb404f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/OnlineCustomThemeListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/OnlineCustomThemeListingRecyclerViewAdapter.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit.adapters; import android.content.Intent; import android.content.res.ColorStateList; +import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -18,10 +19,10 @@ import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.bottomsheetfragments.CustomThemeOptionsBottomSheetFragment; -import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; +import ml.docilealligator.infinityforreddit.customtheme.OnlineCustomThemeMetadata; import ml.docilealligator.infinityforreddit.databinding.ItemUserCustomThemeBinding; -public class OnlineCustomThemeListingRecyclerViewAdapter extends PagingDataAdapter { +public class OnlineCustomThemeListingRecyclerViewAdapter extends PagingDataAdapter { private static final int VIEW_TYPE_USER_THEME = 1; private static final int VIEW_TYPE_USER_THEME_DIVIDER = 2; @@ -30,13 +31,13 @@ public class OnlineCustomThemeListingRecyclerViewAdapter extends PagingDataAdapt public OnlineCustomThemeListingRecyclerViewAdapter(BaseActivity activity) { super(new DiffUtil.ItemCallback<>() { @Override - public boolean areItemsTheSame(@NonNull CustomTheme oldItem, @NonNull CustomTheme newItem) { - return oldItem.name.equals(newItem.name); + public boolean areItemsTheSame(@NonNull OnlineCustomThemeMetadata oldItem, @NonNull OnlineCustomThemeMetadata newItem) { + return oldItem.name.equals(newItem.name) && oldItem.username.equals(newItem.username); } @Override - public boolean areContentsTheSame(@NonNull CustomTheme oldItem, @NonNull CustomTheme newItem) { - return false; + public boolean areContentsTheSame(@NonNull OnlineCustomThemeMetadata oldItem, @NonNull OnlineCustomThemeMetadata newItem) { + return true; } }); this.activity = activity; @@ -61,24 +62,24 @@ public class OnlineCustomThemeListingRecyclerViewAdapter extends PagingDataAdapt @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { if (holder instanceof OnlineCustomThemeViewHolder) { - CustomTheme customTheme = getItem(position); - ((OnlineCustomThemeViewHolder) holder).binding.colorPrimaryItemUserCustomTheme.setBackgroundTintList(ColorStateList.valueOf(customTheme.colorPrimary)); - ((OnlineCustomThemeViewHolder) holder).binding.nameTextViewItemUserCustomTheme.setText(customTheme.name); + OnlineCustomThemeMetadata onlineCustomThemeMetadata = getItem(position); + ((OnlineCustomThemeViewHolder) holder).binding.colorPrimaryItemUserCustomTheme.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(onlineCustomThemeMetadata.colorPrimary))); + ((OnlineCustomThemeViewHolder) holder).binding.nameTextViewItemUserCustomTheme.setText(onlineCustomThemeMetadata.name); ((OnlineCustomThemeViewHolder) holder).binding.addImageViewItemUserCustomTheme.setOnClickListener(view -> { Intent intent = new Intent(activity, CustomizeThemeActivity.class); - intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, customTheme.name); - intent.putExtra(CustomizeThemeActivity.EXTRA_CUSTOM_THEME, customTheme); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, onlineCustomThemeMetadata.name); + intent.putExtra(CustomizeThemeActivity.EXTRA_ONLINE_CUSTOM_THEME_METADATA, onlineCustomThemeMetadata); intent.putExtra(CustomizeThemeActivity.EXTRA_CREATE_THEME, true); activity.startActivity(intent); }); ((OnlineCustomThemeViewHolder) holder).binding.shareImageViewItemUserCustomTheme.setOnClickListener(view -> { - ((CustomThemeListingActivity) activity).shareTheme(customTheme); + ((CustomThemeListingActivity) activity).shareTheme(onlineCustomThemeMetadata); }); holder.itemView.setOnClickListener(view -> { CustomThemeOptionsBottomSheetFragment customThemeOptionsBottomSheetFragment = new CustomThemeOptionsBottomSheetFragment(); Bundle bundle = new Bundle(); - bundle.putString(CustomThemeOptionsBottomSheetFragment.EXTRA_THEME_NAME, customTheme.name); - bundle.putParcelable(CustomThemeOptionsBottomSheetFragment.EXTRA_CUSTOM_THEME, customTheme); + bundle.putString(CustomThemeOptionsBottomSheetFragment.EXTRA_THEME_NAME, onlineCustomThemeMetadata.name); + bundle.putParcelable(CustomThemeOptionsBottomSheetFragment.EXTRA_ONLINE_CUSTOM_THEME_METADATA, onlineCustomThemeMetadata); customThemeOptionsBottomSheetFragment.setArguments(bundle); customThemeOptionsBottomSheetFragment.show(activity.getSupportFragmentManager(), customThemeOptionsBottomSheetFragment.getTag()); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/OnlineCustomThemeAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/OnlineCustomThemeAPI.java index de2e46e2..e328a9b8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/OnlineCustomThemeAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/OnlineCustomThemeAPI.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit.apis; import com.google.common.util.concurrent.ListenableFuture; +import retrofit2.Call; import retrofit2.Response; import retrofit2.http.GET; import retrofit2.http.Query; @@ -9,4 +10,7 @@ import retrofit2.http.Query; public interface OnlineCustomThemeAPI { @GET("/themes/") ListenableFuture> getCustomThemesListenableFuture(@Query("page") String page); + + @GET("/themes/theme") + Call getCustomTheme(@Query("name") String themeName, @Query("username") String username); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CustomThemeOptionsBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CustomThemeOptionsBottomSheetFragment.java index e497bdff..4fb6113c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CustomThemeOptionsBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CustomThemeOptionsBottomSheetFragment.java @@ -12,7 +12,7 @@ import androidx.fragment.app.Fragment; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity; -import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; +import ml.docilealligator.infinityforreddit.customtheme.OnlineCustomThemeMetadata; import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment; import ml.docilealligator.infinityforreddit.databinding.FragmentCustomThemeOptionsBottomSheetBinding; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -23,10 +23,10 @@ import ml.docilealligator.infinityforreddit.utils.Utils; public class CustomThemeOptionsBottomSheetFragment extends LandscapeExpandedRoundedBottomSheetDialogFragment { public static final String EXTRA_THEME_NAME = "ETN"; - public static final String EXTRA_CUSTOM_THEME = "ECT"; + public static final String EXTRA_ONLINE_CUSTOM_THEME_METADATA = "ECT"; private String themeName; - private CustomTheme customTheme; + private OnlineCustomThemeMetadata onlineCustomThemeMetadata; private BaseActivity activity; public CustomThemeOptionsBottomSheetFragment() { @@ -36,6 +36,7 @@ public class CustomThemeOptionsBottomSheetFragment extends LandscapeExpandedRoun public interface CustomThemeOptionsBottomSheetFragmentListener { void changeName(String oldThemeName); void shareTheme(String themeName); + void shareTheme(OnlineCustomThemeMetadata onlineCustomThemeMetadata); void delete(String themeName); } @@ -45,20 +46,24 @@ public class CustomThemeOptionsBottomSheetFragment extends LandscapeExpandedRoun FragmentCustomThemeOptionsBottomSheetBinding binding = FragmentCustomThemeOptionsBottomSheetBinding.inflate(inflater, container, false); themeName = getArguments().getString(EXTRA_THEME_NAME); - customTheme = getArguments().getParcelable(EXTRA_CUSTOM_THEME); + onlineCustomThemeMetadata = getArguments().getParcelable(EXTRA_ONLINE_CUSTOM_THEME_METADATA); binding.themeNameTextViewCustomThemeOptionsBottomSheetFragment.setText(themeName); binding.editThemeTextViewCustomThemeOptionsBottomSheetFragment.setOnClickListener(view -> { Intent intent = new Intent(activity, CustomizeThemeActivity.class); intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, themeName); - intent.putExtra(CustomizeThemeActivity.EXTRA_CUSTOM_THEME, customTheme); + intent.putExtra(CustomizeThemeActivity.EXTRA_ONLINE_CUSTOM_THEME_METADATA, onlineCustomThemeMetadata); startActivity(intent); dismiss(); }); binding.shareThemeTextViewCustomThemeOptionsBottomSheetFragment.setOnClickListener(view -> { - ((CustomThemeOptionsBottomSheetFragmentListener) activity).shareTheme(themeName); + if (onlineCustomThemeMetadata != null) { + ((CustomThemeOptionsBottomSheetFragmentListener) activity).shareTheme(onlineCustomThemeMetadata); + } else { + ((CustomThemeOptionsBottomSheetFragmentListener) activity).shareTheme(themeName); + } dismiss(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeViewModel.java index b953cee2..9da70d3c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeViewModel.java @@ -48,8 +48,8 @@ public class CustomThemeViewModel extends ViewModel { return localCustomThemeRepository.getCurrentAmoledCustomTheme(); } - public LiveData> getOnlineCustomThemes() { - return onlineCustomThemeRepository.getOnlineCustomThemes(); + public LiveData> getOnlineCustomThemeMetadata() { + return onlineCustomThemeRepository.getOnlineCustomThemeMetadata(); } public static class Factory extends ViewModelProvider.NewInstanceFactory { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeMetadata.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeMetadata.java new file mode 100644 index 00000000..1e629f92 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeMetadata.java @@ -0,0 +1,52 @@ +package ml.docilealligator.infinityforreddit.customtheme; + +import android.os.Parcel; +import android.os.Parcelable; + +import androidx.annotation.NonNull; + +import com.google.gson.Gson; +import com.google.gson.JsonParseException; +import com.google.gson.annotations.SerializedName; + +public class OnlineCustomThemeMetadata implements Parcelable { + public String name; + public String username; + @SerializedName("primary_color") + public String colorPrimary; + + protected OnlineCustomThemeMetadata(Parcel in) { + name = in.readString(); + username = in.readString(); + colorPrimary = in.readString(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public OnlineCustomThemeMetadata createFromParcel(Parcel in) { + return new OnlineCustomThemeMetadata(in); + } + + @Override + public OnlineCustomThemeMetadata[] newArray(int size) { + return new OnlineCustomThemeMetadata[size]; + } + }; + + public static OnlineCustomThemeMetadata fromJson(String json) throws JsonParseException { + Gson gson = new Gson(); + return gson.fromJson(json, OnlineCustomThemeMetadata.class); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(@NonNull Parcel dest, int flags) { + dest.writeString(name); + dest.writeString(username); + dest.writeString(colorPrimary); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemePagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemePagingSource.java index 5bcfd175..b8049243 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemePagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemePagingSource.java @@ -7,6 +7,7 @@ import androidx.paging.PagingState; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.gson.JsonParseException; import org.json.JSONArray; import org.json.JSONException; @@ -24,7 +25,7 @@ import retrofit2.HttpException; import retrofit2.Response; import retrofit2.Retrofit; -public class OnlineCustomThemePagingSource extends ListenableFuturePagingSource { +public class OnlineCustomThemePagingSource extends ListenableFuturePagingSource { private final Executor executor; private final OnlineCustomThemeAPI api; private final RedditDataRoomDatabase redditDataRoomDatabase; @@ -37,19 +38,19 @@ public class OnlineCustomThemePagingSource extends ListenableFuturePagingSource< @Nullable @Override - public String getRefreshKey(@NonNull PagingState pagingState) { + public String getRefreshKey(@NonNull PagingState pagingState) { return null; } @NonNull @Override - public ListenableFuture> loadFuture(@NonNull LoadParams loadParams) { + public ListenableFuture> loadFuture(@NonNull LoadParams loadParams) { ListenableFuture> customThemes; customThemes = api.getCustomThemesListenableFuture(loadParams.getKey()); - ListenableFuture> pageFuture = Futures.transform(customThemes, this::transformData, executor); + ListenableFuture> pageFuture = Futures.transform(customThemes, this::transformData, executor); - ListenableFuture> partialLoadResultFuture = + ListenableFuture> partialLoadResultFuture = Futures.catching(pageFuture, HttpException.class, LoadResult.Error::new, executor); @@ -57,9 +58,9 @@ public class OnlineCustomThemePagingSource extends ListenableFuturePagingSource< IOException.class, LoadResult.Error::new, executor); } - public LoadResult transformData(Response response) { + public LoadResult transformData(Response response) { if (response.isSuccessful()) { - List themes = new ArrayList<>(); + List themeMetadataList = new ArrayList<>(); try { String responseString = response.body(); JSONObject data = new JSONObject(responseString); @@ -67,14 +68,16 @@ public class OnlineCustomThemePagingSource extends ListenableFuturePagingSource< JSONArray themesArray = data.getJSONArray(JSONUtils.DATA_KEY); for (int i = 0; i < themesArray.length(); i++) { try { - themes.add(CustomTheme.fromJson(themesArray.getJSONObject(i).getString(JSONUtils.DATA_KEY))); - } catch (JSONException ignore) {} + themeMetadataList.add(OnlineCustomThemeMetadata.fromJson(themesArray.getJSONObject(i).toString())); + } catch (JsonParseException ignore) { + + } } - if (themes.isEmpty()) { - return new LoadResult.Page<>(themes, null, null); + if (themeMetadataList.isEmpty()) { + return new LoadResult.Page<>(themeMetadataList, null, null); } else { - return new LoadResult.Page<>(themes, null, Integer.toString(page + 1)); + return new LoadResult.Page<>(themeMetadataList, null, Integer.toString(page + 1)); } } catch (JSONException e) { return new LoadResult.Error<>(new Exception("Response failed")); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeRepository.java index 7a978e7a..732a7a05 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/OnlineCustomThemeRepository.java @@ -15,21 +15,21 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import retrofit2.Retrofit; public class OnlineCustomThemeRepository { - private final LiveData> customThemes; + private final LiveData> customThemes; private MutableLiveData onlineCustomThemeFilterMutableLiveData; public OnlineCustomThemeRepository(Executor executor, Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase, CoroutineScope viewModelScope) { onlineCustomThemeFilterMutableLiveData = new MutableLiveData<>(new OnlineCustomThemeFilter()); - Pager pager = new Pager<>(new PagingConfig(25, 4, false, 10), + Pager pager = new Pager<>(new PagingConfig(25, 4, false, 10), () -> new OnlineCustomThemePagingSource(executor, retrofit, redditDataRoomDatabase)); customThemes = PagingLiveData.cachedIn(Transformations.switchMap(onlineCustomThemeFilterMutableLiveData, customThemeFilter -> PagingLiveData.getLiveData(pager)), viewModelScope); } - public LiveData> getOnlineCustomThemes() { + public LiveData> getOnlineCustomThemeMetadata() { return customThemes; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CustomThemeListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CustomThemeListingFragment.java index ddb3d11d..83335b3c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CustomThemeListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CustomThemeListingFragment.java @@ -95,7 +95,7 @@ public class CustomThemeListingFragment extends Fragment { customThemeViewModel = new ViewModelProvider(this, new CustomThemeViewModel.Factory(executor, onlineCustomThemesRetrofit, redditDataRoomDatabase)) .get(CustomThemeViewModel.class); - customThemeViewModel.getOnlineCustomThemes().observe(getViewLifecycleOwner(), + customThemeViewModel.getOnlineCustomThemeMetadata().observe(getViewLifecycleOwner(), customThemePagingData -> adapter.submitData(getViewLifecycleOwner().getLifecycle(), customThemePagingData)); } else { CustomThemeListingRecyclerViewAdapter adapter = new CustomThemeListingRecyclerViewAdapter(activity, diff --git a/app/src/main/res/layout/activity_customize_theme.xml b/app/src/main/res/layout/activity_customize_theme.xml index 275131c1..fffa5e60 100644 --- a/app/src/main/res/layout/activity_customize_theme.xml +++ b/app/src/main/res/layout/activity_customize_theme.xml @@ -41,4 +41,11 @@ app:layoutManager="ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> + + \ 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 f15f2893..cc3a44d7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -956,6 +956,7 @@ Copied! Paste and share it to other people. Cannot copy the theme configuration Cannot find this theme + Cannot download theme data Import Theme Cannot find data in clipboard Import theme successfully -- cgit v1.2.3