From 7785dca5c4b46ed196b4494821dfd1febc9affc9 Mon Sep 17 00:00:00 2001 From: Ahmed Umar Date: Sat, 21 Oct 2017 18:41:49 +0100 Subject: [PATCH] Be able to hide memes (fixes #14) by @a-u-bello (#72) --- .../memetastic/activity/MainActivity.java | 66 +++++++++++-- .../memetastic/activity/MemeFragment.java | 10 +- .../memetastic/ui/MemeItemAdapter.java | 99 +++++++++++++++++-- .../gsantner/memetastic/util/AppSettings.java | 52 ++++++++++ .../drawable/ic_remove_circle_black_24dp.xml | 5 + app/src/main/res/menu/main__drawer.xml | 6 ++ app/src/main/res/menu/meme_options__menu.xml | 9 ++ .../res/values/strings-not_translatable.xml | 1 + app/src/main/res/values/strings.xml | 7 ++ 9 files changed, 238 insertions(+), 17 deletions(-) create mode 100644 app/src/main/res/drawable/ic_remove_circle_black_24dp.xml create mode 100644 app/src/main/res/menu/meme_options__menu.xml diff --git a/app/src/main/java/io/github/gsantner/memetastic/activity/MainActivity.java b/app/src/main/java/io/github/gsantner/memetastic/activity/MainActivity.java index 3e32ebb7e..26da2aa1f 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/activity/MainActivity.java +++ b/app/src/main/java/io/github/gsantner/memetastic/activity/MainActivity.java @@ -72,7 +72,8 @@ import io.github.gsantner.memetastic.util.PermissionChecker; public class MainActivity extends AppCompatActivity - implements NavigationView.OnNavigationItemSelectedListener { + implements NavigationView.OnNavigationItemSelectedListener, + ViewPager.OnPageChangeListener { public static final int REQUEST_LOAD_GALLERY_IMAGE = 50; public static final int REQUEST_TAKE_CAMERA_PICTURE = 51; public static final int REQUEST_SHOW_IMAGE = 52; @@ -80,7 +81,6 @@ public class MainActivity extends AppCompatActivity public static final String IMAGE_POS = "image_pos"; private static boolean _isShowingFullscreenImage = false; - private boolean _areTabsReady = false; @BindView(R.id.toolbar) Toolbar _toolbar; @@ -145,7 +145,8 @@ protected void onCreate(Bundle savedInstanceState) { // Setup _toolbar setSupportActionBar(_toolbar); - ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, _drawer, _toolbar, R.string.main__navdrawer__open, R.string.main__navdrawer__close); + ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, _drawer, _toolbar, + R.string.main__navdrawer__open, R.string.main__navdrawer__close); _drawer.addDrawerListener(toggle); toggle.syncState(); _navigationView.setNavigationItemSelectedListener(this); @@ -177,13 +178,11 @@ protected void onCreate(Bundle savedInstanceState) { tab.setText(cat); _tabLayout.addTab(tab); } - _areTabsReady = true; _viewPager.setAdapter(new MemePagerAdapter(getSupportFragmentManager(), _tagKeys.length, _tagValues)); _tabLayout.setupWithViewPager(_viewPager); - selectTab(app.settings.getLastSelectedTab(), app.settings.getDefaultMainMode()); _infoBarProgressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(this, R.color.accent), PorterDuff.Mode.SRC_IN); @@ -220,6 +219,22 @@ protected void onCreate(Bundle savedInstanceState) { } } + public void updateHiddenNavOption() { + MenuItem hiddenItem = _navigationView.getMenu().findItem(R.id.action_mode_hidden); + + List imageList = new ArrayList<>(); + + for (String hidden : app.settings.getHiddenMemesTemplate()) { + MemeData.Image image = MemeData.findImage(new File(hidden)); + if (image != null) { + imageList.add(image); + break; + } + } + + hiddenItem.setVisible(!imageList.isEmpty()); + } + @SuppressWarnings("ConstantConditions") private void selectTab(int pos, int mainMode) { MenuItem navItem = null; @@ -235,6 +250,9 @@ private void selectTab(int pos, int mainMode) { case 2: navItem = _navigationView.getMenu().findItem(R.id.action_mode_saved); break; + case 3: + navItem = _navigationView.getMenu().findItem(R.id.action_mode_hidden); + break; } if (navItem != null) { @@ -269,12 +287,14 @@ protected void onResume() { } } catch (Exception ignored) { } + _viewPager.addOnPageChangeListener(this); } @Override protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiver(_localBroadcastReceiver); + _viewPager.removeOnPageChangeListener(this); } public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { @@ -373,6 +393,20 @@ public boolean handleBarClick(MenuItem item) { _toolbar.setTitle(R.string.memelist_data_mode__saved); break; } + + case R.id.action_mode_hidden:{ + _currentMainMode = 3; + imageList = new ArrayList<>(); + + for (String hidden : app.settings.getHiddenMemesTemplate()) { + MemeData.Image image = MemeData.findImage(new File(hidden)); + if (image != null) { + imageList.add(image); + } + } + _toolbar.setTitle(R.string.memelist_data_mode__hidden); + break; + } } // Change mode @@ -584,7 +618,7 @@ public void onReceive(Context context, Intent intent) { } case AppCast.ASSETS_LOADED.ACTION: { selectTab(_tabLayout.getSelectedTabPosition(), _currentMainMode); - return; + updateHiddenNavOption(); } } } @@ -687,4 +721,24 @@ public boolean onOptionsItemSelected(MenuItem item) { return handleBarClick(item); } + @Override + public void onPageScrolled(int i, float v, int i1) { + + } + + @Override + public void onPageSelected(int i) { + app.settings.setLastSelectedTab(i); + } + + @Override + public void onPageScrollStateChanged(int i) { + + } + + public void swapTabs() { + MenuItem createItem = _navigationView.getMenu().findItem(R.id.action_mode_create); + onNavigationItemSelected(createItem); + createItem.setChecked(true); + } } diff --git a/app/src/main/java/io/github/gsantner/memetastic/activity/MemeFragment.java b/app/src/main/java/io/github/gsantner/memetastic/activity/MemeFragment.java index b9d0ea7cf..0e7db0e2d 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/activity/MemeFragment.java +++ b/app/src/main/java/io/github/gsantner/memetastic/activity/MemeFragment.java @@ -73,8 +73,6 @@ public void onCreate(Bundle savedInstanceState) { _tabPos = getArguments().getInt("pos"); _imageList = new ArrayList<>(); - - _app.settings.setLastSelectedTab(_tabPos); } private void reloadAdapter() { @@ -87,6 +85,14 @@ private void reloadAdapter() { if (_app.settings.isShuffleTagLists()) { Collections.shuffle(_imageList); } + + List hiddenImages = new ArrayList<>(); + for (MemeData.Image image : _imageList) { + if (_app.settings.isHidden(image.fullPath.getAbsolutePath())){ + hiddenImages.add(image); + } + } + _imageList.removeAll(hiddenImages); _recyclerMemeAdapter.setOriginalImageDataList(_imageList); _recyclerMemeAdapter.notifyDataSetChanged(); setRecyclerMemeListAdapter(_recyclerMemeAdapter); diff --git a/app/src/main/java/io/github/gsantner/memetastic/ui/MemeItemAdapter.java b/app/src/main/java/io/github/gsantner/memetastic/ui/MemeItemAdapter.java index a66656b48..43c23fba9 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/ui/MemeItemAdapter.java +++ b/app/src/main/java/io/github/gsantner/memetastic/ui/MemeItemAdapter.java @@ -3,8 +3,11 @@ import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; +import android.support.v7.widget.PopupMenu; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -91,16 +94,10 @@ public void onBindViewHolder(final ViewHolder holder, final int pos) { holder.imageView.setTag(imageData); holder.imageButtonFav.setTag(imageData); - tintFavouriteImage(holder.imageButtonFav, _app.settings.isFavorite(imageData.fullPath.toString())); - holder.imageView.setOnLongClickListener(new View.OnLongClickListener() { - public boolean onLongClick(View v) { - MemeData.Image image = (MemeData.Image) v.getTag(); - Toast.makeText(v.getContext(), image.conf.getTitle(), Toast.LENGTH_SHORT).show(); - return true; - } - }); + setViewLongClickListener(holder, false); + holder.imageButtonFav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -138,7 +135,7 @@ public int getItemCount() { } @Override - public void onImageLoaded(Bitmap bitmap, ViewHolder holder) { + public void onImageLoaded(Bitmap bitmap, final ViewHolder holder) { MemeData.Image dataImage = (MemeData.Image) holder.imageView.getTag(); Animation animation = AnimationUtils.loadAnimation(_activity, R.anim.fadeinfast); holder.imageView.startAnimation(animation); @@ -146,6 +143,12 @@ public void onImageLoaded(Bitmap bitmap, ViewHolder holder) { holder.imageButtonFav.startAnimation(animation); holder.imageButtonFav.setVisibility(View.VISIBLE); } + + if (_app.settings.isHidden(dataImage.fullPath.getAbsolutePath())){ + holder.imageButtonFav.setVisibility(View.INVISIBLE); + holder.imageView.setOnClickListener(null); + setViewLongClickListener(holder, true); + } holder.imageView.setImageBitmap(bitmap); holder.imageView.setVisibility(View.VISIBLE); } @@ -166,6 +169,84 @@ private void toggleFavorite(ViewHolder holder) { } } + private void setViewLongClickListener(final ViewHolder holder, boolean isHidden){ + final PopupMenu menu = new PopupMenu(_activity, holder.imageView); + menu.inflate(R.menu.meme_options__menu); + + menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem menuItem) { + switch (menuItem.getItemId()){ + case R.id.meme_action_fav: + toggleFavorite(holder); + return true; + case R.id.meme_action_hide: + int position = holder.getAdapterPosition(); + toggleHidden(holder, position); + ((MainActivity)_activity).updateHiddenNavOption(); + return true; + case R.id.meme_action_title: + showTitleToast(menuItem.getActionView()); + return true; + } + return false; + } + }); + + View longClickView; + + switch (_itemViewType) { + case VIEW_TYPE__ROWS_WITH_TITLE: { + longClickView = holder.itemView; + break; + } + + case VIEW_TYPE__PICTURE_GRID: + default: { + longClickView = holder.imageView; + break; + } + } + + if (isHidden){ + longClickView.setOnLongClickListener(new View.OnLongClickListener() { + public boolean onLongClick(final View v) { + Menu itemMenu = menu.getMenu(); + + itemMenu.findItem(R.id.meme_action_hide).setTitle(R.string.unhide); + itemMenu.removeItem(R.id.meme_action_fav); + menu.show(); + return true; + } + }); + }else { + longClickView.setOnLongClickListener(new View.OnLongClickListener() { + public boolean onLongClick(final View v) { + menu.show(); + return true; + } + }); + } + } + + private void toggleHidden(ViewHolder holder, int position){ + MemeData.Image image = (MemeData.Image) holder.imageView.getTag(); + String filePath = image.fullPath.getAbsolutePath(); + + _app.settings.toggleHiddenMeme(filePath); + _imageDataList.remove(image); + notifyItemRemoved(position); + + if (_imageDataList.isEmpty()){ + ((MainActivity)_activity).swapTabs(); + } + } + + private void showTitleToast(View v){ + MemeData.Image image = (MemeData.Image) v.getTag(); + Toast.makeText(v.getContext(), image.conf.getTitle(), Toast.LENGTH_SHORT).show(); + } + private void tintFavouriteImage(ImageView iv, boolean isFav) { ContextUtils.setDrawableWithColorToImageView(iv, isFav ? R.drawable.ic_star_black_32dp : R.drawable.ic_star_border_black_32dp, diff --git a/app/src/main/java/io/github/gsantner/memetastic/util/AppSettings.java b/app/src/main/java/io/github/gsantner/memetastic/util/AppSettings.java index 8ac337946..23c2415fa 100644 --- a/app/src/main/java/io/github/gsantner/memetastic/util/AppSettings.java +++ b/app/src/main/java/io/github/gsantner/memetastic/util/AppSettings.java @@ -142,6 +142,58 @@ public void removeFavorite(String filepath) { setFavoriteMemes(newFavs.toArray(new String[newFavs.size()])); } + private void setHiddenMemes(String[] hiddenMemes){ + setStringArray(R.string.pref_key__hidden_meme_templates, hiddenMemes); + } + + public String[] getHiddenMemesTemplate(){ + return getStringArray(R.string.pref_key__hidden_meme_templates); + } + + private void appendHiddenMeme(String filepath){ + String[] hiddenMeme = insertAndMaximize(getHiddenMemesTemplate(), + filepath, MAX_FAVS); + setHiddenMemes(hiddenMeme); + + } + + public boolean isHidden(String filePath){ + String[] hiddenMemes = getHiddenMemesTemplate(); + + if (hiddenMemes == null) + return false; + + for (String hiddenPath : hiddenMemes) { + if (filePath.equals(hiddenPath)) + return true; + } + return false; + } + + public boolean toggleHiddenMeme(String filePath){ + if (!isHidden(filePath)){ + appendHiddenMeme(filePath); + return true; + } + + removeHiddenMeme(filePath); + return false; + } + + private void removeHiddenMeme(String filePath){ + String[] hiddenMeme = getHiddenMemesTemplate(); + + List newHiddenMemes = new ArrayList<>(); + + for (String hiddenPath :hiddenMeme) { + if (!hiddenPath.equals(filePath)){ + newHiddenMemes.add(hiddenPath); + } + } + + setHiddenMemes(newHiddenMemes.toArray(new String[newHiddenMemes.size()])); + } + public void setLastSelectedTab(int value) { setInt(R.string.pref_key__last_selected_tab, value); } diff --git a/app/src/main/res/drawable/ic_remove_circle_black_24dp.xml b/app/src/main/res/drawable/ic_remove_circle_black_24dp.xml new file mode 100644 index 000000000..732527669 --- /dev/null +++ b/app/src/main/res/drawable/ic_remove_circle_black_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/menu/main__drawer.xml b/app/src/main/res/menu/main__drawer.xml index 2e5f5ce2d..076900a7f 100644 --- a/app/src/main/res/menu/main__drawer.xml +++ b/app/src/main/res/menu/main__drawer.xml @@ -14,10 +14,16 @@ android:id="@+id/action_mode_favs" android:icon="@drawable/ic_star_black_32dp" android:title="@string/memelist_data_mode__favs" /> + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings-not_translatable.xml b/app/src/main/res/values/strings-not_translatable.xml index 459673d0c..04d4d265c 100644 --- a/app/src/main/res/values/strings-not_translatable.xml +++ b/app/src/main/res/values/strings-not_translatable.xml @@ -71,5 +71,6 @@ pref_key__download_assets_try pref_key__memelist_view_type pref_key__is_show_in_gallery + pref_key__hidden_meme_templates diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7a731aff2..e8e78d474 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,6 +18,7 @@ Saved Favs Create + Hidden Rows with title Picture Grid @@ -117,4 +118,10 @@ Select color Code + + + Favourite + Title + Hide + Unhide