Make ContactSelectorFragment reusable

This commit is contained in:
Torsten Grote
2016-10-13 10:04:00 -03:00
parent d25f4d1fbe
commit bd1f3fc2bd
11 changed files with 55 additions and 27 deletions

View File

@@ -4,9 +4,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_share_forum"
android:id="@+id/action_contacts_selected"
android:icon="@drawable/ic_check_white"
android:title="@string/forum_share_action"
android:title="@string/contacts_selected"
app:showAsAction="always"/>
</menu>

View File

@@ -171,7 +171,7 @@
<string name="forum_left_toast">Forum wurde verlassen</string>
<!--Forum Sharing-->
<string name="forum_share_button">Forum teilen</string>
<string name="forum_share_action">Teile dieses Forum mit den gewählten Kontakten</string>
<string name="contacts_selected">Teile dieses Forum mit den gewählten Kontakten</string>
<string name="activity_share_toolbar_header">Kontakte auswählen</string>
<string name="no_contacts_selector">Du scheinst hier neu zu sein und noch keine Kontakte zu haben.\n\nBitte komm zurück, wenn du deinen ersten Kontakt hinzugefügt hast.</string>
<string name="forum_shared_snackbar">Forum mit gewählten Kontakten geteilt</string>

View File

@@ -159,7 +159,7 @@
<string name="forum_left_toast">Foro abandonado</string>
<!--Forum Sharing-->
<string name="forum_share_button">Compartir foro</string>
<string name="forum_share_action">Compartir este foro con los contactos seleccionados</string>
<string name="contacts_selected">Compartir este foro con los contactos seleccionados</string>
<string name="activity_share_toolbar_header">Elige contactos</string>
<string name="no_contacts_selector">Parece que eres nuevo aquí y no tienes contactos aún.\n\nPor favor, vuelve cuando hayas añadido tu primer contacto.</string>
<string name="forum_shared_snackbar">Foro compartido con los contactos seleccionados</string>

View File

@@ -136,7 +136,7 @@
<string name="forum_left_toast">Forum lasciato</string>
<!--Forum Sharing-->
<string name="forum_share_button">Condividi Forum</string>
<string name="forum_share_action">Condividi questo forum con i contatti scelti</string>
<string name="contacts_selected">Condividi questo forum con i contatti scelti</string>
<string name="activity_share_toolbar_header">Scegli Contatti</string>
<string name="forum_shared_snackbar">Forum condiviso con i contatti scelti</string>
<string name="forum_share_error">C\'è stato un errore nella condivisione di questo forum.</string>

View File

@@ -171,7 +171,7 @@ Se sentido sozinho aqui? Compartilhe esse fórum com seus contatos!</string>
<string name="forum_left_toast">Saiu do fórum</string>
<!--Forum Sharing-->
<string name="forum_share_button">Compartilhar fórum</string>
<string name="forum_share_action">Compartilhar este fórum com os contatos escolhidos</string>
<string name="contacts_selected">Compartilhar este fórum com os contatos escolhidos</string>
<string name="activity_share_toolbar_header">Escolher contatos</string>
<string name="no_contacts_selector">Parece que você é novo aqui e não tem nenhum contato ainda.
Por favor volte aqui depois de adicionar um contato.</string>

View File

@@ -195,7 +195,7 @@
<!-- Forum Sharing -->
<string name="forum_share_button">Share Forum</string>
<string name="forum_share_action">Share this forum with chosen contacts</string>
<string name="contacts_selected">Contacts selected</string>
<string name="activity_share_toolbar_header">Choose Contacts</string>
<string name="no_contacts_selector">It seems that you are new here and have no contacts yet.\n\nPlease come back here after you added your first contact.</string>
<string name="forum_shared_snackbar">Forum shared with chosen contacts</string>

View File

