Use Inheritence for shared Forum and Blog Sharing Code

This commit is contained in:
Torsten Grote
2016-08-03 13:00:24 -03:00
parent a3b2358164
commit a4cf91fba5
29 changed files with 755 additions and 466 deletions

View File

@@ -2,22 +2,17 @@ package org.briarproject.android.sharing;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.briarproject.R;
import org.briarproject.android.ActivityComponent;
import org.briarproject.android.fragment.BaseFragment;
import org.briarproject.api.blogs.BlogManager;
import org.briarproject.api.blogs.BlogSharingManager;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.forum.ForumSharingManager;
import org.briarproject.api.sync.GroupId;
@@ -27,24 +22,18 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static android.widget.Toast.LENGTH_SHORT;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.sharing.ShareActivity.BLOG;
import static org.briarproject.android.sharing.ShareActivity.CONTACTS;
import static org.briarproject.android.sharing.ShareActivity.FORUM;
import static org.briarproject.android.sharing.ShareActivity.SHAREABLE;
import static org.briarproject.android.sharing.ShareActivity.getContactsFromIds;
import static org.briarproject.api.sharing.SharingConstants.GROUP_ID;
public class ShareMessageFragment extends BaseFragment {
abstract class ShareMessageFragment extends BaseFragment {
public final static String TAG = "IntroductionMessageFragment";
public final static String TAG = ShareMessageFragment.class.getName();
private static final Logger LOG =
Logger.getLogger(ShareMessageFragment.class.getName());
protected static final Logger LOG = Logger.getLogger(TAG);
protected ViewHolder ui;
private ShareActivity shareActivity;
private ViewHolder ui;
// Fields that are accessed from background threads must be volatile
@Inject
@@ -52,19 +41,15 @@ public class ShareMessageFragment extends BaseFragment {
@Inject
protected volatile BlogSharingManager blogSharingManager;
private volatile GroupId groupId;
private volatile int shareable;
private volatile Collection<ContactId> contacts;
public static ShareMessageFragment newInstance(int shareable,
GroupId groupId, Collection<ContactId> contacts) {
protected static Bundle getArguments(GroupId groupId,
Collection<ContactId> contacts) {
Bundle args = new Bundle();
args.putByteArray(GROUP_ID, groupId.getBytes());
args.putInt(SHAREABLE, shareable);
args.putIntegerArrayList(CONTACTS, getContactsFromIds(contacts));
ShareMessageFragment fragment = new ShareMessageFragment();
fragment.setArguments(args);
return fragment;
return args;
}
@Override
@@ -82,29 +67,16 @@ public class ShareMessageFragment extends BaseFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// allow for home button to act as back button
// allow for "up" button to act as back button
setHasOptionsMenu(true);
// get groupID, shareable type and contactIDs from fragment arguments
// get groupID and contactIDs from fragment arguments
groupId = new GroupId(getArguments().getByteArray(GROUP_ID));
shareable = getArguments().getInt(SHAREABLE);
ArrayList<Integer> intContacts =
getArguments().getIntegerArrayList(CONTACTS);
if (intContacts == null) throw new IllegalArgumentException();
contacts = ShareActivity.getContactsFromIntegers(intContacts);
// change toolbar text
ActionBar actionBar = shareActivity.getSupportActionBar();
if (actionBar != null) {
if (shareable == FORUM) {
actionBar.setTitle(R.string.forum_share_button);
} else if (shareable == BLOG) {
actionBar.setTitle(R.string.blogs_sharing_button);
} else {
throw new IllegalArgumentException("Invalid Shareable Type!");
}
}
// inflate view
View v = inflater.inflate(R.layout.fragment_share_message, container,
false);
@@ -115,9 +87,6 @@ public class ShareMessageFragment extends BaseFragment {
onButtonClick();
}
});
if (shareable == BLOG) {
ui.button.setText(getString(R.string.blogs_sharing_button));
}
return v;
}
@@ -138,9 +107,8 @@ public class ShareMessageFragment extends BaseFragment {
return TAG;
}
@Override
public void injectFragment(ActivityComponent component) {
component.inject(this);
protected void setTitle(int res) {
shareActivity.setTitle(res);
}
private void onButtonClick() {
@@ -148,49 +116,31 @@ public class ShareMessageFragment extends BaseFragment {
ui.button.setEnabled(false);
String msg = ui.message.getText().toString();
shareForum(msg);
share(msg);
// don't wait for the introduction to be made before finishing activity
// don't wait for the invitation to be made before finishing activity
shareActivity.sharingSuccessful(ui.message);
}
private void shareForum(final String msg) {
listener.runOnDbThread(new Runnable() {
@Override
public void run() {
try {
for (ContactId c : contacts) {
if (shareable == FORUM) {
forumSharingManager.sendInvitation(groupId, c,
msg);
} else if (shareable == BLOG) {
blogSharingManager.sendInvitation(groupId, c, msg);
}
}
} catch (DbException e) {
sharingError();
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
abstract void share(final String msg);
abstract void sharingError();
protected Collection<ContactId> getContacts() {
return contacts;
}
private void sharingError() {
shareActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
int res = R.string.forum_share_error;
if (shareable == BLOG) res = R.string.blogs_sharing_error;
Toast.makeText(shareActivity, res, LENGTH_SHORT).show();
}
});
protected GroupId getGroupId() {
return groupId;
}
private static class ViewHolder {
protected void runOnUiThread(Runnable runnable) {
listener.runOnUiThread(runnable);
}
private final EditText message;
private final Button button;
protected static class ViewHolder {
protected final EditText message;
protected final Button button;
ViewHolder(View v) {
message = (EditText) v.findViewById(R.id.invitationMessageView);