Skip to content

Commit

Permalink
Create New Tab page and add an option to set New Tab page as homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
haanhvu committed Nov 18, 2024
1 parent 86abf88 commit 96768a2
Show file tree
Hide file tree
Showing 13 changed files with 706 additions and 16 deletions.
29 changes: 29 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 All @@ -25,6 +27,11 @@ public enum Type {
private Type mType;
private boolean mHasChildren;

public Bookmark(String title, String url) {
mTitle = title;
mURL = url;
}

public Bookmark(@NonNull BookmarkNode node, int level, boolean isExpanded) {
mIsExpanded = isExpanded;
mLevel = level;
Expand Down Expand Up @@ -131,6 +138,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
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ static class BookmarkViewHolder extends RecyclerView.ViewHolder {
}

static class BookmarkFolderViewHolder extends RecyclerView.ViewHolder {

final BookmarkItemFolderBinding binding;

BookmarkFolderViewHolder(@NonNull BookmarkItemFolderBinding binding) {
Expand Down
183 changes: 183 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,183 @@
package com.igalia.wolvic.ui.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.BookmarkItemInNewTabBinding;
import com.igalia.wolvic.ui.callbacks.BookmarkItemCallback;

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;

// Initial implementation: Show, add, delete bookmarks in New Tab page.
// TODO: Implement data structure and logics to handle pages in New Tab that are separate from bookmarks.
public class NewTabAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<Bookmark> bookmarkItems;

@Nullable
private final BookmarkItemCallback mBookmarkItemCallback;

public NewTabAdapter(@Nullable BookmarkItemCallback clickCallback) {
mBookmarkItemCallback = clickCallback;
}

public void setBookmarkListInNewTab(final List<BookmarkNode> bookmarkNodes) {
//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());
}

public void removeItem(Bookmark aBookmark) {
bookmarkItems.remove(aBookmark);
notifyDataSetChanged();
}

public void addItem(String title, String url) {
SessionStore.get().getBookmarkStore().addBookmark(url, title);
Bookmark item = new Bookmark(title, url);
bookmarkItems.add(item);
notifyDataSetChanged();
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
BookmarkItemInNewTabBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_item_in_new_tab,
parent, false);

binding.setCallback(mBookmarkItemCallback);
binding.setIsHovered(false);

return new BookmarkViewHolder(binding);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Bookmark item = bookmarkItems.get(position);

BookmarkViewHolder bookmarkHolder = (BookmarkViewHolder) holder;
BookmarkItemInNewTabBinding binding = bookmarkHolder.binding;
binding.setItem(item);

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.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.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();
}

static class BookmarkViewHolder extends RecyclerView.ViewHolder {

final BookmarkItemInNewTabBinding binding;

BookmarkViewHolder(@NonNull BookmarkItemInNewTabBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}

private View.OnHoverListener mIconHoverListener = (view, motionEvent) -> {
ImageView icon = (ImageView) view;
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
icon.setImageState(new int[]{android.R.attr.state_hovered}, true);
return false;

case MotionEvent.ACTION_HOVER_EXIT:
icon.setImageState(new int[]{android.R.attr.state_active}, true);
return false;
}

return false;
};
}
Loading

0 comments on commit 96768a2

Please sign in to comment.