Skip to content

Commit

Permalink
Be able to hide memes (fixes #14) by @a-u-bello (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
aumarbello authored and gsantner committed Oct 21, 2017
1 parent 63add40 commit 7785dca
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@
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;
public static final String IMAGE_PATH = "imagePath";
public static final String IMAGE_POS = "image_pos";

private static boolean _isShowingFullscreenImage = false;
private boolean _areTabsReady = false;

@BindView(R.id.toolbar)
Toolbar _toolbar;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -220,6 +219,22 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

public void updateHiddenNavOption() {
MenuItem hiddenItem = _navigationView.getMenu().findItem(R.id.action_mode_hidden);

List<MemeData.Image> 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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -584,7 +618,7 @@ public void onReceive(Context context, Intent intent) {
}
case AppCast.ASSETS_LOADED.ACTION: {
selectTab(_tabLayout.getSelectedTabPosition(), _currentMainMode);
return;
updateHiddenNavOption();
}
}
}
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public void onCreate(Bundle savedInstanceState) {
_tabPos = getArguments().getInt("pos");

_imageList = new ArrayList<>();

_app.settings.setLastSelectedTab(_tabPos);
}

private void reloadAdapter() {
Expand All @@ -87,6 +85,14 @@ private void reloadAdapter() {
if (_app.settings.isShuffleTagLists()) {
Collections.shuffle(_imageList);
}

List<MemeData.Image> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -138,14 +135,20 @@ 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);
if (dataImage.isTemplate) {
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);
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_remove_circle_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:alpha="0.80" android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM17,13L7,13v-2h10v2z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/main__drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
android:id="@+id/action_mode_favs"
android:icon="@drawable/ic_star_black_32dp"
android:title="@string/memelist_data_mode__favs" />

<item
android:id="@+id/action_mode_saved"
android:icon="@drawable/ic_save_white_48px"
android:title="@string/memelist_data_mode__saved" />

<item
android:id="@+id/action_mode_hidden"
android:icon="@drawable/ic_remove_circle_black_24dp"
android:title="@string/memelist_data_mode__hidden" />
</group>

<group
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/menu/meme_options__menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/meme_action_fav"
android:title="@string/meme_options__favourite"/>
<item android:id="@+id/meme_action_title"
android:title="@string/meme_options__title"/>
<item android:id="@+id/meme_action_hide"
android:title="@string/meme_options__hide"/>
</menu>
Loading

0 comments on commit 7785dca

Please sign in to comment.