Skip to content

Commit

Permalink
make sort mode persistent ...
Browse files Browse the repository at this point in the history
streamline preferences handling
  • Loading branch information
edeso committed Aug 16, 2024
1 parent aab3a95 commit bc9cd87
Showing 1 changed file with 91 additions and 24 deletions.
115 changes: 91 additions & 24 deletions app/src/main/java/com/nkanaev/comics/fragment/LibraryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.*;
import android.provider.Settings;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.*;
import android.widget.*;
Expand Down Expand Up @@ -45,7 +50,9 @@

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;


Expand Down Expand Up @@ -116,6 +123,18 @@ public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().getCanonicalName(), "External Storage -> " + f.toString());
}

// restore saved sorting
int savedSortMode = MainApplication.getPreferences().getInt(
Constants.SETTINGS_LIBRARY_SORT,
Constants.SortMode.NAME_ASC.id);
mSort = R.id.sort_name_asc;
for (Constants.SortMode mode: Constants.SortMode.values()) {
if (savedSortMode != mode.id)
continue;
mSort = mode.resId;
break;
}

mDirectorySelectDialog = new DirectorySelectDialog(getActivity(), externalStorageFiles);
mDirectorySelectDialog.setCurrentDirectory(Environment.getExternalStorageDirectory());
mDirectorySelectDialog.setOnDirectorySelectListener(this);
Expand Down Expand Up @@ -267,6 +286,16 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
menu.findItem(item).setChecked(true);
}

private static final HashMap<Integer, List<Integer>> sortIds = new HashMap<>();
// sort menu entry ids (label, button ids with default first)
static {
sortIds.put(R.id.sort_name_label, Arrays.asList(new Integer[]{R.id.sort_name_asc, R.id.sort_name_desc}));
sortIds.put(R.id.sort_access_label, Arrays.asList(new Integer[]{R.id.sort_access_desc, R.id.sort_access_asc}));
sortIds.put(R.id.sort_size_label, Arrays.asList(new Integer[]{R.id.sort_size_asc, R.id.sort_size_desc}));
sortIds.put(R.id.sort_creation_label, Arrays.asList(new Integer[]{R.id.sort_creation_desc, R.id.sort_creation_asc}));
sortIds.put(R.id.sort_modified_label, Arrays.asList(new Integer[]{R.id.sort_modified_desc, R.id.sort_modified_asc}));
}

@SuppressLint("RestrictedApi")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Expand Down Expand Up @@ -335,23 +364,47 @@ public void run() {
// weirdly needed on preAPI21 to dismiss on tap outside
popupWindow.setBackgroundDrawable(new ColorDrawable(androidx.appcompat.R.attr.colorPrimary));

int[] ids = new int[]{
R.id.sort_modified_asc, R.id.sort_modified_desc,
R.id.sort_name_asc, R.id.sort_name_desc,
R.id.sort_size_asc, R.id.sort_size_desc,
R.id.sort_access_asc, R.id.sort_access_desc,
};
for (int id: ids ) {
ImageView v = popupView.findViewById(id);
int tint = id == mSort ? active : normal;
ImageViewCompat.setImageTintList(v, ColorStateList.valueOf(tint));
v.setOnClickListener(new View.OnClickListener() {
// add click listener/apply styling according to selected sort mode
for (int labelId : sortIds.keySet()) {
TextView tv = popupView.findViewById(labelId);
// attach clicklistener
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSortItemSelected(id);
onSortItemSelected(labelId);
popupWindow.dismiss();
}
});

List<Integer> buttonIds = sortIds.get(labelId);
// is it selected?
boolean label_active = buttonIds.contains(mSort);
int label_tint = label_active ? active : normal;
// reset formatting
CharSequence text = tv.getText();
SpannableString s = new SpannableString(text);
// style away
s.setSpan(new ForegroundColorSpan(label_tint), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new StyleSpan(label_active ? Typeface.BOLD : Typeface.NORMAL), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(s);

// handle buttons
for (int buttonId : buttonIds) {
ImageView iv = popupView.findViewById(buttonId);
// attach clicklistener
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSortItemSelected(buttonId);
popupWindow.dismiss();
}
});

// switch button colors, for buttons only
int tint = buttonId == mSort ? active : normal;
ImageViewCompat.setImageTintList(iv, ColorStateList.valueOf(tint));
}

}

float dp = getResources().getDisplayMetrics().density;
Expand All @@ -375,19 +428,32 @@ public void onClick(View v) {
}

public void onSortItemSelected(int id) {
if (BuildConfig.DEBUG)
if (false && BuildConfig.DEBUG)
Toast.makeText(
getActivity(),
"sort "+id,
"sort " + id,
Toast.LENGTH_SHORT).show();
mSort = id;
new Handler().post(new Runnable() {
@Override
public void run() {
sortContent();
mFolderListView.getAdapter().notifyDataSetChanged();
}
});

// filter label clicks
if (sortIds.containsKey(id)) {
int defaultId = sortIds.get(id).get(0);
int alternateId = sortIds.get(id).get(1);
mSort = mSort == defaultId ? alternateId : defaultId;
} else
mSort = id;

// save sortMode
for (Constants.SortMode mode: Constants.SortMode.values()) {
if (mSort != mode.resId)
continue;
SharedPreferences.Editor editor = MainApplication.getPreferences().edit();
editor.putInt(Constants.SETTINGS_LIBRARY_SORT, mode.id);
editor.apply();
break;
}

sortContent();
mFolderListView.getAdapter().notifyDataSetChanged();
}

@Override
Expand Down Expand Up @@ -476,6 +542,8 @@ private void sortContent(){
}

private void refreshLibrary( boolean finished ) {
if (mFolderListView == null) return;

if (!mIsRefreshPlanned || finished) {
final Runnable updateRunnable = new Runnable() {
@Override
Expand Down Expand Up @@ -514,8 +582,7 @@ private void setLoading(boolean isLoading) {
}

private String getLibraryDir() {
return getActivity()
.getSharedPreferences(Constants.SETTINGS_NAME, 0)
return MainApplication.getPreferences()
.getString(Constants.SETTINGS_LIBRARY_DIR, "");
}

Expand Down

0 comments on commit bc9cd87

Please sign in to comment.