Skip to content

Commit

Permalink
Create NewTabView with NewTabAdapter that shows bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
haanhvu committed Oct 22, 2024
1 parent b9de8a1 commit 218b7b8
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 6 deletions.
24 changes: 24 additions & 0 deletions app/src/common/shared/com/igalia/wolvic/ui/adapters/Bookmark.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.igalia.wolvic.ui.adapters;

import android.util.Log;

import androidx.annotation.NonNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -131,6 +133,28 @@ private static List<Bookmark> getDisplayListTree(@NonNull List<BookmarkNode> boo
return children;
}

/*public static List<Bookmark> getBookmarkItems(@NonNull List<BookmarkNode> bookmarkNodes) {
Log.e("New Tab", "Bookmark nodes size: " + bookmarkNodes.size());
ArrayList<Bookmark> bookmarkItems = new ArrayList<>();
for (BookmarkNode node : bookmarkNodes) {
if (node.getType() == BookmarkNodeType.ITEM) {
//if (node.getTitle() != null) {
//if (node.getType() != BookmarkNodeType.FOLDER && node.getTitle() != null) {
Log.e("New Tab", "Bookmark node's item type recognized");
Bookmark bookmark = new Bookmark(node, 0, false);
bookmarkItems.add(bookmark);
} else {
Log.e("New Tab", "Not item type. Real type: " + node.getType());
}
}
Log.e("New Tab", "Bookmark items size: " + bookmarkItems.size());
return bookmarkItems;
}*/

/**
* Traverses the current display list looking for opened folders
* @param displayList
Expand Down
169 changes: 169 additions & 0 deletions app/src/common/shared/com/igalia/wolvic/ui/adapters/NewTabAdapter.java
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 app/src/common/shared/com/igalia/wolvic/ui/views/NewTabView.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private void updateUI() {
if (homeUri != "") {
getSession().loadUri(homeUri);
} else {
mAttachedWindow.showPanel(Windows.BOOKMARKS);
mAttachedWindow.showNewTab();
}

if (mAudio != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.igalia.wolvic.telemetry.TelemetryService;
import com.igalia.wolvic.ui.adapters.WebApp;
import com.igalia.wolvic.ui.viewmodel.WindowViewModel;
import com.igalia.wolvic.ui.views.NewTabView;
import com.igalia.wolvic.ui.views.library.LibraryPanel;
import com.igalia.wolvic.ui.widgets.dialogs.PromptDialogWidget;
import com.igalia.wolvic.ui.widgets.dialogs.SelectionActionWidget;
Expand Down Expand Up @@ -134,6 +135,7 @@ public class WindowWidget extends UIWidget implements SessionChangeListener,
private Session mSession;
private int mWindowId;
private LibraryPanel mLibrary;
private NewTabView mNewTab;
private Windows.WindowPlacement mWindowPlacement = Windows.WindowPlacement.FRONT;
private Windows.WindowPlacement mWindowPlacementBeforeFullscreen = Windows.WindowPlacement.FRONT;
private float mMaxWindowScale = 3;
Expand Down Expand Up @@ -217,6 +219,7 @@ private void initialize(Context aContext) {
setupListeners(mSession);

mLibrary = new LibraryPanel(aContext);
mNewTab = new NewTabView(aContext);

SessionStore.get().getBookmarkStore().addListener(mBookmarksListener);

Expand Down Expand Up @@ -404,7 +407,8 @@ public void loadHome() {

} else {
if (mSession.getHomeUri() == "") {
showPanel(Windows.BOOKMARKS);
//showPanel(Windows.BOOKMARKS);
showNewTab();
} else {
mSession.loadUri(SettingsStore.getInstance(getContext()).getHomepage());
}
Expand Down Expand Up @@ -527,15 +531,39 @@ public void showPanel(@Windows.PanelType int panelType) {
showPanel(panelType, true);
}

public void showPanel(@Windows.PanelType int panelType, boolean switchSurface) {
public void showNewTab() {
if (mNewTab != null) {
if (mView == null) {
setView(mNewTab, true);
mViewModel.setIsFindInPage(false);
//mViewModel.setIsPanelVisible(true);
//if (mRestoreFirstPaint == null && !isFirstPaintReady() && (mFirstDrawCallback != null) && (mSurface != null)) {
final Runnable firstDrawCallback = mFirstDrawCallback;
onFirstContentfulPaint(mSession.getWSession());
mRestoreFirstPaint = () -> {
setFirstPaintReady(false);
setFirstDrawCallback(firstDrawCallback);
if (mWidgetManager != null) {
mWidgetManager.updateWidget(WindowWidget.this);
}
};
//}
}
/*} else if (mView == mLibrary) {
mLibrary.selectPanel(panelType);
}*/
}
}

private void showPanel(@Windows.PanelType int panelType, boolean switchSurface) {
if (mLibrary != null) {
if (mView == null) {
setView(mLibrary, switchSurface);
mLibrary.selectPanel(panelType);
mLibrary.onShow();
mViewModel.setIsFindInPage(false);
mViewModel.setIsPanelVisible(true);
//if (mRestoreFirstPaint == null && !isFirstPaintReady() && (mFirstDrawCallback != null) && (mSurface != null)) {
if (mRestoreFirstPaint == null && !isFirstPaintReady() && (mFirstDrawCallback != null) && (mSurface != null)) {
final Runnable firstDrawCallback = mFirstDrawCallback;
onFirstContentfulPaint(mSession.getWSession());
mRestoreFirstPaint = () -> {
Expand All @@ -545,7 +573,7 @@ public void showPanel(@Windows.PanelType int panelType, boolean switchSurface) {
mWidgetManager.updateWidget(WindowWidget.this);
}
};

}
} else if (mView == mLibrary) {
mLibrary.selectPanel(panelType);
}
Expand Down
Loading

0 comments on commit 218b7b8

Please sign in to comment.