Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Update Bookmarks UI (#1704)
Browse files Browse the repository at this point in the history
* Update Bookmarks UI

* Fixed crash when scrolling
  • Loading branch information
keianhzo authored and bluemarvin committed Aug 29, 2019
1 parent 1e0af3e commit 357f7e1
Show file tree
Hide file tree
Showing 39 changed files with 975 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BookmarksStore constructor(val context: Context) {

interface BookmarkListener {
fun onBookmarksUpdated()
fun onBookmarkAdded()
}

fun addListener(aListener: BookmarkListener) {
Expand All @@ -44,6 +45,7 @@ class BookmarksStore constructor(val context: Context) {
fun addBookmark(aURL: String, aTitle: String) = GlobalScope.future {
storage.addItem(BookmarkRoot.Mobile.id, aURL, aTitle, null)
notifyListeners()
notifyAddedListeners()
}

fun deleteBookmarkByURL(aURL: String) = GlobalScope.future {
Expand Down Expand Up @@ -89,4 +91,15 @@ class BookmarksStore constructor(val context: Context) {
}
}
}

private fun notifyAddedListeners() {
if (listeners.size > 0) {
val listenersCopy = ArrayList(listeners)
Handler(Looper.getMainLooper()).post {
for (listener in listenersCopy) {
listener.onBookmarkAdded()
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.mozilla.vrbrowser.ui.adapters;

import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class BindingAdapters {
Expand Down Expand Up @@ -53,4 +56,13 @@ public static void bindDate(@NonNull TextView textView, long timestamp) {
}
textView.setText(androidDateTime.concat(AmPm));
}

@BindingAdapter(value={"textDrawable", "textString"})
public static void setSpannableString(@NonNull TextView textView, Drawable drawable, String text) {
SpannableString spannableString = new SpannableString(text);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
spannableString.setSpan(span, spannableString.toString().indexOf("@"), spannableString.toString().indexOf("@")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.databinding.BookmarkItemBinding;
import org.mozilla.vrbrowser.ui.callbacks.BookmarkClickCallback;
import org.mozilla.vrbrowser.ui.callbacks.BookmarkItemCallback;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.AnimationHelper;

Expand All @@ -39,13 +39,13 @@ public class BookmarkAdapter extends RecyclerView.Adapter<BookmarkAdapter.Bookma
private int mIconNormalColor;

@Nullable
private final BookmarkClickCallback mBookmarkClickCallback;
private final BookmarkItemCallback mBookmarkItemCallback;

public BookmarkAdapter(@Nullable BookmarkClickCallback clickCallback, Context aContext) {
mBookmarkClickCallback = clickCallback;
public BookmarkAdapter(@Nullable BookmarkItemCallback clickCallback, Context aContext) {
mBookmarkItemCallback = clickCallback;

mMinPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.tray_icon_padding_min);
mMaxPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.tray_icon_padding_max);
mMinPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.library_icon_padding_min);
mMaxPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.library_icon_padding_max);

mIconColorHover = aContext.getResources().getColor(R.color.white, aContext.getTheme());
mIconNormalColor = aContext.getResources().getColor(R.color.rhino, aContext.getTheme());
Expand Down Expand Up @@ -102,19 +102,66 @@ public int itemCount() {
return mBookmarkList != null ? mBookmarkList.size() : 0;
}

public int getItemPosition(String id) {
for (int position=0; position<mBookmarkList.size(); position++)
if (mBookmarkList.get(position).getGuid() == id)
return position;
return 0;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public BookmarkViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
BookmarkItemBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_item,
parent, false);
binding.setCallback(mBookmarkClickCallback);
binding.setCallback(mBookmarkItemCallback);
binding.setIsHovered(false);
binding.layout.setOnHoverListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
binding.setIsHovered(true);
return false;

case MotionEvent.ACTION_HOVER_EXIT:
binding.setIsHovered(false);
return false;
}

return false;
});
binding.layout.setOnTouchListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_UP:
return false;

case MotionEvent.ACTION_DOWN:
binding.setIsHovered(true);
return false;
}
return false;
});
binding.more.setOnHoverListener(mIconHoverListener);
binding.more.setOnTouchListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_UP:
mBookmarkItemCallback.onMore(view, binding.getItem());
return true;

case MotionEvent.ACTION_DOWN:
return true;
}
return false;
});
binding.trash.setOnHoverListener(mIconHoverListener);
binding.trash.setOnTouchListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_UP:
mBookmarkClickCallback.onDelete(binding.getBookmark());
mBookmarkItemCallback.onDelete(view, binding.getItem());
return true;

