Skip to content

Commit

Permalink
Refresh shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
kaaholst committed Oct 13, 2024
1 parent ac583b8 commit 1e78705
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 505 deletions.
23 changes: 10 additions & 13 deletions Squeezer/src/main/java/uk/org/ngo/squeezer/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,15 @@ public void setArchivedMenuItems(List<String> list, Player player) {
editor.apply();
}

public void saveShortcuts(Map<String, Object> map) {
public void saveShortcuts(List<JiveItem> shortcuts) {
Map<String, Object> map = convertShortcuts(shortcuts);
SharedPreferences.Editor editor = sharedPreferences.edit();
JSONObject json = new JSONObject(map);
editor.putString(CUSTOM_SHORTCUTS, json.toString());
editor.apply();
}

// TODO If possible, remove this or CustomJiveItemHandling.convertShortcuts()
public Map<String, Object> convertShortcuts(List<JiveItem> customShortcuts) {
private Map<String, Object> convertShortcuts(List<JiveItem> customShortcuts) {
Map<String, Object> map = new HashMap<>();
for (JiveItem item : customShortcuts) {
map.put(item.getName(), item.getRecord());
Expand All @@ -790,28 +790,25 @@ public Map<String, Object> convertShortcuts(List<JiveItem> customShortcuts) {
* Return a map of names (keys) of shortcuts with value: Map<String, Object> which is a record
* and can be used as such when generating JiveItems
*/
public HashMap<String, Map<String, Object>> restoreCustomShortcuts() {
HashMap<String, Map<String, Object>> allShortcutsFromPref = new HashMap<>();
public List<Map<String, Object>> getCustomShortcuts() {
List<Map<String, Object>> allShortcutsFromPref = new ArrayList<>();
String jsonString = sharedPreferences.getString(CUSTOM_SHORTCUTS, null);
if (TextUtils.isEmpty(jsonString)) {
return allShortcutsFromPref;
}
if (TextUtils.isEmpty(jsonString)) return allShortcutsFromPref;

try {
// whole String to JSON, then extract name/record pairs
JSONObject allShortcuts = new JSONObject(jsonString);
Iterator<String> keysItr = allShortcuts.keys();
while (keysItr.hasNext()) {
String key = keysItr.next();
JSON json = new JSON();
try {
Map<String, Object> recordFromPref = (Map) json.parse(allShortcuts.getString(key));
allShortcutsFromPref.put(key, recordFromPref);
allShortcutsFromPref.add((Map<String, Object>) JSON.parse(allShortcuts.getString(key)));
} catch (IllegalStateException e) {
Log.w(TAG, "Can't parse custom shortcut '" + key + "': " + e.getMessage());
Log.w(TAG, "Can't parse custom shortcut '" + key + "'", e);
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.w(TAG, "Exception reading shortcuts", e);
}
return allShortcutsFromPref;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import android.view.View;

import uk.org.ngo.squeezer.Preferences;
import uk.org.ngo.squeezer.R;
import uk.org.ngo.squeezer.Squeezer;
import uk.org.ngo.squeezer.framework.ItemAdapter;
import uk.org.ngo.squeezer.model.CustomJiveItemHandling;
import uk.org.ngo.squeezer.framework.ItemViewHolder;
import uk.org.ngo.squeezer.model.JiveItem;
import uk.org.ngo.squeezer.service.HomeMenuHandling;
import uk.org.ngo.squeezer.service.ISqueezeService;
import uk.org.ngo.squeezer.widget.UndoBarController;


Expand All @@ -23,41 +22,35 @@ public class HomeMenuJiveItemView extends JiveItemView {
public HomeMenuJiveItemView(HomeMenuActivity homeMenuActivity, View view, ItemAdapter<ItemViewHolder<JiveItem>, JiveItem> adapter) {
super(homeMenuActivity, view);
mItemAdapter = adapter;
if (mCustomJiveItemHandling == null) {
mCustomJiveItemHandling = new CustomJiveItemHandling(getActivity());
}
}

@Override
public void bindView(JiveItem item) {
super.bindView(item);
final boolean isArchiveActive = Squeezer.getPreferences().getCustomizeHomeMenuMode() == Preferences.CustomizeHomeMenuMode.ARCHIVE;
final boolean isShortcutsActive = Squeezer.getPreferences().getCustomizeShortcutsMode() == Preferences.CustomizeShortcutsMode.ENABLED;

// archive DISABLED
if (isArchiveActive) {
itemView.setOnLongClickListener(view -> setArchive(item, isShortcutsActive));
} else { // archive DISABLED
if (isShortcutsActive) {
itemView.setOnLongClickListener(view -> setShortcuts(item));
} else { // no archive and no shortcuts
itemView.setOnLongClickListener(null);
}
itemView.setOnLongClickListener(view -> setArchive(item));
} else if (isShortcutsActive) {
itemView.setOnLongClickListener(view -> setShortcut(item));
} else { // no archive and no shortcuts
itemView.setOnLongClickListener(null);
}
}

private boolean setArchive(JiveItem item, boolean isShortcutsActive) {
private boolean setArchive(JiveItem item) {
if (!item.getId().equals(JiveItem.ARCHIVE.getId())) { // not the Archive node itself
ISqueezeService service = getActivity().requireService();
if (!item.getNode().equals(JiveItem.ARCHIVE.getId())) { // not INSIDE archive node
if (getActivity().requireService().isInArchive(item)) {
if (service.isInArchive(item)) {
getActivity().showDisplayMessage(R.string.MENU_IS_SUBMENU_IN_ARCHIVE);
return true;
}
if (mCustomJiveItemHandling.isCustomShortcut(item)) {
if (service.getHomeMenuHandling().isCustomShortcut(item)) {
if (isShortcutsActive) {
return removeShortcuts(item);
} else {
return true; // is shortcut but setting DISABLED, do nothing
removeShortcut(item);
}
return true; // Don't show UndoBar for shortcuts
} else {
// is not a shortcut, remove the item and bring up UndoBar
mItemAdapter.removeItem(getBindingAdapterPosition());
Expand All @@ -69,16 +62,16 @@ private boolean setArchive(JiveItem item, boolean isShortcutsActive) {
UndoBarController.show(getActivity(), R.string.MENU_ITEM_MOVED, new UndoBarController.UndoListener() {
@Override
public void onUndo() {
getActivity().requireService().toggleArchiveItem(item);
getActivity().requireService().triggerHomeMenuEvent();
service.toggleArchiveItem(item);
service.triggerHomeMenuEvent();
}

@Override
public void onDone() {
}
});

if ((getActivity().requireService().toggleArchiveItem(item))) {
if ((service.toggleArchiveItem(item))) {
// TODO: Do not instantly show the next screen or put UndoBar onto next screen
HomeActivity.show(getActivity());
getActivity().showDisplayMessage(R.string.ARCHIVE_NODE_REMOVED);
Expand All @@ -89,18 +82,19 @@ public void onDone() {
return true;
}

private boolean setShortcuts(JiveItem item) {
if (mCustomJiveItemHandling.isCustomShortcut(item)) {
return removeShortcuts(item);
private boolean setShortcut(JiveItem item) {
HomeMenuHandling homeMenuHandling = getActivity().requireService().getHomeMenuHandling();
if (homeMenuHandling.isCustomShortcut(item)) {
removeShortcut(item);
}
return true;
}

private boolean removeShortcuts(JiveItem item) {
private void removeShortcut(JiveItem item) {
HomeMenuHandling homeMenuHandling = getActivity().requireService().getHomeMenuHandling();
mItemAdapter.removeItem(getBindingAdapterPosition());
getActivity().showDisplayMessage(R.string.CUSTOM_SHORTCUT_REMOVED);
getActivity().requireService().removeCustomShortcut(item);
mPreferences.saveShortcuts(mCustomJiveItemHandling.convertShortcuts());
return true; // don't show UndoBar if Custom Shortcut
mPreferences.saveShortcuts(homeMenuHandling.getCustomShortcuts());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,23 @@
import uk.org.ngo.squeezer.model.CustomJiveItemHandling;
import uk.org.ngo.squeezer.model.JiveItem;
import uk.org.ngo.squeezer.model.Window;
import uk.org.ngo.squeezer.service.HomeMenuHandling;

public class JiveItemView extends ViewParamItemView<JiveItem> {

private final Window.WindowStyle windowStyle;
private final ArtworkListLayout listLayout;

Preferences mPreferences = Squeezer.getPreferences();
final boolean isShortcutActive = mPreferences.getCustomizeShortcutsMode() == Preferences.CustomizeShortcutsMode.ENABLED;
final boolean isShortcutsActive = mPreferences.getCustomizeShortcutsMode() == Preferences.CustomizeShortcutsMode.ENABLED;
final boolean isArchiveActive = mPreferences.getCustomizeHomeMenuMode() == Preferences.CustomizeHomeMenuMode.ARCHIVE;

/**
* Will also be used (and set) in HomeMenuJiveItemView.
*/
CustomJiveItemHandling mCustomJiveItemHandling = null;

JiveItemView(@NonNull JiveItemListActivity activity, @NonNull View view) {
this(activity, activity.window.windowStyle, activity.getPreferredListLayout(), view);
}

JiveItemView(@NonNull JiveItemListActivity activity, Window.WindowStyle windowStyle, ArtworkListLayout preferredListLayout, @NonNull View view) {
super(activity, view);
if (mCustomJiveItemHandling == null) {
mCustomJiveItemHandling = new CustomJiveItemHandling(activity);
}
this.windowStyle = windowStyle;
this.listLayout = listLayout(preferredListLayout, windowStyle);

Expand Down Expand Up @@ -105,7 +98,7 @@ public void bindView(JiveItem item) {
text2.setAlpha(getAlpha());
itemView.setOnClickListener(view -> onItemSelected());

if ( isShortcutActive || isArchiveActive ) {
if ( isShortcutsActive || isArchiveActive ) {
itemView.setOnLongClickListener(view -> putItemAsShortcut());
} else {
itemView.setOnLongClickListener(null);
Expand All @@ -130,15 +123,16 @@ public void bindView(JiveItem item) {
*/
private boolean putItemAsShortcut() {
@StringRes int message = !isArchiveActive ? R.string.ITEM_CANNOT_BE_SHORTCUT :
isShortcutActive ? R.string.ITEM_CAN_NOT_BE_SHORTCUT_OR_ARCHIVED : R.string.ITEM_CANNOT_BE_ARCHIVED;
isShortcutsActive ? R.string.ITEM_CAN_NOT_BE_SHORTCUT_OR_ARCHIVED : R.string.ITEM_CANNOT_BE_ARCHIVED;

if (!mCustomJiveItemHandling.isShortcutable(item)) {
int shortCutWeight = CustomJiveItemHandling.shortcutWeight(item);
if (shortCutWeight == CustomJiveItemHandling.CUSTOM_SHORTCUT_WEIGHT_NOT_ALLOWED) {
getActivity().showDisplayMessage(message);
} else {
if (isShortcutActive) {
if (mCustomJiveItemHandling.triggerCustomShortcut(item)) {
mPreferences.saveShortcuts(mCustomJiveItemHandling.convertShortcuts());
// TODO: check ok?
if (isShortcutsActive) {
HomeMenuHandling homeMenuHandling = getActivity().requireService().getHomeMenuHandling();
if (homeMenuHandling.addShortcut(item, getActivity().parent, shortCutWeight)) {
mPreferences.saveShortcuts(homeMenuHandling.getCustomShortcuts());
getActivity().showDisplayMessage(R.string.ITEM_PUT_AS_SHORTCUT_ON_HOME_MENU);
} else {
getActivity().showDisplayMessage(R.string.ITEM_IS_ALREADY_A_SHORTCUT);
Expand Down
Loading

0 comments on commit 1e78705

Please sign in to comment.