From 4edbfab34821d5cb2af30f7bb0a930cd0cfe7bc7 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 11 Jul 2024 17:47:56 -0400 Subject: Update online theme information after modifying the theme. --- .../activities/CustomThemeListingActivity.java | 25 ++++++++++++++++++++-- .../activities/CustomizeThemeActivity.java | 10 +++++++++ ...nlineCustomThemeListingRecyclerViewAdapter.java | 16 ++++++++++++++ .../CustomThemeOptionsBottomSheetFragment.java | 10 ++++----- .../fragments/CustomThemeListingFragment.java | 24 +++++++++++++++++++++ 5 files changed, 77 insertions(+), 8 deletions(-) (limited to 'app/src/main/java/ml') 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 ceaa27cf..500d9506 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; @@ -12,6 +13,7 @@ import android.view.View; import android.widget.EditText; import android.widget.Toast; +import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; @@ -91,6 +93,7 @@ public class CustomThemeListingActivity extends BaseActivity implements private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; private ActivityCustomThemeListingBinding binding; + private ActivityResultLauncher customizeThemeActivityResultLauncher; @Override protected void onCreate(Bundle savedInstanceState) { @@ -181,6 +184,24 @@ public class CustomThemeListingActivity extends BaseActivity implements applyTabLayoutTheme(binding.tabLayoutCustomizeThemeListingActivity); } + @Override + public void editTheme(String themeName, @Nullable OnlineCustomThemeMetadata onlineCustomThemeMetadata, int indexInThemeList) { + Intent intent = new Intent(this, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, themeName); + intent.putExtra(CustomizeThemeActivity.EXTRA_ONLINE_CUSTOM_THEME_METADATA, onlineCustomThemeMetadata); + intent.putExtra(CustomizeThemeActivity.EXTRA_INDEX_IN_THEME_LIST, indexInThemeList); + + if (indexInThemeList >= 0) { + //Online theme + Fragment fragment = sectionsPagerAdapter.getOnlineThemeFragment(); + if (fragment != null && ((CustomThemeListingFragment) fragment).getCustomizeThemeActivityResultLauncher() != null) { + ((CustomThemeListingFragment) fragment).getCustomizeThemeActivityResultLauncher().launch(intent); + return; + } + } + startActivity(intent); + } + @Override public void changeName(String oldThemeName) { View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_name, null); @@ -409,11 +430,11 @@ public class CustomThemeListingActivity extends BaseActivity implements } @Nullable - private Fragment getCurrentFragment() { + private Fragment getOnlineThemeFragment() { if (fragmentManager == null) { return null; } - return fragmentManager.findFragmentByTag("f" + binding.viewPager2CustomizeThemeListingActivity.getCurrentItem()); + return fragmentManager.findFragmentByTag("f1"); } @Override 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 27328fdf..82a152b3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java @@ -51,8 +51,12 @@ public class CustomizeThemeActivity extends BaseActivity { public static final int EXTRA_AMOLED_THEME = CustomThemeSharedPreferencesUtils.AMOLED; public static final String EXTRA_THEME_NAME = "ETN"; public static final String EXTRA_ONLINE_CUSTOM_THEME_METADATA = "EOCTM"; + public static final String EXTRA_INDEX_IN_THEME_LIST = "EIITL"; public static final String EXTRA_IS_PREDEFIINED_THEME = "EIPT"; public static final String EXTRA_CREATE_THEME = "ECT"; + public static final String RETURN_EXTRA_THEME_NAME = "RETN"; + public static final String RETURN_EXTRA_PRIMARY_COLOR = "REPC"; + public static final String RETURN_EXTRA_INDEX_IN_THEME_LIST = "REIITL"; private static final String CUSTOM_THEME_SETTINGS_ITEMS_STATE = "CTSIS"; private static final String THEME_NAME_STATE = "TNS"; @@ -257,6 +261,12 @@ public class CustomizeThemeActivity extends BaseActivity { public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { Toast.makeText(CustomizeThemeActivity.this, R.string.online_theme_modified, Toast.LENGTH_SHORT).show(); + Intent returnIntent = new Intent(); + returnIntent.putExtra(RETURN_EXTRA_INDEX_IN_THEME_LIST, getIntent().getIntExtra(EXTRA_INDEX_IN_THEME_LIST, -1)); + returnIntent.putExtra(RETURN_EXTRA_THEME_NAME, customTheme.name); + returnIntent.putExtra(RETURN_EXTRA_PRIMARY_COLOR, '#' + Integer.toHexString(customTheme.colorPrimary)); + setResult(RESULT_OK, returnIntent); + finish(); } else { Toast.makeText(CustomizeThemeActivity.this, R.string.upload_theme_failed, Toast.LENGTH_SHORT).show(); 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 27bb404f..630bfc0a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/OnlineCustomThemeListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/OnlineCustomThemeListingRecyclerViewAdapter.java @@ -10,6 +10,7 @@ import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.paging.ItemSnapshotList; import androidx.paging.PagingDataAdapter; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; @@ -80,6 +81,7 @@ public class OnlineCustomThemeListingRecyclerViewAdapter extends PagingDataAdapt Bundle bundle = new Bundle(); bundle.putString(CustomThemeOptionsBottomSheetFragment.EXTRA_THEME_NAME, onlineCustomThemeMetadata.name); bundle.putParcelable(CustomThemeOptionsBottomSheetFragment.EXTRA_ONLINE_CUSTOM_THEME_METADATA, onlineCustomThemeMetadata); + bundle.putInt(CustomThemeOptionsBottomSheetFragment.EXTRA_INDEX_IN_THEME_LIST, holder.getBindingAdapterPosition()); customThemeOptionsBottomSheetFragment.setArguments(bundle); customThemeOptionsBottomSheetFragment.show(activity.getSupportFragmentManager(), customThemeOptionsBottomSheetFragment.getTag()); }); @@ -90,6 +92,20 @@ public class OnlineCustomThemeListingRecyclerViewAdapter extends PagingDataAdapt } } + public void updateMetadata(int index, String themeName, String primaryColor) { + if (index >= 0) { + ItemSnapshotList list = snapshot(); + if (index < list.size()) { + OnlineCustomThemeMetadata metadata = list.get(index); + if (metadata != null) { + metadata.name = themeName; + metadata.colorPrimary = primaryColor; + } + } + notifyItemChanged(index); + } + } + class OnlineCustomThemeViewHolder extends RecyclerView.ViewHolder { ItemUserCustomThemeBinding binding; 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 4fb6113c..ffd68c93 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CustomThemeOptionsBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CustomThemeOptionsBottomSheetFragment.java @@ -1,17 +1,16 @@ package ml.docilealligator.infinityforreddit.bottomsheetfragments; import android.content.Context; -import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import ml.docilealligator.infinityforreddit.activities.BaseActivity; -import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.customtheme.OnlineCustomThemeMetadata; import ml.docilealligator.infinityforreddit.customviews.LandscapeExpandedRoundedBottomSheetDialogFragment; import ml.docilealligator.infinityforreddit.databinding.FragmentCustomThemeOptionsBottomSheetBinding; @@ -24,6 +23,7 @@ public class CustomThemeOptionsBottomSheetFragment extends LandscapeExpandedRoun public static final String EXTRA_THEME_NAME = "ETN"; public static final String EXTRA_ONLINE_CUSTOM_THEME_METADATA = "ECT"; + public static final String EXTRA_INDEX_IN_THEME_LIST = "EIITL"; private String themeName; private OnlineCustomThemeMetadata onlineCustomThemeMetadata; @@ -34,6 +34,7 @@ public class CustomThemeOptionsBottomSheetFragment extends LandscapeExpandedRoun } public interface CustomThemeOptionsBottomSheetFragmentListener { + void editTheme(String themeName, @Nullable OnlineCustomThemeMetadata onlineCustomThemeMetadata, int indexInThemeList); void changeName(String oldThemeName); void shareTheme(String themeName); void shareTheme(OnlineCustomThemeMetadata onlineCustomThemeMetadata); @@ -51,10 +52,7 @@ public class CustomThemeOptionsBottomSheetFragment extends LandscapeExpandedRoun 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_ONLINE_CUSTOM_THEME_METADATA, onlineCustomThemeMetadata); - startActivity(intent); + ((CustomThemeOptionsBottomSheetFragmentListener) activity).editTheme(themeName, onlineCustomThemeMetadata, getArguments().getInt(EXTRA_INDEX_IN_THEME_LIST, -1)); dismiss(); }); 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 83335b3c..fab4d49a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CustomThemeListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CustomThemeListingFragment.java @@ -1,13 +1,18 @@ package ml.docilealligator.infinityforreddit.fragments; +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; @@ -21,6 +26,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.activities.BaseActivity; +import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.adapters.CustomThemeListingRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.OnlineCustomThemeListingRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeViewModel; @@ -60,6 +66,8 @@ public class CustomThemeListingFragment extends Fragment { private BaseActivity activity; private FragmentCustomThemeListingBinding binding; private boolean isOnline; + @Nullable + private ActivityResultLauncher customizeThemeActivityResultLauncher; public CustomThemeListingFragment() { // Required empty public constructor @@ -97,6 +105,17 @@ public class CustomThemeListingFragment extends Fragment { .get(CustomThemeViewModel.class); customThemeViewModel.getOnlineCustomThemeMetadata().observe(getViewLifecycleOwner(), customThemePagingData -> adapter.submitData(getViewLifecycleOwner().getLifecycle(), customThemePagingData)); + + customizeThemeActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), activityResult -> { + if (activityResult.getResultCode() == Activity.RESULT_OK) { + Intent data = activityResult.getData(); + int index = data.getIntExtra(CustomizeThemeActivity.RETURN_EXTRA_INDEX_IN_THEME_LIST, -1); + String themeName = data.getStringExtra(CustomizeThemeActivity.RETURN_EXTRA_THEME_NAME); + String primaryColorHex = data.getStringExtra(CustomizeThemeActivity.RETURN_EXTRA_PRIMARY_COLOR); + + adapter.updateMetadata(index, themeName, primaryColorHex); + } + }); } else { CustomThemeListingRecyclerViewAdapter adapter = new CustomThemeListingRecyclerViewAdapter(activity, CustomThemeWrapper.getPredefinedThemes(activity)); @@ -111,6 +130,11 @@ public class CustomThemeListingFragment extends Fragment { return binding.getRoot(); } + @Nullable + public ActivityResultLauncher getCustomizeThemeActivityResultLauncher() { + return customizeThemeActivityResultLauncher; + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); -- cgit v1.2.3