Commit 33844774 authored by Torsten Grote's avatar Torsten Grote

[android] Add BriarSnackbarBuilder to standardize snackbar creation

parent 58ffc6e7
......@@ -5,8 +5,6 @@ import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
......@@ -34,6 +32,7 @@ import org.briarproject.briar.android.controller.handler.UiResultExceptionHandle
import org.briarproject.briar.android.fragment.BaseFragment;
import org.briarproject.briar.android.sharing.BlogSharingStatusActivity;
import org.briarproject.briar.android.sharing.ShareBlogActivity;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.api.blog.BlogPostHeader;
......@@ -44,6 +43,7 @@ import javax.inject.Inject;
import static android.app.Activity.RESULT_OK;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.support.design.widget.Snackbar.LENGTH_LONG;
import static android.widget.Toast.LENGTH_SHORT;
import static java.util.Objects.requireNonNull;
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
......@@ -356,17 +356,12 @@ public class BlogFragment extends BaseFragment
}
private void displaySnackbar(int stringId, boolean scroll) {
Snackbar snackbar =
Snackbar.make(list, stringId, Snackbar.LENGTH_LONG);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
BriarSnackbarBuilder sb = new BriarSnackbarBuilder();
if (scroll) {
View.OnClickListener onClick = v -> list.smoothScrollToPosition(0);
snackbar.setActionTextColor(ContextCompat
.getColor(getContext(),
R.color.briar_button_text_positive));
snackbar.setAction(R.string.blogs_blog_post_scroll_to, onClick);
sb.setAction(R.string.blogs_blog_post_scroll_to,
v -> list.smoothScrollToPosition(0));
}
snackbar.show();
sb.make(list, stringId, LENGTH_LONG).show();
}
private void showDeleteDialog() {
......
......@@ -4,15 +4,12 @@ import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import org.briarproject.bramble.api.db.DbException;
......@@ -23,6 +20,7 @@ import org.briarproject.briar.android.activity.ActivityComponent;
import org.briarproject.briar.android.blog.FeedController.FeedListener;
import org.briarproject.briar.android.controller.handler.UiResultExceptionHandler;
import org.briarproject.briar.android.fragment.BaseFragment;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.api.blog.Blog;
import org.briarproject.briar.api.blog.BlogPostHeader;
......@@ -270,16 +268,12 @@ public class FeedFragment extends BaseFragment implements
int count = adapter.getItemCount();
boolean scroll = count > (lastVisible - firstVisible + 1);
Snackbar s = Snackbar.make(list, stringRes, LENGTH_LONG);
s.getView().setBackgroundResource(R.color.briar_primary);
BriarSnackbarBuilder sb = new BriarSnackbarBuilder();
if (scroll) {
OnClickListener onClick = v -> list.smoothScrollToPosition(0);
s.setActionTextColor(ContextCompat
.getColor(getContext(),
R.color.briar_button_text_positive));
s.setAction(R.string.blogs_blog_post_scroll_to, onClick);
sb.setAction(R.string.blogs_blog_post_scroll_to,
v -> list.smoothScrollToPosition(0));
}
s.show();
sb.make(list, stringRes, LENGTH_LONG).show();
}
@Override
......
......@@ -7,7 +7,6 @@ import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.Pair;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
......@@ -39,6 +38,7 @@ import org.briarproject.briar.android.contact.add.remote.PendingRequestsActivity
import org.briarproject.briar.android.conversation.ConversationActivity;
import org.briarproject.briar.android.fragment.BaseFragment;
import org.briarproject.briar.android.keyagreement.ContactExchangeActivity;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.api.android.AndroidNotificationManager;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
......@@ -162,14 +162,12 @@ public class ContactListFragment extends BaseFragment implements EventListener,
list.setEmptyText(getString(R.string.no_contacts));
list.setEmptyAction(getString(R.string.no_contacts_action));
// TODO UiUtils helper method?
snackbar = Snackbar.make(contentView,
R.string.pending_contact_requests_snackbar, LENGTH_INDEFINITE);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.setAction(R.string.show, v -> startActivity(
new Intent(getContext(), PendingRequestsActivity.class)));
snackbar.setActionTextColor(ContextCompat
.getColor(getContext(), R.color.briar_button_text_positive));
snackbar = new BriarSnackbarBuilder()
.setAction(R.string.show, v ->
startActivity(new Intent(getContext(),
PendingRequestsActivity.class)))
.make(contentView, R.string.pending_contact_requests_snackbar,
LENGTH_INDEFINITE);
return contentView;
}
......
......@@ -59,6 +59,7 @@ import org.briarproject.briar.android.conversation.ConversationVisitor.TextCache
import org.briarproject.briar.android.forum.ForumActivity;
import org.briarproject.briar.android.introduction.IntroductionActivity;
import org.briarproject.briar.android.privategroup.conversation.GroupActivity;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.android.view.ImagePreview;
import org.briarproject.briar.android.view.TextAttachmentController;
......@@ -296,10 +297,10 @@ public class ConversationActivity extends BriarActivity
super.onActivityResult(request, result, data);
if (request == REQUEST_INTRODUCTION && result == RESULT_OK) {
Snackbar snackbar = Snackbar.make(list, R.string.introduction_sent,
Snackbar.LENGTH_SHORT);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.show();
new BriarSnackbarBuilder()
.make(list, R.string.introduction_sent,
Snackbar.LENGTH_SHORT)
.show();
} else if (request == REQUEST_ATTACH_IMAGE && result == RESULT_OK) {
// remove cast when removing FEATURE_FLAG_IMAGE_ATTACHMENTS
((TextAttachmentController) sendController).onImageReceived(data);
......
......@@ -9,7 +9,6 @@ import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
......@@ -32,6 +31,7 @@ import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.ActivityComponent;
import org.briarproject.briar.android.activity.BriarActivity;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.PullDownLayout;
import java.text.SimpleDateFormat;
......@@ -308,9 +308,10 @@ public class ImageActivity extends BriarActivity
R.string.save_image_error : R.string.save_image_success;
int colorRes = error ?
R.color.briar_red : R.color.briar_primary;
Snackbar s = Snackbar.make(layout, stringRes, LENGTH_LONG);
s.getView().setBackgroundResource(colorRes);
s.show();
new BriarSnackbarBuilder()
.setBackgroundColor(colorRes)
.make(layout, stringRes, LENGTH_LONG)
.show();
viewModel.onSaveStateSeen();
}
......
......@@ -4,7 +4,6 @@ import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
......@@ -27,6 +26,7 @@ import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.ActivityComponent;
import org.briarproject.briar.android.fragment.BaseEventFragment;
import org.briarproject.briar.android.sharing.ForumInvitationActivity;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.api.android.AndroidNotificationManager;
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
......@@ -105,11 +105,9 @@ public class ForumListFragment extends BaseEventFragment implements
list.setLayoutManager(new LinearLayoutManager(getActivity()));
list.setAdapter(adapter);
snackbar = Snackbar.make(list, "", LENGTH_INDEFINITE);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.setAction(R.string.show, this);
snackbar.setActionTextColor(ContextCompat
.getColor(getActivity(), R.color.briar_button_text_positive));
snackbar = new BriarSnackbarBuilder()
.setAction(R.string.show, this)
.make(list, "", LENGTH_INDEFINITE);
return contentView;
}
......
......@@ -4,7 +4,6 @@ import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
......@@ -27,6 +26,7 @@ import org.briarproject.briar.android.privategroup.creation.CreateGroupActivity;
import org.briarproject.briar.android.privategroup.invitation.GroupInvitationActivity;
import org.briarproject.briar.android.privategroup.list.GroupListController.GroupListListener;
import org.briarproject.briar.android.privategroup.list.GroupViewHolder.OnGroupRemoveClickListener;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.api.privategroup.GroupMessageHeader;
......@@ -82,11 +82,9 @@ public class GroupListFragment extends BaseFragment implements
list.setLayoutManager(new LinearLayoutManager(getContext()));
list.setAdapter(adapter);
snackbar = Snackbar.make(list, "", LENGTH_INDEFINITE);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.setAction(R.string.show, this);
snackbar.setActionTextColor(ContextCompat
.getColor(getActivity(), R.color.briar_button_text_positive));
snackbar = new BriarSnackbarBuilder()
.setAction(R.string.show, this)
.make(list, "", LENGTH_INDEFINITE);
return v;
}
......
......@@ -26,6 +26,7 @@ import org.briarproject.briar.android.controller.handler.UiResultExceptionHandle
import org.briarproject.briar.android.threaded.ThreadItemAdapter.ThreadItemListener;
import org.briarproject.briar.android.threaded.ThreadListController.ThreadListDataSource;
import org.briarproject.briar.android.threaded.ThreadListController.ThreadListListener;
import org.briarproject.briar.android.util.BriarSnackbarBuilder;
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.android.view.KeyboardAwareLinearLayout;
import org.briarproject.briar.android.view.TextInputView;
......@@ -41,7 +42,6 @@ import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static android.support.design.widget.Snackbar.make;
import static android.support.v7.widget.RecyclerView.NO_POSITION;
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
......@@ -324,9 +324,9 @@ public abstract class ThreadListActivity<G extends NamedGroup, I extends ThreadI
}
protected void displaySnackbar(@StringRes int stringId) {
Snackbar snackbar = make(list, stringId, Snackbar.LENGTH_SHORT);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.show();
new BriarSnackbarBuilder()
.make(list, stringId, Snackbar.LENGTH_SHORT)
.show();
}
private void updateTextInput() {
......
package org.briarproject.briar.android.util;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.View.OnClickListener;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.R;
import static android.support.v4.content.ContextCompat.getColor;
@NotNullByDefault
public class BriarSnackbarBuilder {
@ColorRes
private int backgroundResId = R.color.briar_primary;
@StringRes
private int actionResId;
@Nullable
private OnClickListener onClickListener;
public Snackbar make(View view, CharSequence text, int duration) {
Snackbar s = Snackbar.make(view, text, duration);
s.getView().setBackgroundResource(backgroundResId);
if (onClickListener != null) {
s.setActionTextColor(getColor(view.getContext(),
R.color.briar_button_text_positive));
s.setAction(actionResId, onClickListener);
}
return s;
}
public Snackbar make(View view, @StringRes int resId, int duration) {
return make(view, view.getResources().getText(resId), duration);
}
public BriarSnackbarBuilder setBackgroundColor(
@ColorRes int backgroundResId) {
this.backgroundResId = backgroundResId;
return this;
}
public BriarSnackbarBuilder setAction(@StringRes int actionResId,
OnClickListener onClickListener) {
this.actionResId = actionResId;
this.onClickListener = onClickListener;
return this;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment