From 036445a57b149dec82c72e48b98ae4b7b7c6c2b8 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Sun, 8 Jan 2023 23:56:30 -0600 Subject: Initial progress of huge refactoring job that I really, really shouldn't be doing --- .../github/daneren2005/dsub/view/UpdateView.java | 83 ++++++++++------------ 1 file changed, 38 insertions(+), 45 deletions(-) (limited to 'app/src/main/java/github/daneren2005/dsub/view/UpdateView.java') diff --git a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java index a4727643..d977bdf0 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java @@ -43,7 +43,7 @@ import github.daneren2005.dsub.util.SilentBackgroundTask; public abstract class UpdateView extends LinearLayout { private static final String TAG = UpdateView.class.getSimpleName(); - private static final WeakHashMap INSTANCES = new WeakHashMap(); + private static final WeakHashMap, ?> INSTANCES = new WeakHashMap<>(); protected static Handler backgroundHandler; protected static Handler uiHandler; @@ -79,7 +79,7 @@ public abstract class UpdateView extends LinearLayout { this.autoUpdate = autoUpdate; setLayoutParams(new AbsListView.LayoutParams( - ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); if(autoUpdate) { @@ -110,9 +110,15 @@ public abstract class UpdateView extends LinearLayout { updateBackground(); update(); } + public void setObject(T obj1, Object obj2) { - setObject(obj1, null); + if (obj2 == null) { + setObject(obj1); + } else { + setObject(obj1, null); + } } + protected abstract void setObjectImpl(T obj); private static synchronized void startUpdater() { @@ -123,20 +129,13 @@ public abstract class UpdateView extends LinearLayout { uiHandler = new Handler(); // Needed so handler is never null until thread creates it backgroundHandler = uiHandler; - updateRunnable = new Runnable() { - @Override - public void run() { - updateAll(); - } - }; + updateRunnable = UpdateView::updateAll; - new Thread(new Runnable() { - public void run() { - Looper.prepare(); - backgroundHandler = new Handler(Looper.myLooper()); - uiHandler.post(updateRunnable); - Looper.loop(); - } + new Thread(() -> { + Looper.prepare(); + backgroundHandler = new Handler(Looper.myLooper()); + uiHandler.post(updateRunnable); + Looper.loop(); }, "UpdateView").start(); } @@ -156,8 +155,8 @@ public abstract class UpdateView extends LinearLayout { return; } - List views = new ArrayList(); - for (UpdateView view : INSTANCES.keySet()) { + List> views = new ArrayList<>(); + for (UpdateView view : INSTANCES.keySet()) { if (view.isShown()) { views.add(view); } @@ -171,32 +170,26 @@ public abstract class UpdateView extends LinearLayout { Log.w(TAG, "Error when updating song views.", x); } } - private static void updateAllLive(final List views) { - final Runnable runnable = new Runnable() { - @Override - public void run() { - try { - for(UpdateView view: views) { - view.update(); - } - } catch (Throwable x) { - Log.w(TAG, "Error when updating song views.", x); + private static void updateAllLive(final List> views) { + final Runnable runnable = () -> { + try { + for(UpdateView view: views) { + view.update(); } - uiHandler.postDelayed(updateRunnable, 1000L); + } catch (Throwable x) { + Log.w(TAG, "Error when updating song views.", x); } + uiHandler.postDelayed(updateRunnable, 1000L); }; - backgroundHandler.post(new Runnable() { - @Override - public void run() { - try { - for(UpdateView view: views) { - view.updateBackground(); - } - uiHandler.post(runnable); - } catch (Throwable x) { - Log.w(TAG, "Error when updating song views.", x); + backgroundHandler.post(() -> { + try { + for(UpdateView view: views) { + view.updateBackground(); } + uiHandler.post(runnable); + } catch (Throwable x) { + Log.w(TAG, "Error when updating song views.", x); } }); } @@ -218,7 +211,7 @@ public abstract class UpdateView extends LinearLayout { } public static MusicDirectory.Entry findEntry(MusicDirectory.Entry entry) { - for(UpdateView view: INSTANCES.keySet()) { + for(UpdateView view: INSTANCES.keySet()) { MusicDirectory.Entry check = null; if(view instanceof SongView) { check = ((SongView) view).getEntry(); @@ -297,7 +290,7 @@ public abstract class UpdateView extends LinearLayout { startBackgroundDrawable = child.getBackground(); child.setBackgroundColor(DrawableTint.getColorRes(context, R.attr.colorPrimary)); } else if (!checked && startBackgroundDrawable != null) { - child.setBackgroundDrawable(startBackgroundDrawable); + child.setBackground(startBackgroundDrawable); startBackgroundDrawable = null; } } @@ -311,11 +304,11 @@ public abstract class UpdateView extends LinearLayout { } public static class UpdateViewHolder extends RecyclerView.ViewHolder { - private UpdateView updateView; - private View view; + private UpdateView updateView; + private final View view; private T item; - public UpdateViewHolder(UpdateView itemView) { + public UpdateViewHolder(UpdateView itemView) { super(itemView); this.updateView = itemView; @@ -323,7 +316,7 @@ public abstract class UpdateView extends LinearLayout { } // Different is so that call is not ambiguous - public UpdateViewHolder(View view, boolean different) { + public UpdateViewHolder(View view) { super(view); this.view = view; } -- cgit v1.2.3