diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-03-26 16:30:05 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-03-26 16:30:05 +0000 |
commit | 3dca261deaa2674b1faca52fec213f6cc960a61f (patch) | |
tree | a8dfbfe0c1503014ef66ee01d31d383073041a98 /app/src/main/java/ml/docilealligator | |
parent | 0694c471c4ba39fa8235e1ecfcc4e8e40ad13ad2 (diff) | |
download | infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.tar infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.tar.gz infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.tar.bz2 infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.tar.lz infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.tar.xz infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.tar.zst infinity-for-reddit-3dca261deaa2674b1faca52fec213f6cc960a61f.zip |
Still implementing custom themes.
Diffstat (limited to 'app/src/main/java/ml/docilealligator')
14 files changed, 450 insertions, 35 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java index 0e8376ec..6bc3a927 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomThemeListingActivity.java @@ -1,8 +1,12 @@ package ml.docilealligator.infinityforreddit.Activity; +import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.MenuItem; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; @@ -11,6 +15,8 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import javax.inject.Inject; import javax.inject.Named; @@ -18,13 +24,17 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Adapter.CustomThemeListingRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.AsyncTask.ChangeThemeNameAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteThemeAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Fragment.CustomThemeOptionsBottomSheetFragment; +import ml.docilealligator.infinityforreddit.Fragment.SelectBaseThemeBottomSheetFragment; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -public class CustomThemeListingActivity extends BaseActivity { +public class CustomThemeListingActivity extends BaseActivity implements CustomThemeOptionsBottomSheetFragment.CustomThemeOptionsBottomSheetFragmentListener { @BindView(R.id.appbar_layout_customize_theme_listing_activity) AppBarLayout appBarLayout; @@ -32,6 +42,8 @@ public class CustomThemeListingActivity extends BaseActivity { Toolbar toolbar; @BindView(R.id.recycler_view_customize_theme_listing_activity) RecyclerView recyclerView; + @BindView(R.id.fab_custom_theme_listing_activity) + FloatingActionButton fab; @Inject @Named("default") SharedPreferences sharedPreferences; @@ -64,6 +76,11 @@ public class CustomThemeListingActivity extends BaseActivity { new CustomThemeViewModel.Factory(redditDataRoomDatabase)) .get(CustomThemeViewModel.class); customThemeViewModel.getAllCustomThemes().observe(this, adapter::setUserThemes); + + fab.setOnClickListener(view -> { + SelectBaseThemeBottomSheetFragment selectBaseThemeBottomSheetFragment = new SelectBaseThemeBottomSheetFragment(); + selectBaseThemeBottomSheetFragment.show(getSupportFragmentManager(), selectBaseThemeBottomSheetFragment.getTag()); + }); } @Override @@ -89,4 +106,42 @@ public class CustomThemeListingActivity extends BaseActivity { protected void applyCustomTheme() { applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); } + + @Override + public void changeName(String oldName) { + View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_theme_name, null); + EditText themeNameEditText = dialogView.findViewById(R.id.theme_name_edit_text_edit_theme_name_dialog); + themeNameEditText.setText(oldName); + themeNameEditText.requestFocus(); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm != null) { + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + } + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.edit_theme_name) + .setView(dialogView) + .setPositiveButton(R.string.ok, (dialogInterface, i) + -> { + if (imm != null) { + imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); + } + new ChangeThemeNameAsyncTask(redditDataRoomDatabase, oldName, themeNameEditText.getText().toString()); + }) + .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { + if (imm != null) { + imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); + } + }) + .setOnDismissListener(dialogInterface -> { + if (imm != null) { + imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); + } + }) + .show(); + } + + @Override + public void delete(String name) { + new DeleteThemeAsyncTask(redditDataRoomDatabase, name).execute(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java index 7dde8fc6..e4847ac4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java @@ -9,10 +9,12 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; @@ -43,9 +45,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_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"; + @BindView(R.id.coordinator_customize_theme_activity) + CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_customize_theme_activity) AppBarLayout appBarLayout; @BindView(R.id.toolbar_customize_theme_activity) @@ -88,6 +93,10 @@ public class CustomizeThemeActivity extends BaseActivity { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + if (getIntent().getBooleanExtra(EXTRA_CREATE_THEME, false)) { + setTitle(R.string.customize_theme_activity_create_theme_label); + } + recyclerView.setLayoutManager(new LinearLayoutManager(this)); if (savedInstanceState != null) { @@ -132,7 +141,6 @@ public class CustomizeThemeActivity extends BaseActivity { themeName = customTheme.name; } - setTitle(themeName); adapter = new CustomizeThemeRecyclerViewAdapter(this, themeName, isPredefinedTheme); recyclerView.setAdapter(adapter); adapter.setCustomThemeSettingsItem(customThemeSettingsItems); @@ -142,14 +150,12 @@ public class CustomizeThemeActivity extends BaseActivity { themeName = getIntent().getStringExtra(EXTRA_THEME_NAME); adapter = new CustomizeThemeRecyclerViewAdapter(this, themeName, isPredefinedTheme); recyclerView.setAdapter(adapter); - setTitle(themeName); if (isPredefinedTheme) { customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( CustomizeThemeActivity.this, CustomThemeWrapper.getPredefinedCustomTheme(this, themeName), androidVersion); - setTitle(themeName); adapter = new CustomizeThemeRecyclerViewAdapter(this, themeName, isPredefinedTheme); recyclerView.setAdapter(adapter); adapter.setCustomThemeSettingsItem(customThemeSettingsItems); @@ -184,13 +190,20 @@ public class CustomizeThemeActivity extends BaseActivity { finish(); return true; case R.id.action_save_customize_theme_activity: - CustomTheme customTheme = CustomTheme.convertSettingsItemsToCustomTheme(customThemeSettingsItems, themeName); - new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences, - darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme, () -> { - Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); - EventBus.getDefault().post(new RecreateActivityEvent()); - finish(); - }).execute(); + if (adapter != null) { + themeName = adapter.getThemeName(); + if (themeName.equals("")) { + Snackbar.make(coordinatorLayout, R.string.no_theme_name, Snackbar.LENGTH_SHORT).show(); + return true; + } + CustomTheme customTheme = CustomTheme.convertSettingsItemsToCustomTheme(customThemeSettingsItems, themeName); + new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences, + darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme, () -> { + Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + finish(); + }).execute(); + } return true; } @@ -201,8 +214,10 @@ public class CustomizeThemeActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putParcelableArrayList(CUSTOM_THEME_SETTINGS_ITEMS_STATE, customThemeSettingsItems); - outState.putString(THEME_NAME_STATE, themeName); + if (adapter != null) { + outState.putParcelableArrayList(CUSTOM_THEME_SETTINGS_ITEMS_STATE, customThemeSettingsItems); + outState.putString(THEME_NAME_STATE, adapter.getThemeName()); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java index 267c72cb..525564b8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomThemeListingRecyclerViewAdapter.java @@ -1,8 +1,8 @@ package ml.docilealligator.infinityforreddit.Adapter; -import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -10,6 +10,7 @@ import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; @@ -19,6 +20,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; +import ml.docilealligator.infinityforreddit.Fragment.CustomThemeOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.R; public class CustomThemeListingRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { @@ -27,12 +29,12 @@ public class CustomThemeListingRecyclerViewAdapter extends RecyclerView.Adapter< private static final int VIEW_TYPE_PREDEFINED_THEME_DIVIDER = 2; private static final int VIEW_TYPE_USER_THEME_DIVIDER = 3; - private Context context; + private AppCompatActivity activity; private ArrayList<CustomTheme> predefinedCustomThemes; private ArrayList<CustomTheme> userCustomThemes; - public CustomThemeListingRecyclerViewAdapter(Context context, ArrayList<CustomTheme> predefinedCustomThemes) { - this.context = context; + public CustomThemeListingRecyclerViewAdapter(AppCompatActivity activity, ArrayList<CustomTheme> predefinedCustomThemes) { + this.activity = activity; this.predefinedCustomThemes = predefinedCustomThemes; userCustomThemes = new ArrayList<>(); } @@ -72,25 +74,30 @@ public class CustomThemeListingRecyclerViewAdapter extends RecyclerView.Adapter< ((PredefinedCustomThemeViewHolder) holder).colorPrimaryView.setBackgroundTintList(ColorStateList.valueOf(customTheme.colorPrimary)); ((PredefinedCustomThemeViewHolder) holder).nameTextView.setText(customTheme.name); ((PredefinedCustomThemeViewHolder) holder).itemView.setOnClickListener(view -> { - Intent intent = new Intent(context, CustomizeThemeActivity.class); + Intent intent = new Intent(activity, CustomizeThemeActivity.class); intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, customTheme.name); intent.putExtra(CustomizeThemeActivity.EXTRA_IS_PREDEFIINED_THEME, true); - context.startActivity(intent); + activity.startActivity(intent); }); } else if (holder instanceof UserCustomThemeViewHolder) { CustomTheme customTheme = userCustomThemes.get(position - predefinedCustomThemes.size() - 2); ((UserCustomThemeViewHolder) holder).colorPrimaryView.setBackgroundTintList(ColorStateList.valueOf(customTheme.colorPrimary)); ((UserCustomThemeViewHolder) holder).nameTextView.setText(customTheme.name); - ((UserCustomThemeViewHolder) holder).deleteImageView.setOnClickListener(view -> { - + ((UserCustomThemeViewHolder) holder).createThemeImageView.setOnClickListener(view -> { + Intent intent = new Intent(activity, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, customTheme.name); + intent.putExtra(CustomizeThemeActivity.EXTRA_CREATE_THEME, true); + activity.startActivity(intent); }); ((UserCustomThemeViewHolder) holder).shareImageView.setOnClickListener(view -> { }); ((UserCustomThemeViewHolder) holder).itemView.setOnClickListener(view -> { - Intent intent = new Intent(context, CustomizeThemeActivity.class); - intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, customTheme.name); - context.startActivity(intent); + CustomThemeOptionsBottomSheetFragment customThemeOptionsBottomSheetFragment = new CustomThemeOptionsBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(CustomThemeOptionsBottomSheetFragment.EXTRA_THEME_NAME, customTheme.name); + customThemeOptionsBottomSheetFragment.setArguments(bundle); + customThemeOptionsBottomSheetFragment.show(activity.getSupportFragmentManager(), customThemeOptionsBottomSheetFragment.getTag()); }); } else if (holder instanceof PreDefinedThemeDividerViewHolder) { ((TextView) ((PreDefinedThemeDividerViewHolder) holder).itemView).setText(R.string.predefined_themes); @@ -115,6 +122,8 @@ public class CustomThemeListingRecyclerViewAdapter extends RecyclerView.Adapter< View colorPrimaryView; @BindView(R.id.name_text_view_item_predefined_custom_theme) TextView nameTextView; + @BindView(R.id.add_image_view_item_predefined_custom_theme) + ImageView createThemeImageView; public PredefinedCustomThemeViewHolder(@NonNull View itemView) { super(itemView); @@ -128,8 +137,8 @@ public class CustomThemeListingRecyclerViewAdapter extends RecyclerView.Adapter< View colorPrimaryView; @BindView(R.id.name_text_view_item_user_custom_theme) TextView nameTextView; - @BindView(R.id.delete_image_view_item_user_custom_theme) - ImageView deleteImageView; + @BindView(R.id.add_image_view_item_user_custom_theme) + ImageView createThemeImageView; @BindView(R.id.share_image_view_item_user_custom_theme) ImageView shareImageView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomizeThemeRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomizeThemeRecyclerViewAdapter.java index be2aae85..95833e72 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomizeThemeRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CustomizeThemeRecyclerViewAdapter.java @@ -1,9 +1,12 @@ package ml.docilealligator.infinityforreddit.Adapter; +import android.content.Context; import android.content.res.ColorStateList; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import android.widget.Switch; import android.widget.TextView; @@ -11,6 +14,8 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + import java.util.ArrayList; import butterknife.BindView; @@ -40,7 +45,7 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<Recy public int getItemViewType(int position) { if (position == 0) { return VIEW_TYPE_THEME_NAME; - } else if (position > 3 && position < customThemeSettingsItems.size() - 3) { + } else if (position > 3 && position < customThemeSettingsItems.size() - 2) { return VIEW_TYPE_COLOR; } @@ -82,7 +87,36 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<Recy } else if (holder instanceof ThemeNameItemViewHolder) { ((ThemeNameItemViewHolder) holder).themeNameTextView.setText(themeName); holder.itemView.setOnClickListener(view -> { - + View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_edit_theme_name, null); + EditText themeNameEditText = dialogView.findViewById(R.id.theme_name_edit_text_edit_theme_name_dialog); + themeNameEditText.setText(themeName); + themeNameEditText.requestFocus(); + InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm != null) { + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + } + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.edit_theme_name) + .setView(dialogView) + .setPositiveButton(R.string.ok, (dialogInterface, i) + -> { + if (imm != null) { + imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); + } + themeName = themeNameEditText.getText().toString(); + ((ThemeNameItemViewHolder) holder).themeNameTextView.setText(themeName); + }) + .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { + if (imm != null) { + imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); + } + }) + .setOnDismissListener(dialogInterface -> { + if (imm != null) { + imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); + } + }) + .show(); }); } } @@ -99,6 +133,10 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<Recy notifyDataSetChanged(); } + public String getThemeName() { + return themeName; + } + class ThemeColorItemViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.color_image_view_item_custom_theme_color_item) View colorImageView; @@ -135,10 +173,6 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<Recy public ThemeNameItemViewHolder(@NonNull View itemView) { super(itemView); ButterKnife.bind(this, itemView); - if (isPredefinedTheme) { - descriptionTextView.setText(activity.getString(R.string.theme_name_forbid_change_description)); - itemView.setOnClickListener(view ->{}); - } } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/ChangeThemeNameAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/ChangeThemeNameAsyncTask.java new file mode 100644 index 00000000..65fc3b8d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/ChangeThemeNameAsyncTask.java @@ -0,0 +1,23 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class ChangeThemeNameAsyncTask extends AsyncTask<Void, Void, Void> { + private RedditDataRoomDatabase redditDataRoomDatabase; + private String oldName; + private String newName; + + public ChangeThemeNameAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String oldName, String newName) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.oldName = oldName; + this.newName = newName; + } + + @Override + protected Void doInBackground(Void... voids) { + redditDataRoomDatabase.customThemeDao().updateName(oldName, newName); + return null; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteThemeAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteThemeAsyncTask.java new file mode 100644 index 00000000..7b8caec2 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteThemeAsyncTask.java @@ -0,0 +1,21 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeleteThemeAsyncTask extends AsyncTask<Void, Void, Void> { + private RedditDataRoomDatabase redditDataRoomDatabase; + private String themeName; + + public DeleteThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String themeName) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.themeName = themeName; + } + + @Override + protected Void doInBackground(Void... voids) { + redditDataRoomDatabase.customThemeDao().deleteCustomTheme(themeName); + return null; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java index 48ec7122..8741f8e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java @@ -25,6 +25,15 @@ public interface CustomThemeDao { @Query("SELECT * FROM custom_themes WHERE is_amoled_theme = 1 LIMIT 1") CustomTheme getAmoledCustomTheme(); + @Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1") + LiveData<CustomTheme> getLightCustomThemeLiveData(); + + @Query("SELECT * FROM custom_themes WHERE is_dark_theme = 1 LIMIT 1") + LiveData<CustomTheme> getDarkCustomThemeLiveData(); + + @Query("SELECT * FROM custom_themes WHERE is_amoled_theme = 1 LIMIT 1") + LiveData<CustomTheme> getAmoledCustomThemeLiveData(); + @Query("SELECT * FROM custom_themes WHERE name = :name COLLATE NOCASE LIMIT 1") CustomTheme getCustomTheme(String name); @@ -39,4 +48,7 @@ public interface CustomThemeDao { @Query("DELETE FROM custom_themes WHERE name = :name") void deleteCustomTheme(String name); + + @Query("UPDATE custom_themes SET name = :newName WHERE name = :oldName") + void updateName(String oldName, String newName); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java index e2c0b692..2084bd5b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java @@ -8,12 +8,30 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class CustomThemeRepository { private LiveData<List<CustomTheme>> mAllCustomThemes; + private LiveData<CustomTheme> mCurrentLightCustomTheme; + private LiveData<CustomTheme> mCurrentDarkCustomTheme; + private LiveData<CustomTheme> mCurrentAmoledCustomTheme; CustomThemeRepository(RedditDataRoomDatabase redditDataRoomDatabase) { mAllCustomThemes = redditDataRoomDatabase.customThemeDao().getAllCustomThemes(); + mCurrentLightCustomTheme = redditDataRoomDatabase.customThemeDao().getLightCustomThemeLiveData(); + mCurrentDarkCustomTheme = redditDataRoomDatabase.customThemeDao().getDarkCustomThemeLiveData(); + mCurrentAmoledCustomTheme = redditDataRoomDatabase.customThemeDao().getAmoledCustomThemeLiveData(); } LiveData<List<CustomTheme>> getAllCustomThemes() { return mAllCustomThemes; } + + LiveData<CustomTheme> getCurrentLightCustomTheme() { + return mCurrentLightCustomTheme; + } + + LiveData<CustomTheme> getCurrentDarkCustomTheme() { + return mCurrentDarkCustomTheme; + } + + LiveData<CustomTheme> getCurrentAmoledCustomTheme() { + return mCurrentAmoledCustomTheme; + } } 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 c9b1fde2..be8f044d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeViewModel.java @@ -11,16 +11,34 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class CustomThemeViewModel extends ViewModel { private LiveData<List<CustomTheme>> mAllCustomThemes; + private LiveData<CustomTheme> mCurrentLightTheme; + private LiveData<CustomTheme> mCurrentDarkTheme; + private LiveData<CustomTheme> mCurrentAmoledTheme; public CustomThemeViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { CustomThemeRepository customThemeRepository = new CustomThemeRepository(redditDataRoomDatabase); mAllCustomThemes = customThemeRepository.getAllCustomThemes(); + mCurrentLightTheme = customThemeRepository.getCurrentLightCustomTheme(); + mCurrentDarkTheme = customThemeRepository.getCurrentDarkCustomTheme(); + mCurrentAmoledTheme = customThemeRepository.getCurrentAmoledCustomTheme(); } public LiveData<List<CustomTheme>> getAllCustomThemes() { return mAllCustomThemes; } + public LiveData<CustomTheme> getCurrentLightThemeLiveData() { + return mCurrentLightTheme; + } + + public LiveData<CustomTheme> getCurrentDarkThemeLiveData() { + return mCurrentDarkTheme; + } + + public LiveData<CustomTheme> getCurrentAmoledThemeLiveData() { + return mCurrentAmoledTheme; + } + public static class Factory extends ViewModelProvider.NewInstanceFactory { private RedditDataRoomDatabase mRedditDataRoomDatabase; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java index b53fbab8..3af6c99a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java @@ -546,8 +546,8 @@ public class CustomThemeWrapper { public static CustomTheme getIndigoAmoled(Context context) { CustomTheme customTheme = new CustomTheme(context.getString(R.string.theme_name_indigo_amoled)); customTheme.isLightTheme = false; - customTheme.isDarkTheme = true; - customTheme.isAmoledTheme = false; + customTheme.isDarkTheme = false; + customTheme.isAmoledTheme = true; customTheme.colorPrimary = Color.parseColor("#000000"); customTheme.colorPrimaryDark = Color.parseColor("#000000"); customTheme.colorAccent = Color.parseColor("#FF4081"); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CustomThemeOptionsBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CustomThemeOptionsBottomSheetFragment.java new file mode 100644 index 00000000..7657bba9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CustomThemeOptionsBottomSheetFragment.java @@ -0,0 +1,92 @@ +package ml.docilealligator.infinityforreddit.Fragment; + +import android.app.Activity; +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 android.view.inputmethod.InputMethodManager; +import android.widget.EditText; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; +import ml.docilealligator.infinityforreddit.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class CustomThemeOptionsBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + public static final String EXTRA_THEME_NAME = "ETN"; + @BindView(R.id.theme_name_text_view_custom_theme_options_bottom_sheet_fragment) + TextView themeNameTextView; + @BindView(R.id.edit_theme_text_view_custom_theme_options_bottom_sheet_fragment) + TextView editThemeTextView; + @BindView(R.id.share_theme_text_view_custom_theme_options_bottom_sheet_fragment) + TextView shareThemeTextView; + @BindView(R.id.change_theme_name_text_view_custom_theme_options_bottom_sheet_fragment) + TextView changeThemeNameTextView; + @BindView(R.id.delete_theme_text_view_custom_theme_options_bottom_sheet_fragment) + TextView deleteTextView; + private String themeName; + private Activity activity; + + public CustomThemeOptionsBottomSheetFragment() { + // Required empty public constructor + } + + public interface CustomThemeOptionsBottomSheetFragmentListener { + void changeName(String oldName); + void delete(String name); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_custom_theme_options_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + themeName = getArguments().getString(EXTRA_THEME_NAME); + themeNameTextView.setText(themeName); + + editThemeTextView.setOnClickListener(view -> { + Intent intent = new Intent(activity, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, themeName); + startActivity(intent); + dismiss(); + }); + + shareThemeTextView.setOnClickListener(view -> { + dismiss(); + }); + + changeThemeNameTextView.setOnClickListener(view -> { + ((CustomThemeOptionsBottomSheetFragmentListener) activity).changeName(themeName); + dismiss(); + }); + + deleteTextView.setOnClickListener(view -> { + ((CustomThemeOptionsBottomSheetFragmentListener) activity).delete(themeName); + dismiss(); + }); + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (AppCompatActivity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SelectBaseThemeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SelectBaseThemeBottomSheetFragment.java new file mode 100644 index 00000000..81df06c5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SelectBaseThemeBottomSheetFragment.java @@ -0,0 +1,78 @@ +package ml.docilealligator.infinityforreddit.Fragment; + +import android.app.Activity; +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 android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; +import ml.docilealligator.infinityforreddit.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class SelectBaseThemeBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + @BindView(R.id.light_theme_text_view_select_base_theme_bottom_sheet_fragment) + TextView lightThemeTextView; + @BindView(R.id.dark_theme_text_view_select_base_theme_bottom_sheet_fragment) + TextView darkThemeTextView; + @BindView(R.id.amoled_theme_text_view_select_base_theme_bottom_sheet_fragment) + TextView amoledThemeTextView; + private Activity activity; + public SelectBaseThemeBottomSheetFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_select_base_theme_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + lightThemeTextView.setOnClickListener(view -> { + Intent intent = new Intent(activity, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_CREATE_THEME, true); + intent.putExtra(CustomizeThemeActivity.EXTRA_IS_PREDEFIINED_THEME, true); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, getString(R.string.theme_name_indigo)); + startActivity(intent); + dismiss(); + }); + + darkThemeTextView.setOnClickListener(view -> { + Intent intent = new Intent(activity, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_CREATE_THEME, true); + intent.putExtra(CustomizeThemeActivity.EXTRA_IS_PREDEFIINED_THEME, true); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, getString(R.string.theme_name_indigo_dark)); + startActivity(intent); + dismiss(); + }); + + amoledThemeTextView.setOnClickListener(view -> { + Intent intent = new Intent(activity, CustomizeThemeActivity.class); + intent.putExtra(CustomizeThemeActivity.EXTRA_CREATE_THEME, true); + intent.putExtra(CustomizeThemeActivity.EXTRA_IS_PREDEFIINED_THEME, true); + intent.putExtra(CustomizeThemeActivity.EXTRA_THEME_NAME, getString(R.string.theme_name_indigo_amoled)); + startActivity(intent); + dismiss(); + }); + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (Activity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java index 0447c7ba..d3c54782 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java @@ -137,6 +137,6 @@ public class ShareLinkBottomSheetFragment extends RoundedBottomSheetDialogFragme @Override public void onAttach(@NonNull Context context) { super.onAttach(context); - this.activity = (AppCompatActivity) context; + activity = (AppCompatActivity) context; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java index aa41849c..01c55179 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/ThemePreferenceFragment.java @@ -10,6 +10,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; @@ -21,10 +22,12 @@ import javax.inject.Inject; import ml.docilealligator.infinityforreddit.Activity.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -40,7 +43,10 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { private AppCompatActivity activity; @Inject + RedditDataRoomDatabase redditDataRoomDatabase; + @Inject CustomThemeWrapper customThemeWrapper; + public CustomThemeViewModel customThemeViewModel; @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { @@ -143,6 +149,40 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + customThemeViewModel = new ViewModelProvider(this, + new CustomThemeViewModel.Factory(redditDataRoomDatabase)) + .get(CustomThemeViewModel.class); + customThemeViewModel.getCurrentLightThemeLiveData().observe(this, customTheme -> { + if (customizeLightThemePreference != null) { + if (customTheme != null) { + customizeLightThemePreference.setVisible(true); + customizeLightThemePreference.setSummary(customTheme.name); + } else { + customizeLightThemePreference.setVisible(false); + } + } + }); + customThemeViewModel.getCurrentDarkThemeLiveData().observe(this, customTheme -> { + if (customizeDarkThemePreference != null) { + if (customTheme != null) { + customizeDarkThemePreference.setVisible(true); + customizeDarkThemePreference.setSummary(customTheme.name); + } else { + customizeDarkThemePreference.setVisible(false); + } + } + }); + customThemeViewModel.getCurrentAmoledThemeLiveData().observe(this, customTheme -> { + if (customizeAmoledThemePreference != null) { + if (customTheme != null) { + customizeAmoledThemePreference.setVisible(true); + customizeAmoledThemePreference.setSummary(customTheme.name); + } else { + customizeAmoledThemePreference.setVisible(false); + } + } + }); } @Override |