From 21acb77c5b5540ed38cbb3abb2d6fe8c26e4a02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Eisfeld?= Date: Wed, 12 Jan 2022 12:34:24 +0100 Subject: [PATCH] Fix refresh behavior --- .../jeisfeld/lifx/app/home/DeviceAdapter.java | 6 ++-- .../lifx/app/home/DeviceViewModel.java | 29 ++++++++++++------- .../lifx/app/home/GroupViewModel.java | 19 ++++++------ .../jeisfeld/lifx/app/home/HomeFragment.java | 22 ++++++++++---- .../lifx/app/home/LightViewModel.java | 4 +-- .../jeisfeld/lifx/app/home/MainViewModel.java | 4 ++- .../lifx/app/home/MultizoneViewModel.java | 4 +-- .../jeisfeld/lifx/app/home/TileViewModel.java | 4 +-- 8 files changed, 57 insertions(+), 35 deletions(-) diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceAdapter.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceAdapter.java index 79e3092..5ef42ee 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceAdapter.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceAdapter.java @@ -194,10 +194,12 @@ else if (device instanceof Light) { /** * Refresh view data for all devices. + * + * @param isHighPriority flag indicating if this is high priority refresh. */ - protected void refresh() { + protected void refresh(final boolean isHighPriority) { for (MainViewModel model : mViewModels) { - model.refresh(); + model.refresh(isHighPriority); } } diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceViewModel.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceViewModel.java index 443f1f1..ac83100 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceViewModel.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/DeviceViewModel.java @@ -31,7 +31,7 @@ public class DeviceViewModel extends MainViewModel { * Constructor. * * @param context the context. - * @param device The device. + * @param device The device. */ public DeviceViewModel(final Context context, final Device device) { super(context); @@ -53,13 +53,13 @@ public final void checkPower() { } @Override - protected final void refresh() { - if (isRefreshAllowed()) { + protected final void refresh(final boolean isHighPriority) { + if (isRefreshPowerAllowed()) { if (mPower.getValue() == null) { new RefreshAfterCheckReachabilityTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { - refreshRemoteData(true, false); + refreshRemoteData(true, isHighPriority || isRefreshColorsAllowed()); } } } @@ -70,13 +70,22 @@ public final String getLabel() { } /** - * Check if refresh is allowed. + * Check if refresh of colors is allowed. * - * @return true if refresh is allowed. + * @return true if refresh of colors is allowed. */ - protected boolean isRefreshAllowed() { - return PreferenceUtil.getSharedPreferenceLongString(R.string.key_pref_refresh_period, R.string.pref_default_refresh_period) > 0 - && PreferenceUtil.getSharedPreferenceIntString(R.string.key_pref_power_duration, R.string.pref_default_power_duration) > 0; + // OVERRIDABLE + protected boolean isRefreshColorsAllowed() { + return true; + } + + /** + * Check if refresh of power is allowed. + * + * @return true if refresh of power is allowed. + */ + private boolean isRefreshPowerAllowed() { + return PreferenceUtil.getSharedPreferenceIntString(R.string.key_pref_power_duration, R.string.pref_default_power_duration) > 0; } /** @@ -180,7 +189,7 @@ protected void onPostExecute(final Boolean isReachable) { return; } if (isReachable) { - model.refreshRemoteData(true, true); + model.refreshRemoteData(true, model.isRefreshColorsAllowed()); } } } diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/GroupViewModel.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/GroupViewModel.java index 0a3a05d..e250322 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/GroupViewModel.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/GroupViewModel.java @@ -99,6 +99,13 @@ public final void checkPower() { new CheckPowerTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } + @Override + protected final void refresh(final boolean isHighPriority) { + if (isHighPriority) { + checkPower(); + } + } + @Override public final void togglePower() { new TogglePowerTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); @@ -116,11 +123,7 @@ protected final void updateBrightness(final double brightness) { synchronized (mRunningSetColorTasks) { for (Device device : DeviceRegistry.getInstance().getDevices(mGroupId, false)) { if (device instanceof Light) { - List tasksForDevice = mRunningSetColorTasks.get(device); - if (tasksForDevice == null) { - tasksForDevice = new ArrayList<>(); - mRunningSetColorTasks.put(device, tasksForDevice); - } + List tasksForDevice = mRunningSetColorTasks.computeIfAbsent(device, k -> new ArrayList<>()); tasksForDevice.add(new SetColorTask(this, brightness, (Light) device)); if (tasksForDevice.size() > 2) { @@ -144,11 +147,7 @@ public void updateColor(final Color color) { synchronized (mRunningSetColorTasks) { for (Device device : DeviceRegistry.getInstance().getDevices(mGroupId, false)) { if (device instanceof Light) { - List tasksForDevice = mRunningSetColorTasks.get(device); - if (tasksForDevice == null) { - tasksForDevice = new ArrayList<>(); - mRunningSetColorTasks.put(device, tasksForDevice); - } + List tasksForDevice = mRunningSetColorTasks.computeIfAbsent(device, k -> new ArrayList<>()); tasksForDevice.add(new SetColorTask(this, color, (Light) device)); if (tasksForDevice.size() > 2) { diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/HomeFragment.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/HomeFragment.java index c705b08..71def7e 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/HomeFragment.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/HomeFragment.java @@ -21,6 +21,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import androidx.annotation.NonNull; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.ListFragment; import de.jeisfeld.lifx.app.Application; @@ -56,6 +57,10 @@ public class HomeFragment extends ListFragment { * The adapter used. */ private DeviceAdapter mAdapter; + /** + * The view creation time. + */ + private long mViewCreationTime; /** * Send broadcast to home fragment informing about the end of an animation. @@ -75,8 +80,8 @@ public final View onCreateView(final LayoutInflater inflater, final ViewGroup co } @Override - public final void onActivityCreated(final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); + public final void onViewCreated(@NonNull final View view, final Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); if (DeviceRegistry.getInstance().getDevices(false).size() == 0) { getListView().setVisibility(View.GONE); @@ -99,6 +104,8 @@ public void onReceive(final Context context, final Intent intent) { } } }; + + mViewCreationTime = System.currentTimeMillis(); } @Override @@ -108,10 +115,13 @@ public final void onResume() { requireActivity().registerReceiver(mReceiver, new IntentFilter(EXTRA_ANIMATION_STOP_INTENT)); mExecutor = Executors.newScheduledThreadPool(1); - if (PreferenceUtil.getSharedPreferenceLongString(R.string.key_pref_refresh_period, R.string.pref_default_refresh_period) > 0) { - mExecutor.scheduleAtFixedRate(() -> mAdapter.refresh(), 0, - PreferenceUtil.getSharedPreferenceLongString(R.string.key_pref_refresh_period, R.string.pref_default_refresh_period), - TimeUnit.MILLISECONDS); + long refreshDelay = PreferenceUtil.getSharedPreferenceLongString(R.string.key_pref_refresh_period, R.string.pref_default_refresh_period); + // Avoid duplication of refresh immediately after creation of fragment, but refresh when bringing to foreground again + if (System.currentTimeMillis() - mViewCreationTime > TimeUnit.SECONDS.toMillis(1)) { + mAdapter.refresh(true); + } + if (refreshDelay > 0) { + mExecutor.scheduleAtFixedRate(() -> mAdapter.refresh(false), refreshDelay, refreshDelay, TimeUnit.MILLISECONDS); } } diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/LightViewModel.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/LightViewModel.java index aa589cb..e11bc32 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/LightViewModel.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/LightViewModel.java @@ -78,8 +78,8 @@ public final LiveData getColor() { // OVERRIDABLE @Override - protected boolean isRefreshAllowed() { - return super.isRefreshAllowed() && !Boolean.TRUE.equals(mAnimationStatus.getValue()); + protected boolean isRefreshColorsAllowed() { + return super.isRefreshColorsAllowed() && !Boolean.TRUE.equals(mAnimationStatus.getValue()); } // OVERRIDABLE diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MainViewModel.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MainViewModel.java index dee79db..d7013a4 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MainViewModel.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MainViewModel.java @@ -70,9 +70,11 @@ protected static boolean isAutoOn() { /** * Refresh the device. If offline, first check if online again. + * + * @param isHighPriority flag indicating if this is high priority refresh. */ // OVERRIDABLE - protected void refresh() { + protected void refresh(final boolean isHighPriority) { } /** diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MultizoneViewModel.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MultizoneViewModel.java index 85b9118..1bb84d8 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MultizoneViewModel.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/MultizoneViewModel.java @@ -177,9 +177,9 @@ public final void checkColor() { } @Override - protected final boolean isRefreshAllowed() { + protected final boolean isRefreshColorsAllowed() { // Due to tendency for connectivity issues, check Multizone light only if disconnected or if colors have not yet been initialized. - return super.isRefreshAllowed() + return super.isRefreshColorsAllowed() && (mColors.getValue() == null || (!Power.ON.equals(mPower.getValue()) && !Power.OFF.equals(mPower.getValue()))); } diff --git a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/TileViewModel.java b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/TileViewModel.java index 53ff49d..dce2c0c 100644 --- a/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/TileViewModel.java +++ b/LifxTools/app/src/main/java/de/jeisfeld/lifx/app/home/TileViewModel.java @@ -172,9 +172,9 @@ public final void checkColor() { } @Override - protected final boolean isRefreshAllowed() { + protected final boolean isRefreshColorsAllowed() { // Due to tendency for connectivity issues, check Multizone light only if disconnected or if colors have not yet been initialized. - return super.isRefreshAllowed() + return super.isRefreshColorsAllowed() && (mColors.getValue() == null || (!Power.ON.equals(mPower.getValue()) && !Power.OFF.equals(mPower.getValue()))); }