ToggleSwitch: Improve reliability
It's not great, but it's better than it was. Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
parent
668d90f063
commit
b6ed17884f
@ -237,9 +237,10 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
|
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
|
||||||
final CompletionStage<State> completion =
|
final CompletionStage<State> completion =
|
||||||
asyncWorker.supplyAsync(() -> backend.setState(tunnel, state));
|
asyncWorker.supplyAsync(() -> backend.setState(tunnel, state));
|
||||||
completion.thenAccept(tunnel::onStateChanged);
|
completion.whenComplete((newState, e) -> {
|
||||||
completion.thenAccept(newState -> {
|
// Ensure onStateChanged is always called (failure or not), and with the correct state.
|
||||||
if (newState == State.UP)
|
tunnel.onStateChanged(e == null ? newState : tunnel.getState());
|
||||||
|
if (e == null && newState == State.UP)
|
||||||
setLastUsedTunnel(tunnel);
|
setLastUsedTunnel(tunnel);
|
||||||
});
|
});
|
||||||
return completion;
|
return completion;
|
||||||
|
@ -22,25 +22,15 @@ import android.util.AttributeSet;
|
|||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
|
|
||||||
public class ToggleSwitch extends Switch {
|
public class ToggleSwitch extends Switch {
|
||||||
private boolean hasPendingStateChange;
|
|
||||||
private boolean isRestoringState;
|
private boolean isRestoringState;
|
||||||
private OnBeforeCheckedChangeListener listener;
|
private OnBeforeCheckedChangeListener listener;
|
||||||
|
|
||||||
public ToggleSwitch(final Context context) {
|
|
||||||
super(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ToggleSwitch(final Context context, final AttributeSet attrs) {
|
public ToggleSwitch(final Context context, final AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ToggleSwitch(final Context context, final AttributeSet attrs, final int defStyleAttr) {
|
public ToggleSwitch(final Context context) {
|
||||||
super(context, attrs, defStyleAttr);
|
this(context, null);
|
||||||
}
|
|
||||||
|
|
||||||
public ToggleSwitch(final Context context, final AttributeSet attrs, final int defStyleAttr,
|
|
||||||
final int defStyleRes) {
|
|
||||||
super(context, attrs, defStyleAttr, defStyleRes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -48,28 +38,23 @@ public class ToggleSwitch extends Switch {
|
|||||||
isRestoringState = true;
|
isRestoringState = true;
|
||||||
super.onRestoreInstanceState(state);
|
super.onRestoreInstanceState(state);
|
||||||
isRestoringState = false;
|
isRestoringState = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setChecked(final boolean checked) {
|
public void setChecked(final boolean checked) {
|
||||||
|
if (checked == isChecked())
|
||||||
|
return;
|
||||||
if (isRestoringState || listener == null) {
|
if (isRestoringState || listener == null) {
|
||||||
super.setChecked(checked);
|
super.setChecked(checked);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (hasPendingStateChange)
|
|
||||||
return;
|
|
||||||
hasPendingStateChange = true;
|
|
||||||
setEnabled(false);
|
setEnabled(false);
|
||||||
listener.onBeforeCheckedChanged(this, checked);
|
listener.onBeforeCheckedChanged(this, checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCheckedInternal(final boolean checked) {
|
public void setCheckedInternal(final boolean checked) {
|
||||||
if (hasPendingStateChange) {
|
|
||||||
setEnabled(true);
|
|
||||||
hasPendingStateChange = false;
|
|
||||||
}
|
|
||||||
super.setChecked(checked);
|
super.setChecked(checked);
|
||||||
|
setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOnBeforeCheckedChangeListener(final OnBeforeCheckedChangeListener listener) {
|
public void setOnBeforeCheckedChangeListener(final OnBeforeCheckedChangeListener listener) {
|
||||||
|
Loading…
Reference in New Issue
Block a user