-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create NewTabView with NewTabAdapter that shows bookmarks
- Loading branch information
Showing
7 changed files
with
338 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
app/src/common/shared/com/igalia/wolvic/ui/adapters/NewTabAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package com.igalia.wolvic.ui.adapters; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.MotionEvent; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.databinding.DataBindingUtil; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
import com.igalia.wolvic.R; | ||
import com.igalia.wolvic.browser.engine.SessionStore; | ||
import com.igalia.wolvic.databinding.BookmarkItemBinding; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import mozilla.appservices.places.BookmarkRoot; | ||
import mozilla.components.browser.icons.IconRequest; | ||
import mozilla.components.concept.storage.BookmarkNode; | ||
|
||
public class NewTabAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | ||
|
||
private List<Bookmark> bookmarkItems; | ||
|
||
public NewTabAdapter() { | ||
|
||
} | ||
|
||
public void setBookmarkListInNewTab(final List<BookmarkNode> bookmarkNodes) { | ||
if (bookmarkItems == null || bookmarkItems.isEmpty()) { | ||
//bookmarkItems = Bookmark.getBookmarkItems(bookmarkNodes); | ||
|
||
List<Bookmark> newDisplayList; | ||
bookmarkItems = new ArrayList<>(); | ||
newDisplayList = Bookmark.getDisplayListTree(bookmarkNodes, Collections.singletonList(BookmarkRoot.Mobile.getId())); | ||
for (Bookmark node : newDisplayList) { | ||
if (node.getType() == Bookmark.Type.ITEM) { | ||
bookmarkItems.add(node); | ||
} | ||
} | ||
|
||
notifyItemRangeInserted(0, bookmarkItems.size()); | ||
} else { | ||
Log.e("New Tab", "Set bookmarks: Second case - not implemented yet"); | ||
} | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
BookmarkItemBinding binding = DataBindingUtil | ||
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_item, | ||
parent, false); | ||
|
||
/*binding.setCallback(mBookmarkItemCallback); | ||
binding.setIsHovered(false); | ||
binding.setIsNarrow(mIsNarrowLayout);*/ | ||
|
||
return new BookmarkAdapter.BookmarkViewHolder(binding); | ||
} | ||
|
||
@SuppressLint("ClickableViewAccessibility") | ||
@Override | ||
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { | ||
Bookmark item = bookmarkItems.get(position); | ||
|
||
BookmarkAdapter.BookmarkViewHolder bookmarkHolder = (BookmarkAdapter.BookmarkViewHolder) holder; | ||
BookmarkItemBinding binding = bookmarkHolder.binding; | ||
binding.setItem(item); | ||
//binding.setIsNarrow(mIsNarrowLayout); | ||
|
||
// Load favicon | ||
SessionStore.get().getBrowserIcons().loadIntoView(binding.favicon, item.getUrl(), IconRequest.Size.DEFAULT); | ||
|
||
binding.layout.setOnHoverListener((view, motionEvent) -> { | ||
int ev = motionEvent.getActionMasked(); | ||
switch (ev) { | ||
case MotionEvent.ACTION_HOVER_ENTER: | ||
binding.setIsHovered(true); | ||
view.getBackground().setState(new int[]{android.R.attr.state_hovered}); | ||
view.postInvalidate(); | ||
return false; | ||
|
||
case MotionEvent.ACTION_CANCEL: | ||
case MotionEvent.ACTION_HOVER_EXIT: | ||
view.getBackground().setState(new int[]{android.R.attr.state_active}); | ||
binding.setIsHovered(false); | ||
view.postInvalidate(); | ||
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.more.setImageState(new int[]{android.R.attr.state_active},false); | ||
binding.trash.setImageState(new int[]{android.R.attr.state_active},false); | ||
binding.setIsHovered(true); | ||
return false; | ||
|
||
case MotionEvent.ACTION_CANCEL: | ||
binding.setIsHovered(false); | ||
return false; | ||
} | ||
return false; | ||
}); | ||
//binding.more.setOnHoverListener(mIconHoverListener); | ||
binding.more.setOnTouchListener((view, motionEvent) -> { | ||
binding.setIsHovered(true); | ||
int ev = motionEvent.getActionMasked(); | ||
switch (ev) { | ||
case MotionEvent.ACTION_UP: | ||
/*if (mBookmarkItemCallback != null) { | ||
mBookmarkItemCallback.onMore(view, binding.getItem()); | ||
}*/ | ||
binding.more.setImageState(new int[]{android.R.attr.state_active},true); | ||
return true; | ||
|
||
case MotionEvent.ACTION_DOWN: | ||
binding.more.setImageState(new int[]{android.R.attr.state_pressed},true); | ||
return true; | ||
|
||
case MotionEvent.ACTION_CANCEL: | ||
binding.setIsHovered(false); | ||
binding.more.setImageState(new int[]{android.R.attr.state_active},true); | ||
return false; | ||
} | ||
return false; | ||
}); | ||
//binding.trash.setOnHoverListener(mIconHoverListener); | ||
binding.trash.setOnTouchListener((view, motionEvent) -> { | ||
binding.setIsHovered(true); | ||
int ev = motionEvent.getActionMasked(); | ||
switch (ev) { | ||
case MotionEvent.ACTION_UP: | ||
/*if (mBookmarkItemCallback != null) { | ||
mBookmarkItemCallback.onDelete(view, binding.getItem()); | ||
}*/ | ||
binding.trash.setImageState(new int[]{android.R.attr.state_active},true); | ||
return true; | ||
|
||
case MotionEvent.ACTION_DOWN: | ||
binding.trash.setImageState(new int[]{android.R.attr.state_pressed},true); | ||
return true; | ||
|
||
case MotionEvent.ACTION_CANCEL: | ||
binding.setIsHovered(false); | ||
binding.trash.setImageState(new int[]{android.R.attr.state_active},true); | ||
return false; | ||
} | ||
return false; | ||
}); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return bookmarkItems == null ? 0 : bookmarkItems.size(); | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
app/src/common/shared/com/igalia/wolvic/ui/views/NewTabView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.igalia.wolvic.ui.views; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Executor; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.FrameLayout; | ||
|
||
import androidx.databinding.DataBindingUtil; | ||
|
||
import com.igalia.wolvic.R; | ||
import com.igalia.wolvic.VRBrowserActivity; | ||
import com.igalia.wolvic.VRBrowserApplication; | ||
import com.igalia.wolvic.browser.engine.SessionStore; | ||
import com.igalia.wolvic.databinding.NewTab1Binding; | ||
import com.igalia.wolvic.ui.adapters.NewTabAdapter; | ||
|
||
import mozilla.appservices.places.BookmarkRoot; | ||
import mozilla.components.concept.storage.BookmarkNode; | ||
|
||
public class NewTabView extends FrameLayout { | ||
|
||
private NewTab1Binding mBinding; | ||
|
||
private NewTabAdapter mNewTabAdapter; | ||
|
||
public NewTabView(Context context) { | ||
super(context); | ||
initialize(); | ||
} | ||
|
||
protected void initialize() { | ||
updateUI(); | ||
} | ||
|
||
@SuppressLint("ClickableViewAccessibility") | ||
public void updateUI() { | ||
removeAllViews(); | ||
|
||
LayoutInflater inflater = LayoutInflater.from(getContext()); | ||
|
||
mBinding = DataBindingUtil.inflate(inflater, R.layout.new_tab_1, this, true); | ||
mBinding.setLifecycleOwner((VRBrowserActivity)getContext()); | ||
|
||
mNewTabAdapter = new NewTabAdapter(); | ||
mBinding.bookmarksList.setAdapter(mNewTabAdapter); | ||
mBinding.bookmarksList.setOnTouchListener((v, event) -> { | ||
v.requestFocusFromTouch(); | ||
return false; | ||
}); | ||
mBinding.bookmarksList.setHasFixedSize(true); | ||
mBinding.bookmarksList.setItemViewCacheSize(20); | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { | ||
mBinding.bookmarksList.setDrawingCacheEnabled(true); | ||
mBinding.bookmarksList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); | ||
} | ||
|
||
mBinding.executePendingBindings(); | ||
|
||
updateBookmarks(); | ||
|
||
setOnTouchListener((v, event) -> { | ||
v.requestFocusFromTouch(); | ||
return false; | ||
}); | ||
} | ||
|
||
private void updateBookmarks() { | ||
Executor executor = ((VRBrowserApplication)getContext().getApplicationContext()).getExecutors().mainThread(); | ||
|
||
SessionStore.get().getBookmarkStore().getTree(BookmarkRoot.Root.getId(), true). | ||
thenAcceptAsync(this::showBookmarks, executor). | ||
exceptionally(throwable -> { | ||
Log.d("NewTab", "Error getting bookmarks: " + throwable.getLocalizedMessage()); | ||
throwable.printStackTrace(); | ||
return null; | ||
}); | ||
} | ||
|
||
private void showBookmarks(List<BookmarkNode> aBookmarks) { | ||
mNewTabAdapter.setBookmarkListInNewTab(aBookmarks); | ||
mBinding.executePendingBindings(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.