Skip to content

Commit

Permalink
Fix refresh behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jeisfeld committed Jan 12, 2022
1 parent 72bac8b commit 21acb77
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
}
}
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -180,7 +189,7 @@ protected void onPostExecute(final Boolean isReachable) {
return;
}
if (isReachable) {
model.refreshRemoteData(true, true);
model.refreshRemoteData(true, model.isRefreshColorsAllowed());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<AsyncExecutable> tasksForDevice = mRunningSetColorTasks.get(device);
if (tasksForDevice == null) {
tasksForDevice = new ArrayList<>();
mRunningSetColorTasks.put(device, tasksForDevice);
}
List<AsyncExecutable> tasksForDevice = mRunningSetColorTasks.computeIfAbsent(device, k -> new ArrayList<>());
tasksForDevice.add(new SetColorTask(this, brightness, (Light) device));

if (tasksForDevice.size() > 2) {
Expand All @@ -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<AsyncExecutable> tasksForDevice = mRunningSetColorTasks.get(device);
if (tasksForDevice == null) {
tasksForDevice = new ArrayList<>();
mRunningSetColorTasks.put(device, tasksForDevice);
}
List<AsyncExecutable> tasksForDevice = mRunningSetColorTasks.computeIfAbsent(device, k -> new ArrayList<>());
tasksForDevice.add(new SetColorTask(this, color, (Light) device));

if (tasksForDevice.size() > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -99,6 +104,8 @@ public void onReceive(final Context context, final Intent intent) {
}
}
};

mViewCreationTime = System.currentTimeMillis();
}

@Override
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public final LiveData<Color> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}

Expand Down

0 comments on commit 21acb77

Please sign in to comment.