@@ -23,7 +23,6 @@ import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.contact.ContactManager;
import org.briarproject.api.db.DbException;
import org.briarproject.api.forum.ForumSharingManager;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.sync.GroupId;
@@ -48,7 +47,6 @@ public class ContactSelectorFragment extends BaseFragment implements
public static final String TAG = ContactSelectorFragment.class.getName();
private static final Logger LOG = Logger.getLogger(TAG);
private ShareActivity shareActivity;
private Menu menu;
private BriarRecyclerView list;
private ContactSelectorAdapter adapter;
@@ -59,13 +57,11 @@ public class ContactSelectorFragment extends BaseFragment implements
volatile ContactManager contactManager;
@Inject
volatile IdentityManager identityManager;
@Inject
volatile ForumSharingManager forumSharingManager;
private volatile GroupId groupId;
private volatile ContactSelectorListener listener;
public static ContactSelectorFragment newInstance(GroupId groupId) {
Bundle args = new Bundle();
args.putByteArray(GROUP_ID, groupId.getBytes());
ContactSelectorFragment fragment = new ContactSelectorFragment();
@@ -81,7 +77,7 @@ public class ContactSelectorFragment extends BaseFragment implements
@Override
public void onAttach(Context context) {
super.onAttach(context);
shareActivity = (ShareActivity) context;
listener = (ContactSelectorListener) context;
}
@Override
@@ -139,6 +135,7 @@ public class ContactSelectorFragment extends BaseFragment implements
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (adapter != null) {
selectedContacts = adapter.getSelectedContactIds();
outState.putIntegerArrayList(CONTACTS,
@@ -148,7 +145,7 @@ public class ContactSelectorFragment extends BaseFragment implements
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forum_share_actions, menu);
inflater.inflate(R.menu.contact_selection_actions, menu);
super.onCreateOptionsMenu(menu, inflater);
this.menu = menu;
// hide sharing action initially, if no contact is selected
@@ -160,11 +157,11 @@ public class ContactSelectorFragment extends BaseFragment implements
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
shareActivity.onBackPressed();
listener.onBackPressed();
return true;
case R.id.action_share_forum:
case R.id.action_contacts_selected:
selectedContacts = adapter.getSelectedContactIds();
shareActivity.showMessageScreen(groupId, selectedContacts);
listener.contactsSelected(groupId, selectedContacts);
return true;
default:
return super.onOptionsItemSelected(item);
@@ -185,7 +182,7 @@ public class ContactSelectorFragment extends BaseFragment implements
}
private void loadContacts(@Nullable final Collection<ContactId> selection) {
shareActivity.runOnDbThread(new Runnable() {
listener.runOnDbThread(new Runnable() {
@Override
public void run() {
try {
@@ -199,7 +196,7 @@ public class ContactSelectorFragment extends BaseFragment implements
boolean selected = selection != null &&
selection.contains(c.getId());
// do we have already some sharing with that contact?
boolean disabled = shareActivity.isDisabled(groupId, c);
boolean disabled = listener.isDisabled(groupId, c);
contacts.add(new SelectableContactListItem(c,
localAuthor, groupId, selected, disabled));
}
@@ -216,7 +213,7 @@ public class ContactSelectorFragment extends BaseFragment implements
}
private void displayContacts(final List<ContactListItem> contacts) {
shareActivity.runOnUiThreadUnlessDestroyed(new Runnable() {
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
@Override
public void run() {
if (contacts.isEmpty()) list.showData();
@@ -228,7 +225,7 @@ public class ContactSelectorFragment extends BaseFragment implements
private void updateMenuItem() {
if (menu == null) return;
MenuItem item = menu.findItem(R.id.action_share_forum);
MenuItem item = menu.findItem(R.id.action_contacts_selected);
if (item == null) return;
selectedContacts = adapter.getSelectedContactIds();

View File

@@ -0,0 +1,25 @@
package org.briarproject.android.sharing;
import android.support.annotation.UiThread;
import org.briarproject.android.DestroyableContext;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.sync.GroupId;
import java.util.Collection;
public interface ContactSelectorListener extends DestroyableContext {
void runOnDbThread(Runnable runnable);
boolean isDisabled(GroupId groupId, Contact c) throws DbException;
@UiThread
void contactsSelected(GroupId groupId, Collection<ContactId> contacts);
@UiThread
void onBackPressed();
}

View File

@@ -6,7 +6,7 @@ import android.view.View;
import org.briarproject.R;
import org.briarproject.android.BriarActivity;
import org.briarproject.android.fragment.BaseFragment;
import org.briarproject.android.fragment.BaseFragment.BaseFragmentListener;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
@@ -17,7 +17,7 @@ import java.util.Collection;
import java.util.List;
public abstract class ShareActivity extends BriarActivity implements
BaseFragment.BaseFragmentListener {
BaseFragmentListener, ContactSelectorListener {
final static String CONTACTS = "contacts";
@@ -44,9 +44,15 @@ public abstract class ShareActivity extends BriarActivity implements
abstract ShareMessageFragment getMessageFragment(GroupId groupId,
Collection<ContactId> contacts);
abstract boolean isDisabled(GroupId groupId, Contact c) throws DbException;
/**
* This must only be called from a DbThread
*/
public abstract boolean isDisabled(GroupId groupId, Contact c)
throws DbException;
void showMessageScreen(GroupId groupId, Collection<ContactId> contacts) {
@Override
public void contactsSelected(GroupId groupId,
Collection<ContactId> contacts) {
ShareMessageFragment messageFragment =
getMessageFragment(groupId, contacts);

View File

@@ -29,7 +29,7 @@ public class ShareBlogActivity extends ShareActivity {
/**
* This must only be called from a DbThread
*/
boolean isDisabled(GroupId groupId, Contact c) throws DbException {
public boolean isDisabled(GroupId groupId, Contact c) throws DbException {
return !blogSharingManager.canBeShared(groupId, c);
}
}

View File

@@ -28,7 +28,7 @@ public class ShareForumActivity extends ShareActivity {
/**
* This must only be called from a DbThread
*/
boolean isDisabled(GroupId groupId, Contact c) throws DbException {
public boolean isDisabled(GroupId groupId, Contact c) throws DbException {
return !forumSharingManager.canBeShared(groupId, c);
}
}