mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 06:09:55 +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.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
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 org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
|
||||||
public class RemoteWipeDisplayFragment extends
|
public class RemoteWipeDisplayFragment extends BaseFragment
|
||||||
BaseFragment {
|
implements OnContactClickListener<ContactListItem> {
|
||||||
|
|
||||||
public static final String TAG = RemoteWipeDisplayFragment.class.getName();
|
public static final String TAG = RemoteWipeDisplayFragment.class.getName();
|
||||||
|
|
||||||
|
private final ContactListAdapter adapter = new ContactListAdapter(this);
|
||||||
|
private BriarRecyclerView list;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
@@ -41,25 +45,58 @@ public class RemoteWipeDisplayFragment extends
|
|||||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
@Nullable ViewGroup container,
|
@Nullable ViewGroup container,
|
||||||
@Nullable Bundle savedInstanceState) {
|
@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);
|
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||||
button.setOnClickListener(e -> viewModel.onModifyWipers());
|
|
||||||
return view;
|
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
|
@Override
|
||||||
public String getUniqueTag() {
|
public String getUniqueTag() {
|
||||||
return TAG;
|
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;
|
package org.briarproject.briar.android.remotewipe;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.provider.ContactsContract;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.FormatException;
|
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.ContactId;
|
||||||
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
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.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 org.briarproject.briar.api.remotewipe.RemoteWipeManager;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
@NotNullByDefault
|
||||||
|
public class RemoteWipeSetupViewModel extends ContactsViewModel {
|
||||||
private final RemoteWipeManager remoteWipeManager;
|
private final RemoteWipeManager remoteWipeManager;
|
||||||
private final DatabaseComponent db;
|
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
|
@Inject
|
||||||
RemoteWipeSetupViewModel(
|
RemoteWipeSetupViewModel(
|
||||||
@NonNull Application application,
|
@NonNull Application application,
|
||||||
RemoteWipeManager remoteWipeManager,
|
RemoteWipeManager remoteWipeManager,
|
||||||
|
@DatabaseExecutor Executor dbExecutor,
|
||||||
|
LifecycleManager lifecycleManager,
|
||||||
|
AuthorManager authorManager,
|
||||||
|
ConversationManager conversationManager,
|
||||||
|
ConnectionRegistry connectionRegistry,
|
||||||
|
EventBus eventBus,
|
||||||
|
AndroidExecutor androidExecutor,
|
||||||
|
ContactManager contactManager,
|
||||||
DatabaseComponent db) {
|
DatabaseComponent db) {
|
||||||
super(application);
|
super(application, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||||
|
contactManager, authorManager, conversationManager,
|
||||||
|
connectionRegistry, eventBus);
|
||||||
this.remoteWipeManager = remoteWipeManager;
|
this.remoteWipeManager = remoteWipeManager;
|
||||||
|
this.contactManager = contactManager;
|
||||||
|
this.authorManager = authorManager;
|
||||||
this.db = db;
|
this.db = db;
|
||||||
|
getWiperContactIds();
|
||||||
|
loadContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean remoteWipeIsSetup() {
|
public boolean remoteWipeIsSetup() {
|
||||||
try {
|
try {
|
||||||
return db.transactionWithResult(true,
|
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) {
|
} catch (DbException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -47,26 +79,12 @@ public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
public List<ContactId> getWiperContactIds() {
|
public List<ContactId> getWiperContactIds() {
|
||||||
try {
|
try {
|
||||||
return db.transactionWithResult(true,
|
wiperContactIds = db.transactionWithResult(true,
|
||||||
remoteWipeManager::getWiperContactIds);
|
remoteWipeManager::getWiperContactIds);
|
||||||
} catch (DbException ignored) {
|
} catch (DbException ignored) {
|
||||||
return new ArrayList<ContactId>();
|
return new ArrayList<ContactId>();
|
||||||
}
|
}
|
||||||
}
|
return wiperContactIds;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -86,7 +104,7 @@ public class RemoteWipeSetupViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
public void onModifyWipers() {
|
public void onModifyWipers() {
|
||||||
state.postValue(RemoteWipeSetupState.MODIFY);
|
state.postValue(RemoteWipeSetupState.MODIFY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupRemoteWipe(Collection<ContactId> wipers)
|
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() {
|
public MutableLiveData<RemoteWipeSetupState> getState() {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user