Skip to content

Commit

Permalink
Persist catalog sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishiranu committed Nov 29, 2020
1 parent 43c5b1e commit 77df313
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 65 deletions.
45 changes: 45 additions & 0 deletions src/com/mishiranu/dashchan/content/Preferences.java
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -341,6 +342,50 @@ public static void setCaptchaSolvingChans(Collection<String> chanNames) {
}
}

public enum CatalogSort {
UNSORTED("unsorted", R.id.menu_unsorted, R.string.unsorted, null),
CREATED("created", R.id.menu_date_created, R.string.date_created,
(lhs, rhs) -> Long.compare(rhs.getTimestamp(), lhs.getTimestamp())),
REPLIES("replies", R.id.menu_replies, R.string.replies_count,
(lhs, rhs) -> Integer.compare(rhs.getThreadPostsCount(), lhs.getThreadPostsCount()));

private static final EnumValueProvider<CatalogSort> VALUE_PROVIDER = o -> o.value;

public interface Comparable {
long getTimestamp();
int getThreadPostsCount();
}

private final String value;
public final int menuItemId;
public final int titleResId;
public final Comparator<Comparable> comparator;

CatalogSort(String value, int menuItemId, int titleResId, Comparator<Comparable> comparator) {
this.value = value;
this.menuItemId = menuItemId;
this.titleResId = titleResId;
this.comparator = comparator;
}
}

public static final String KEY_CATALOG_SORT = "catalog_sort";
public static final CatalogSort DEFAULT_CATALOG_SORT = CatalogSort.UNSORTED;

public static CatalogSort getCatalogSort() {
return getEnumValue(KEY_CATALOG_SORT, CatalogSort.values(), DEFAULT_CATALOG_SORT, CatalogSort.VALUE_PROVIDER);
}

public static void setCatalogSort(CatalogSort catalogSort) {
SharedPreferences.Editor editor = PREFERENCES.edit();
if (catalogSort != null) {
editor.putString(KEY_CATALOG_SORT, catalogSort.value);
} else {
editor.remove(KEY_CATALOG_SORT);
}
editor.commit();
}

public static final String KEY_CHANS_ORDER = "chans_order";