case MotionEvent.ACTION_DOWN:
Expand All @@ -127,7 +174,7 @@ public BookmarkViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int view

@Override
public void onBindViewHolder(@NonNull BookmarkViewHolder holder, int position) {
holder.binding.setBookmark(mBookmarkList.get(position));
holder.binding.setItem(mBookmarkList.get(position));
holder.binding.executePendingBindings();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryI
public HistoryAdapter(@Nullable HistoryItemCallback clickCallback, Context aContext) {
mHistoryItemCallback = clickCallback;

mMinPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.settings_icon_padding_min);
mMaxPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.settings_icon_padding_max);
mMinPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.library_icon_padding_min);
mMaxPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.library_icon_padding_max);

mIconColorHover = aContext.getResources().getColor(R.color.white, aContext.getTheme());
mIconNormalColor = aContext.getResources().getColor(R.color.rhino, aContext.getTheme());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mozilla.vrbrowser.ui.callbacks;

import android.view.View;

import mozilla.components.concept.storage.BookmarkNode;

public interface BookmarkItemCallback {
void onClick(View view, BookmarkNode item);
void onDelete(View view, BookmarkNode item);
void onMore(View view, BookmarkNode item);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.mozilla.vrbrowser.ui.callbacks;

import android.view.View;

import androidx.annotation.NonNull;

import mozilla.components.concept.storage.BookmarkNode;

public interface BookmarksCallback {
void onClearBookmarks(@NonNull View view);
void onShowContextMenu(@NonNull View view, BookmarkNode item, boolean isLastVisibleItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import android.view.View;

import androidx.annotation.NonNull;

import mozilla.components.concept.storage.VisitInfo;

public interface HistoryCallback {
void onClearHistory(View view);
void onShowContextMenu(View view, VisitInfo item, boolean isLastVisibleItem);
void onClearHistory(@NonNull View view);
void onShowContextMenu(@NonNull View view, @NonNull VisitInfo item, boolean isLastVisibleItem);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.mozilla.vrbrowser.ui.callbacks;

import org.mozilla.vrbrowser.ui.views.LibraryItemContextMenu;

public interface LibraryItemContextMenuClickCallback {
void onOpenInNewWindowClick(LibraryItemContextMenu.LibraryContextMenuItem item);
void onAddToBookmarks(LibraryItemContextMenu.LibraryContextMenuItem item);
void onRemoveFromBookmarks(LibraryItemContextMenu.LibraryContextMenuItem item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.audio.AudioEngine;
Expand All @@ -19,7 +23,8 @@
import org.mozilla.vrbrowser.browser.engine.SessionStore;
import org.mozilla.vrbrowser.databinding.BookmarksBinding;
import org.mozilla.vrbrowser.ui.adapters.BookmarkAdapter;
import org.mozilla.vrbrowser.ui.callbacks.BookmarkClickCallback;
import org.mozilla.vrbrowser.ui.callbacks.BookmarkItemCallback;
import org.mozilla.vrbrowser.ui.callbacks.BookmarksCallback;
import org.mozilla.vrbrowser.utils.UIThreadExecutor;

import java.util.List;
Expand Down Expand Up @@ -55,48 +60,76 @@ private void initialize(Context aContext) {

// Inflate this data binding layout
mBinding = DataBindingUtil.inflate(inflater, R.layout.bookmarks, this, true);
mBookmarkAdapter = new BookmarkAdapter(mBookmarkClickCallback, aContext);
mBookmarkAdapter = new BookmarkAdapter(mBookmarkItemCallback, aContext);
mBinding.bookmarksList.setAdapter(mBookmarkAdapter);
mBinding.bookmarksList.setOnTouchListener((v, event) -> {
v.requestFocusFromTouch();
return false;
});
mBinding.setIsLoading(true);
mBinding.executePendingBindings();
syncBookmarks();
SessionStore.get().getBookmarkStore().addListener(this);

setVisibility(GONE);

setOnTouchListener((v, event) -> {
v.requestFocusFromTouch();
return false;
});
}

public void onDestroy() {
SessionStore.get().getBookmarkStore().removeListener(this);
}

private final BookmarkClickCallback mBookmarkClickCallback = new BookmarkClickCallback() {
private final BookmarkItemCallback mBookmarkItemCallback = new BookmarkItemCallback() {
@Override
public void onClick(BookmarkNode bookmark) {
if (mAudio != null) {
mAudio.playSound(AudioEngine.Sound.CLICK);
}
public void onClick(View view, BookmarkNode item) {
mBinding.bookmarksList.requestFocusFromTouch();

SessionStack sessionStack = SessionStore.get().getActiveStore();
sessionStack.loadUri(bookmark.getUrl());
sessionStack.loadUri(item.getUrl());
}

@Override
public void onDelete(BookmarkNode bookmark) {
if (mAudio != null) {
mAudio.playSound(AudioEngine.Sound.CLICK);
}
public void onDelete(View view, BookmarkNode item) {
mBinding.bookmarksList.requestFocusFromTouch();

mIgnoreNextListener = true;
SessionStore.get().getBookmarkStore().deleteBookmarkById(bookmark.getGuid());
mBookmarkAdapter.removeItem(bookmark);
SessionStore.get().getBookmarkStore().deleteBookmarkById(item.getGuid());
mBookmarkAdapter.removeItem(item);
if (mBookmarkAdapter.itemCount() == 0) {
mBinding.setIsEmpty(true);
mBinding.setIsLoading(false);
mBinding.executePendingBindings();
}
}

@Override
public void onMore(View view, BookmarkNode item) {
mBinding.bookmarksList.requestFocusFromTouch();

int rowPosition = mBookmarkAdapter.getItemPosition(item.getGuid());
RecyclerView.ViewHolder row = mBinding.bookmarksList.findViewHolderForLayoutPosition(rowPosition);
boolean isLastVisibleItem = false;
if (mBinding.bookmarksList.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.bookmarksList.getLayoutManager();
if (rowPosition == layoutManager.findLastVisibleItemPosition()) {
isLastVisibleItem = true;
}
}

mBinding.getCallback().onShowContextMenu(
row.itemView,
item,
isLastVisibleItem);
}
};

public void setBookmarksCallback(@NonNull BookmarksCallback callback) {
mBinding.setCallback(callback);
}

private void syncBookmarks() {
SessionStore.get().getBookmarkStore().getBookmarks().thenAcceptAsync(this::showBookmarks, new UIThreadExecutor());
Expand All @@ -113,6 +146,8 @@ private void showBookmarks(List<BookmarkNode> aBookmarks) {
mBookmarkAdapter.setBookmarkList(aBookmarks);
}
mBinding.executePendingBindings();
mBinding.bookmarksList.post(() -> mBinding.bookmarksList.smoothScrollToPosition(
mBookmarkAdapter.getItemCount() > 0 ? mBookmarkAdapter.getItemCount() - 1 : 0));
}

// BookmarksStore.BookmarksViewListener
Expand All @@ -124,4 +159,9 @@ public void onBookmarksUpdated() {
}
syncBookmarks();
}

@Override
public void onBookmarkAdded() {
// Nothing to do
}
}
Loading

0 comments on commit 357f7e1

Please sign in to comment.