diff --git a/src/org/thoughtcrime/securesms/giph/ui/GiphyActivity.java b/src/org/thoughtcrime/securesms/giph/ui/GiphyActivity.java index 18a7a36319..e28b694343 100644 --- a/src/org/thoughtcrime/securesms/giph/ui/GiphyActivity.java +++ b/src/org/thoughtcrime/securesms/giph/ui/GiphyActivity.java @@ -69,6 +69,7 @@ public class GiphyActivity extends PassphraseRequiredActionBarActivity GiphyActivityToolbar toolbar = ViewUtil.findById(this, R.id.giphy_toolbar); toolbar.setOnFilterChangedListener(this); toolbar.setOnLayoutChangedListener(this); + toolbar.setPersistence(GiphyActivityToolbarTextSecurePreferencesPersistence.fromContext(this)); setSupportActionBar(toolbar); @@ -99,9 +100,9 @@ public class GiphyActivity extends PassphraseRequiredActionBarActivity } @Override - public void onLayoutChanged(int type) { - this.gifFragment.setLayoutManager(type); - this.stickerFragment.setLayoutManager(type); + public void onLayoutChanged(boolean gridLayout) { + gifFragment.setLayoutManager(gridLayout); + stickerFragment.setLayoutManager(gridLayout); } @SuppressLint("StaticFieldLeak") diff --git a/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbar.java b/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbar.java index 669c824bf5..59a2f58f7b 100644 --- a/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbar.java +++ b/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbar.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.giph.ui; import android.content.Context; import android.graphics.Rect; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.Toolbar; import android.text.Editable; @@ -21,7 +22,6 @@ import android.widget.TextView; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.components.AnimatingToggle; -import org.thoughtcrime.securesms.util.ViewUtil; public class GiphyActivityToolbar extends Toolbar { @@ -31,10 +31,11 @@ public class GiphyActivityToolbar extends Toolbar { private EditText searchText; private AnimatingToggle toggle; private ImageView action; - private ImageView listToggle; - private ImageView gridToggle; private ImageView clearToggle; private LinearLayout toggleContainer; + private View listLayoutToggle; + private View gridLayoutToggle; + private Persistence persistence; public GiphyActivityToolbar(Context context) { this(context, null); @@ -48,29 +49,15 @@ public class GiphyActivityToolbar extends Toolbar { super(context, attrs, defStyleAttr); inflate(context, R.layout.giphy_activity_toolbar, this); - this.action = ViewUtil.findById(this, R.id.action_icon); - this.searchText = ViewUtil.findById(this, R.id.search_view); - this.toggle = ViewUtil.findById(this, R.id.button_toggle); - this.listToggle = ViewUtil.findById(this, R.id.view_stream); - this.gridToggle = ViewUtil.findById(this, R.id.view_grid); - this.clearToggle = ViewUtil.findById(this, R.id.search_clear); - this.toggleContainer = ViewUtil.findById(this, R.id.toggle_container); + this.action = findViewById(R.id.action_icon); + this.searchText = findViewById(R.id.search_view); + this.toggle = findViewById(R.id.button_toggle); + this.clearToggle = findViewById(R.id.search_clear); + this.toggleContainer = findViewById(R.id.toggle_container); + this.listLayoutToggle = findViewById(R.id.view_stream); + this.gridLayoutToggle = findViewById(R.id.view_grid); - this.listToggle.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - displayTogglingView(gridToggle); - if (layoutListener != null) layoutListener.onLayoutChanged(OnLayoutChangedListener.LAYOUT_LIST); - } - }); - - this.gridToggle.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - displayTogglingView(listToggle); - if (layoutListener != null) layoutListener.onLayoutChanged(OnLayoutChangedListener.LAYOUT_GRID); - } - }); + setupGridLayoutToggles(); this.clearToggle.setOnClickListener(new View.OnClickListener() { @Override @@ -116,7 +103,29 @@ public class GiphyActivityToolbar extends Toolbar { setNavigationIcon(null); setContentInsetStartWithNavigation(0); expandTapArea(this, action); - expandTapArea(toggleContainer, gridToggle); + } + + public void setPersistence(@NonNull Persistence persistence) { + this.persistence = persistence; + displayTogglingView(persistence.getGridSelected() ? listLayoutToggle : gridLayoutToggle); + } + + private void setupGridLayoutToggles() { + setUpGridToggle(listLayoutToggle, gridLayoutToggle, false); + setUpGridToggle(gridLayoutToggle, listLayoutToggle, true); + displayTogglingView(gridLayoutToggle); + } + + private void setUpGridToggle(View gridToggle, View otherToggle, boolean gridLayout) { + gridToggle.setOnClickListener(v -> { + displayTogglingView(otherToggle); + if (layoutListener != null) { + layoutListener.onLayoutChanged(gridLayout); + } + if (persistence != null) { + persistence.setGridSelected(gridLayout); + } + }); } @Override @@ -184,10 +193,11 @@ public class GiphyActivityToolbar extends Toolbar { } public interface OnLayoutChangedListener { - public static final int LAYOUT_GRID = 1; - public static final int LAYOUT_LIST = 2; - void onLayoutChanged(int type); + void onLayoutChanged(boolean gridLayout); } - + public interface Persistence { + boolean getGridSelected(); + void setGridSelected(boolean isGridSelected); + } } diff --git a/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbarTextSecurePreferencesPersistence.java b/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbarTextSecurePreferencesPersistence.java new file mode 100644 index 0000000000..bc3c87cdc6 --- /dev/null +++ b/src/org/thoughtcrime/securesms/giph/ui/GiphyActivityToolbarTextSecurePreferencesPersistence.java @@ -0,0 +1,28 @@ +package org.thoughtcrime.securesms.giph.ui; + +import android.content.Context; + +import org.thoughtcrime.securesms.util.TextSecurePreferences; + +class GiphyActivityToolbarTextSecurePreferencesPersistence implements GiphyActivityToolbar.Persistence { + + static GiphyActivityToolbar.Persistence fromContext(Context context) { + return new GiphyActivityToolbarTextSecurePreferencesPersistence(context.getApplicationContext()); + } + + private final Context context; + + private GiphyActivityToolbarTextSecurePreferencesPersistence(Context context) { + this.context = context; + } + + @Override + public boolean getGridSelected() { + return TextSecurePreferences.isGifSearchInGridLayout(context); + } + + @Override + public void setGridSelected(boolean isGridSelected) { + TextSecurePreferences.setIsGifSearchInGridLayout(context, isGridSelected); + } +} diff --git a/src/org/thoughtcrime/securesms/giph/ui/GiphyFragment.java b/src/org/thoughtcrime/securesms/giph/ui/GiphyFragment.java index 53c89a8ead..9ce6440866 100644 --- a/src/org/thoughtcrime/securesms/giph/ui/GiphyFragment.java +++ b/src/org/thoughtcrime/securesms/giph/ui/GiphyFragment.java @@ -22,6 +22,7 @@ import org.thoughtcrime.securesms.giph.model.GiphyImage; import org.thoughtcrime.securesms.giph.net.GiphyLoader; import org.thoughtcrime.securesms.giph.util.InfiniteScrollListener; import org.thoughtcrime.securesms.mms.GlideApp; +import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.ViewUtil; import java.util.LinkedList; @@ -56,7 +57,7 @@ public abstract class GiphyFragment extends Fragment implements LoaderManager.Lo this.giphyAdapter = new GiphyAdapter(getActivity(), GlideApp.with(this), new LinkedList<>()); this.giphyAdapter.setListener(this); - this.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + setLayoutManager(TextSecurePreferences.isGifSearchInGridLayout(getContext())); this.recyclerView.setItemAnimator(new DefaultItemAnimator()); this.recyclerView.setAdapter(giphyAdapter); this.recyclerView.addOnScrollListener(new GiphyScrollListener()); @@ -80,12 +81,13 @@ public abstract class GiphyFragment extends Fragment implements LoaderManager.Lo this.giphyAdapter.setImages(new LinkedList()); } - public void setLayoutManager(int type) { - if (type == GiphyActivityToolbar.OnLayoutChangedListener.LAYOUT_GRID) { - this.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); - } else { - this.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); - } + public void setLayoutManager(boolean gridLayout) { + recyclerView.setLayoutManager(getLayoutManager(gridLayout)); + } + + private RecyclerView.LayoutManager getLayoutManager(boolean gridLayout) { + return gridLayout ? new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) + : new LinearLayoutManager(getActivity()); } public void setClickListener(GiphyAdapter.OnItemClickListener listener) { diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 7ebc912774..34dca9038c 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -177,6 +177,8 @@ public class TextSecurePreferences { public static final String LINK_PREVIEWS = "pref_link_previews"; + private static final String GIF_GRID_LAYOUT = "pref_gif_grid_layout"; + public static boolean isScreenLockEnabled(@NonNull Context context) { return getBooleanPreference(context, SCREEN_LOCK, false); } @@ -362,6 +364,14 @@ public class TextSecurePreferences { return getBooleanPreference(context, LINK_PREVIEWS, true); } + public static boolean isGifSearchInGridLayout(Context context) { + return getBooleanPreference(context, GIF_GRID_LAYOUT, false); + } + + public static void setIsGifSearchInGridLayout(Context context, boolean isGrid) { + setBooleanPreference(context, GIF_GRID_LAYOUT, isGrid); + } + public static @Nullable String getProfileKey(Context context) { return getStringPreference(context, PROFILE_KEY_PREF, null); }