mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Improve remote wipe display fragment
This commit is contained in:
@@ -4,26 +4,30 @@ import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.contact.ContactListAdapter;
|
||||
import org.briarproject.briar.android.contact.ContactListItem;
|
||||
import org.briarproject.briar.android.contact.OnContactClickListener;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
|
||||
import java.util.List;
|
||||
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
|
||||
public class RemoteWipeDisplayFragment extends
|
||||
BaseFragment {
|
||||
public class RemoteWipeDisplayFragment extends BaseFragment
|
||||
implements OnContactClickListener<ContactListItem> {
|
||||
|
||||
public static final String TAG = RemoteWipeDisplayFragment.class.getName();
|
||||
|
||||
private final ContactListAdapter adapter = new ContactListAdapter(this);
|
||||
private BriarRecyclerView list;
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
@@ -41,25 +45,58 @@ public class RemoteWipeDisplayFragment extends
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_display_remote_wipe,
|
||||
container, false);
|
||||
List<String> wiperNames = viewModel.getWiperNames();
|
||||
StringBuilder custodianNamesString = new StringBuilder();
|
||||
for (String custodianName : wiperNames) {
|
||||
custodianNamesString
|
||||
.append("• ")
|
||||
.append(custodianName)
|
||||
.append("\n");
|
||||
}
|
||||
TextView textViewThreshold = view.findViewById(R.id.textViewWipers);
|
||||
textViewThreshold.setText(custodianNamesString.toString());
|
||||
|
||||
Button button = view.findViewById(R.id.button);
|
||||
button.setOnClickListener(e -> viewModel.onModifyWipers());
|
||||
return view;
|
||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||
|
||||
viewModel.getWiperContactIds();
|
||||
list = contentView.findViewById(R.id.list);
|
||||
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||
list.setAdapter(adapter);
|
||||
list.setEmptyText(R.string.no_contacts);
|
||||
|
||||
viewModel.getContactListItems().observe(getViewLifecycleOwner(),
|
||||
result -> result.onError(this::handleException)
|
||||
.onSuccess(adapter::submitList)
|
||||
);
|
||||
|
||||
return contentView;
|
||||
}
|
||||
// View view = inflater.inflate(R.layout.fragment_display_remote_wipe,
|
||||
// container, false);
|
||||
// List<String> wiperNames = viewModel.getWiperNames();
|
||||
// StringBuilder custodianNamesString = new StringBuilder();
|
||||
// for (String custodianName : wiperNames) {
|
||||
// custodianNamesString
|
||||
// .append("• ")
|
||||
// .append(custodianName)
|
||||
// .append("\n");
|
||||
// }
|
||||
// TextView textViewThreshold = view.findViewById(R.id.textViewWipers);
|
||||
// textViewThreshold.setText(custodianNamesString.toString());
|
||||
//
|
||||
// Button button = view.findViewById(R.id.button);
|
||||
// button.setOnClickListener(e -> viewModel.onModifyWipers());
|
||||
// return view;
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(View view, ContactListItem item) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
list.startPeriodicUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
list.stopPeriodicUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,77 @@
|
||||
package org.briarproject.briar.android.remotewipe;
|
||||
|
||||
import android.app.Application;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.briar.android.contact.ContactsViewModel;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.identity.AuthorManager;
|
||||
import org.briarproject.briar.api.remotewipe.RemoteWipeManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
||||
@NotNullByDefault
|
||||
public class RemoteWipeSetupViewModel extends ContactsViewModel {
|
||||
private final RemoteWipeManager remoteWipeManager;
|
||||
private final DatabaseComponent db;
|
||||
private final MutableLiveData<RemoteWipeSetupState> state = new MutableLiveData<>();
|
||||
private List<ContactId> wiperContactIds;
|
||||
private final MutableLiveData<RemoteWipeSetupState> state =
|
||||
new MutableLiveData<>();
|
||||
|
||||
private final ContactManager contactManager;
|
||||
private final AuthorManager authorManager;
|
||||
|
||||
@Inject
|
||||
RemoteWipeSetupViewModel(
|
||||
@NonNull Application application,
|
||||
RemoteWipeManager remoteWipeManager,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
AuthorManager authorManager,
|
||||
ConversationManager conversationManager,
|
||||
ConnectionRegistry connectionRegistry,
|
||||
EventBus eventBus,
|
||||
AndroidExecutor androidExecutor,
|
||||
ContactManager contactManager,
|
||||
DatabaseComponent db) {
|
||||
super(application);
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||
contactManager, authorManager, conversationManager,
|
||||
connectionRegistry, eventBus);
|
||||
this.remoteWipeManager = remoteWipeManager;
|
||||
this.contactManager = contactManager;
|
||||
this.authorManager = authorManager;
|
||||
this.db = db;
|
||||
getWiperContactIds();
|
||||
loadContacts();
|
||||
}
|
||||
|
||||
public boolean remoteWipeIsSetup() {
|
||||
try {
|
||||
return db.transactionWithResult(true,
|
||||
txn -> remoteWipeManager.remoteWipeIsSetup(txn));
|
||||
txn -> {
|
||||
boolean isSetup = remoteWipeManager.remoteWipeIsSetup(txn);
|
||||
if (isSetup) wiperContactIds = remoteWipeManager.getWiperContactIds(txn);
|
||||
return isSetup;
|
||||
});
|
||||
} catch (DbException e) {
|
||||
return false;
|
||||
}
|
||||
@@ -47,26 +79,12 @@ public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
||||
|
||||
public List<ContactId> getWiperContactIds() {
|
||||
try {
|
||||
return db.transactionWithResult(true,
|
||||
wiperContactIds = db.transactionWithResult(true,
|
||||
remoteWipeManager::getWiperContactIds);
|
||||
} catch (DbException ignored) {
|
||||
return new ArrayList<ContactId>();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getWiperNames() {
|
||||
ArrayList wiperNames = new ArrayList();
|
||||
try {
|
||||
List<Author> wipers = db.transactionWithResult(true,
|
||||
remoteWipeManager::getWipers);
|
||||
for (Author wiper : wipers) {
|
||||
wiperNames.add(wiper.getName());
|
||||
}
|
||||
return wiperNames;
|
||||
} catch (DbException ignored) {
|
||||
// Will return an empty list
|
||||
}
|
||||
return wiperNames;
|
||||
return wiperContactIds;
|
||||
}
|
||||
|
||||
@UiThread
|
||||
@@ -86,7 +104,7 @@ public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
||||
|
||||
@UiThread
|
||||
public void onModifyWipers() {
|
||||
state.postValue(RemoteWipeSetupState.MODIFY);
|
||||
state.postValue(RemoteWipeSetupState.MODIFY);
|
||||
}
|
||||
|
||||
public void setupRemoteWipe(Collection<ContactId> wipers)
|
||||
@@ -97,6 +115,12 @@ public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean displayContact(ContactId contactId) {
|
||||
// Check if contact is a wiper
|
||||
return wiperContactIds.contains(contactId);
|
||||
}
|
||||
|
||||
public MutableLiveData<RemoteWipeSetupState> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user