Ensure store is properly cleaned up in conversation settings.
This commit is contained in:
parent
392d582865
commit
bad2f99968
2 changed files with 21 additions and 4 deletions
|
@ -103,10 +103,8 @@ sealed class ConversationSettingsViewModel(
|
|||
|
||||
override fun onCleared() {
|
||||
cleared = true
|
||||
store.update { state ->
|
||||
openedMediaCursors.forEach { it.ensureClosed() }
|
||||
state.copy(sharedMedia = null)
|
||||
}
|
||||
openedMediaCursors.forEach { it.ensureClosed() }
|
||||
store.clear()
|
||||
}
|
||||
|
||||
private fun Cursor?.ensureClosed() {
|
||||
|
|
|
@ -11,6 +11,8 @@ import com.annimon.stream.function.Function;
|
|||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.thoughtcrime.securesms.util.concurrent.SerialExecutor;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
|
@ -44,12 +46,21 @@ public class Store<State> {
|
|||
liveStore.update(source, action);
|
||||
}
|
||||
|
||||
@MainThread
|
||||
public void clear() {
|
||||
liveStore.clear();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private final class LiveDataStore extends MediatorLiveData<State> {
|
||||
private State state;
|
||||
private final Executor stateUpdater;
|
||||
|
||||
private final Set<LiveData> sources;
|
||||
|
||||
LiveDataStore(@NonNull State state) {
|
||||
this.stateUpdater = new SerialExecutor(SignalExecutors.BOUNDED);
|
||||
this.sources = new HashSet<>();
|
||||
setState(state);
|
||||
}
|
||||
|
||||
|
@ -63,12 +74,20 @@ public class Store<State> {
|
|||
}
|
||||
|
||||
<Input> void update(@NonNull LiveData<Input> source, @NonNull Action<Input, State> action) {
|
||||
sources.add(source);
|
||||
addSource(source, input -> stateUpdater.execute(() -> setState(action.apply(input, getState()))));
|
||||
}
|
||||
|
||||
void update(@NonNull Function<State, State> updater) {
|
||||
stateUpdater.execute(() -> setState(updater.apply(getState())));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (LiveData source : sources) {
|
||||
removeSource(source);
|
||||
}
|
||||
sources.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public interface Action<Input, State> {
|
||||
|
|
Loading…
Add table
Reference in a new issue