public static ArrayList<String> getChansOrder() {
Expand Down
5 changes: 4 additions & 1 deletion src/com/mishiranu/dashchan/content/model/PostItem.java
Expand Up @@ -26,7 +26,8 @@
import java.util.Set;
import java.util.TreeSet;

public class PostItem implements AttachmentItem.Master, ChanMarkup.MarkupExtra, Comparable<PostItem> {
public class PostItem implements AttachmentItem.Master, ChanMarkup.MarkupExtra, Comparable<PostItem>,
Preferences.CatalogSort.Comparable {
public enum HideState {
UNDEFINED(false),
HIDDEN(true),
Expand Down Expand Up @@ -596,6 +597,7 @@ public GalleryItem.Set getThreadGallerySet() {
return threadData.gallerySet;
}

@Override
public int getThreadPostsCount() {
return threadData.base.postsCount;
}
Expand Down Expand Up @@ -653,6 +655,7 @@ public void formatThreadCardDescription(Resources resources, boolean repliesOnly
}
}

@Override
public long getTimestamp() {
return post.timestamp;
}
Expand Down
Expand Up @@ -9,7 +9,6 @@
import androidx.recyclerview.widget.RecyclerView;
import chan.content.Chan;
import chan.util.StringUtils;
import com.mishiranu.dashchan.R;
import com.mishiranu.dashchan.content.Preferences;
import com.mishiranu.dashchan.content.model.AttachmentItem;
import com.mishiranu.dashchan.content.model.GalleryItem;
Expand All @@ -30,24 +29,6 @@
public class ThreadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements GalleryItem.Provider {
public interface Callback extends ListViewUtils.SimpleCallback<PostItem> {}

public enum CatalogSort {
UNSORTED(R.id.menu_unsorted, R.string.unsorted, null),
DATE_CREATED(R.id.menu_date_created, R.string.date_created,
(lhs, rhs) -> Long.compare(rhs.getTimestamp(), lhs.getTimestamp())),
REPLIES(R.id.menu_replies, R.string.replies_count,
(lhs, rhs) -> Integer.compare(rhs.getThreadPostsCount(), lhs.getThreadPostsCount()));

public final int menuItemId;
public final int titleResId;
private final Comparator<PostItem> comparator;

CatalogSort(int menuItemId, int titleResId, Comparator<PostItem> comparator) {
this.menuItemId = menuItemId;
this.titleResId = titleResId;
this.comparator = comparator;
}
}

private static final int LIST_PADDING = 12;
private static final int CARD_MIN_WIDTH_LARGE_DP = 120;
private static final int CARD_MIN_WIDTH_SMALL_DP = 90;
Expand Down Expand Up @@ -77,18 +58,17 @@ private GridMode(int columns, boolean small, int gridItemContentHeight) {
private final UiManager.ConfigurationSet configurationSet;

private String filterText;
private CatalogSort catalogSort;
private Preferences.CatalogSort catalogSort = Preferences.CatalogSort.UNSORTED;
private boolean cardsMode;
private GridMode gridMode;

public ThreadsAdapter(Context context, Callback callback, String chanName, UiManager uiManager,
UiManager.PostStateProvider postStateProvider, FragmentManager fragmentManager, CatalogSort catalogSort) {
UiManager.PostStateProvider postStateProvider, FragmentManager fragmentManager) {
this.context = context;
this.uiManager = uiManager;
configurationSet = new UiManager.ConfigurationSet(chanName, null, null, postStateProvider,
this, fragmentManager, uiManager.dialog().createStackInstance(), null, callback,
false, false, false, false, false, null);
this.catalogSort = catalogSort;
}

@NonNull
Expand Down Expand Up @@ -264,7 +244,7 @@ public void applyFilter(String text) {
}
}

public void setCatalogSort(CatalogSort catalogSort) {
public void setCatalogSort(Preferences.CatalogSort catalogSort) {
if (this.catalogSort != catalogSort) {
this.catalogSort = catalogSort;
if (catalog) {
Expand All @@ -276,7 +256,8 @@ public void setCatalogSort(CatalogSort catalogSort) {

private void applyCurrentSortingAndFilter(boolean sorting, boolean filter) {
if (sorting) {
Comparator<PostItem> comparator = catalogSort != null ? catalogSort.comparator : null;
Comparator<Preferences.CatalogSort.Comparable> comparator =
catalogSort != null ? catalogSort.comparator : null;
if (catalog && comparator != null) {
if (catalogSortedPostItems == null) {
catalogSortedPostItems = new ArrayList<>(postItems);
Expand Down
46 changes: 6 additions & 40 deletions src/com/mishiranu/dashchan/ui/navigator/page/ThreadsPage.java
Expand Up @@ -4,8 +4,6 @@
import android.content.Context;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -76,36 +74,6 @@ public void clear() {
}
}

private static class ParcelableExtra implements Parcelable {
public static final ExtraFactory<ParcelableExtra> FACTORY = ParcelableExtra::new;

public ThreadsAdapter.CatalogSort catalogSort = ThreadsAdapter.CatalogSort.UNSORTED;

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(catalogSort.name());
}

public static final Creator<ParcelableExtra> CREATOR = new Creator<ParcelableExtra>() {
@Override
public ParcelableExtra createFromParcel(Parcel source) {
ParcelableExtra parcelableExtra = new ParcelableExtra();
parcelableExtra.catalogSort = ThreadsAdapter.CatalogSort.valueOf(source.readString());
return parcelableExtra;
}

@Override
public ParcelableExtra[] newArray(int size) {
return new ParcelableExtra[size];
}
};
}

public static class ReadViewModel extends TaskViewModel.Proxy<ReadThreadsTask, ReadThreadsTask.Callback> {}

private HidePerformer hidePerformer;
Expand Down Expand Up @@ -145,11 +113,10 @@ protected void onCreate() {
Chan chan = getChan();
hidePerformer = new HidePerformer(context);
RetainableExtra retainableExtra = getRetainableExtra(RetainableExtra.FACTORY);
ParcelableExtra parcelableExtra = getParcelableExtra(ParcelableExtra.FACTORY);
UiManager uiManager = getUiManager();
uiManager.view().bindThreadsPostRecyclerView(recyclerView);
ThreadsAdapter adapter = new ThreadsAdapter(context, this, page.chanName, uiManager,
postStateProvider, getFragmentManager(), parcelableExtra.catalogSort);
postStateProvider, getFragmentManager());
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
Expand All @@ -163,6 +130,7 @@ public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull R
recyclerView.getPullable().setPullSides(PullableWrapper.Side.BOTH);
uiManager.observable().register(this);
layoutManager.setSpanCount(adapter.setThreadsView(Preferences.getThreadsView()));
adapter.setCatalogSort(Preferences.getCatalogSort());
adapter.applyFilter(getInitSearch().currentQuery);
FavoritesStorage.getInstance().getObservable().register(this);

Expand Down Expand Up @@ -339,7 +307,7 @@ public void onCreateOptionsMenu(Menu menu) {
menu.add(0, R.id.menu_catalog, 0, R.string.catalog);
menu.add(0, R.id.menu_pages, 0, R.string.pages);
SubMenu sorting = menu.addSubMenu(0, R.id.menu_sorting, 0, R.string.sorting);
for (ThreadsAdapter.CatalogSort catalogSort : ThreadsAdapter.CatalogSort.values()) {
for (Preferences.CatalogSort catalogSort : Preferences.CatalogSort.values()) {
sorting.add(R.id.menu_sorting, catalogSort.menuItemId, 0, catalogSort.titleResId);
}
sorting.setGroupCheckable(R.id.menu_sorting, true, true);
Expand Down Expand Up @@ -367,7 +335,6 @@ public void onCreateOptionsMenu(Menu menu) {
public void onPrepareOptionsMenu(Menu menu) {
Page page = getPage();
RetainableExtra retainableExtra = getRetainableExtra(RetainableExtra.FACTORY);
ParcelableExtra parcelableExtra = getParcelableExtra(ParcelableExtra.FACTORY);
Chan chan = getChan();
ChanConfiguration.Board board = chan.configuration.safe().obtainBoard(page.boardName);
boolean search = board.allowSearch;
Expand All @@ -380,7 +347,7 @@ public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_catalog).setVisible(catalog && !isCatalogOpen);
menu.findItem(R.id.menu_pages).setVisible(catalog && isCatalogOpen);
menu.findItem(R.id.menu_sorting).setVisible(catalog && isCatalogOpen);
menu.findItem(parcelableExtra.catalogSort.menuItemId).setChecked(true);
menu.findItem(Preferences.getCatalogSort().menuItemId).setChecked(true);
menu.findItem(R.id.menu_archive).setVisible(board.allowArchive);
menu.findItem(R.id.menu_new_thread).setVisible(board.allowPosting);
menu.findItem(Preferences.getThreadsView().menuItemId).setChecked(true);
Expand Down Expand Up @@ -439,10 +406,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
for (ThreadsAdapter.CatalogSort catalogSort : ThreadsAdapter.CatalogSort.values()) {
for (Preferences.CatalogSort catalogSort : Preferences.CatalogSort.values()) {
if (item.getItemId() == catalogSort.menuItemId) {
ParcelableExtra parcelableExtra = getParcelableExtra(ParcelableExtra.FACTORY);
parcelableExtra.catalogSort = catalogSort;
Preferences.setCatalogSort(catalogSort);
getAdapter().setCatalogSort(catalogSort);
return true;
}
Expand Down

0 comments on commit 77df313

Please sign in to comment.