That fuck shit the fascists are using
1package org.tm.archive.mediasend;
2
3import androidx.annotation.NonNull;
4import androidx.annotation.Nullable;
5import androidx.lifecycle.LiveData;
6import androidx.lifecycle.MutableLiveData;
7import androidx.lifecycle.ViewModel;
8import androidx.lifecycle.ViewModelProvider;
9
10import org.signal.core.util.ThreadUtil;
11import org.tm.archive.recipients.Recipient;
12import org.tm.archive.util.SingleLiveEvent;
13
14import java.util.ArrayList;
15import java.util.LinkedHashSet;
16import java.util.List;
17import java.util.Set;
18
19class CameraContactSelectionViewModel extends ViewModel {
20
21 static final int MAX_SELECTION_COUNT = 16;
22
23 private final CameraContactsRepository repository;
24 private final MutableLiveData<ContactState> contacts;
25 private final SingleLiveEvent<Error> error;
26 private final Set<Recipient> selected;
27
28 private String currentQuery;
29
30 private CameraContactSelectionViewModel(@NonNull CameraContactsRepository repository) {
31 this.repository = repository;
32 this.contacts = new MutableLiveData<>();
33 this.error = new SingleLiveEvent<>();
34 this.selected = new LinkedHashSet<>();
35
36 repository.getCameraContacts(cameraContacts -> {
37 ThreadUtil.runOnMain(() -> {
38 contacts.postValue(new ContactState(cameraContacts, new ArrayList<>(selected), currentQuery));
39 });
40 });
41 }
42
43 LiveData<ContactState> getContacts() {
44 return contacts;
45 }
46
47 LiveData<Error> getError() {
48 return error;
49 }
50
51 void onSearchClosed() {
52 onQueryUpdated("");
53 }
54
55 void onQueryUpdated(String query) {
56 this.currentQuery = query;
57
58 repository.getCameraContacts(query, cameraContacts -> {
59 ThreadUtil.runOnMain(() -> {
60 contacts.postValue(new ContactState(cameraContacts, new ArrayList<>(selected), query));
61 });
62 });
63 }
64
65 void onRefresh() {
66 repository.getCameraContacts(cameraContacts -> {
67 ThreadUtil.runOnMain(() -> {
68 contacts.postValue(new ContactState(cameraContacts, new ArrayList<>(selected), currentQuery));
69 });
70 });
71 }
72
73 void onContactClicked(@NonNull Recipient recipient) {
74 if (selected.contains(recipient)) {
75 selected.remove(recipient);
76 } else if (selected.size() < MAX_SELECTION_COUNT) {
77 selected.add(recipient);
78 } else {
79 error.postValue(Error.MAX_SELECTION);
80 }
81
82 ContactState currentState = contacts.getValue();
83
84 if (currentState != null) {
85 contacts.setValue(new ContactState(currentState.getContacts(), new ArrayList<>(selected), currentQuery));
86 }
87 }
88
89 static class ContactState {
90 private final CameraContacts contacts;
91 private final List<Recipient> selected;
92 private final String query;
93
94 ContactState(@NonNull CameraContacts contacts, @NonNull List<Recipient> selected, @Nullable String query) {
95 this.contacts = contacts;
96 this.selected = selected;
97 this.query = query;
98 }
99
100 public CameraContacts getContacts() {
101 return contacts;
102 }
103
104 public List<Recipient> getSelected() {
105 return selected;
106 }
107
108 public @Nullable String getQuery() {
109 return query;
110 }
111 }
112
113 enum Error {
114 MAX_SELECTION
115 }
116
117 static class Factory extends ViewModelProvider.NewInstanceFactory {
118
119 private final CameraContactsRepository repository;
120
121 Factory(CameraContactsRepository repository) {
122 this.repository = repository;
123 }
124
125 @Override
126 public @NonNull <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
127 //noinspection ConstantConditions
128 return modelClass.cast(new CameraContactSelectionViewModel(repository));
129 }
130 }
131
132}