Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
spacifici committed Jul 4, 2019
1 parent 8f7ef19 commit 93db29f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 100 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ android {
universalApk false
}
}
testOptions {
animationsDisabled = true
}
}

dexcount {
Expand Down
114 changes: 24 additions & 90 deletions app/src/cliqz/java/com/cliqz/browser/main/HistoryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

import android.database.Cursor;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cliqz.browser.R;
import com.cliqz.browser.app.BrowserApp;
import com.cliqz.browser.overview.OverviewFragment;
import com.cliqz.browser.overview.OverviewTabsEnum;
import com.cliqz.browser.webview.CliqzMessages;
import com.cliqz.browser.starttab.HistoryAdapter;
import com.cliqz.browser.starttab.HistoryItemTouchHelper;
import com.cliqz.nove.Subscribe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -32,7 +35,6 @@ public class HistoryFragment extends FragmentWithBus {

private boolean isMultiSelect;
private HistoryAdapter adapter;
private final ArrayList<HistoryModel> historyList = new ArrayList<>();
private View contextualToolBar;
private TextView contextualToolBarTitle;

Expand All @@ -48,6 +50,9 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
contextualToolBarTitle = contextualToolBar.findViewById(R.id.contextual_title);
final View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this,view);
final FlavoredActivityComponent component = BrowserApp.getActivityComponent(getContext());
Objects.requireNonNull(component).inject(this);
adapter = new HistoryAdapter(engine, handler, bus);
return view;
}

Expand All @@ -62,45 +67,13 @@ public void onStart() {
preferenceManager.setShouldClearQueries(PreferenceManager.ClearQueriesOptions.NO);
}*/
prepareListData();
if (historyList.size() == 0) {
if (adapter.getItemCount() == 0) {
noHistoryMessage.setVisibility(View.VISIBLE);
return;
}
noHistoryMessage.setVisibility(View.GONE);
if (adapter == null) {
adapter = new HistoryAdapter(historyList, engine, handler, new HistoryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
//ignore click on date
if (adapter.getItemViewType(position) == HistoryAdapter.VIEW_TYPE_DATE) {
return;
}
if (isMultiSelect) {
multiSelect(position);
} else if (adapter.getItemViewType(position) == HistoryAdapter.VIEW_TYPE_HISTORY) {
bus.post(CliqzMessages.OpenLink.openFromHistory(historyList.get(position).getUrl()));
} else if (adapter.getItemViewType(position) == HistoryAdapter.VIEW_TYPE_QUERY) {
bus.post(new Messages.OpenQuery(historyList.get(position).getUrl()));
}
}

@Override
public void onLongPress(View view, int position) {
//if view is being swiped ignore long press
if (view.getTranslationX() != 0) {
return;
}
//ignore long press on date
if (adapter.getItemViewType(position) == HistoryAdapter.VIEW_TYPE_DATE) {
return;
}
if (!isMultiSelect) {
adapter.multiSelectList.clear();
showContextualMenu();
}
multiSelect(position);
}
});
adapter = new HistoryAdapter(engine, handler, bus);
}
prepareRecyclerView();
}
Expand Down Expand Up @@ -130,7 +103,7 @@ public void onContextualBarCanceled(Messages.OnContextualBarCancelPressed event)
public void onContextualBarDelete(Messages.OnContextualBarDeletePressed event) {
if (((OverviewFragment)getParentFragment()).getCurrentPageIndex()
== OverviewTabsEnum.HISTORY.getFragmentIndex()) {
deleteSelectedItems();
// deleteSelectedItems();
}
}

Expand All @@ -142,59 +115,17 @@ private void prepareRecyclerView() {
historyListView.setLayoutManager(new LinearLayoutManager(getContext()));

//callback to handle swipe and delete
final ItemTouchHelper.SimpleCallback simpleItemTouchCallback =
new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//Dont swipe date view and when contextual menu is enabled
if (viewHolder instanceof HistoryAdapter.DateViewHolder || isMultiSelect) {
return 0;
}
return super.getSwipeDirs(recyclerView, viewHolder);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
final int position = viewHolder.getAdapterPosition();
final int type = viewHolder.getItemViewType();
if (type == HistoryAdapter.VIEW_TYPE_HISTORY) {
historyDatabase.deleteHistoryPoint(historyList.get(position).getId());
} else {
historyDatabase.deleteQuery(historyList.get(position).getId());
}
historyList.remove(position);
adapter.notifyItemRemoved(position);
//check if date view is to be removed
if ((position == historyList.size()
|| historyListView.getAdapter().getItemViewType(position) == HistoryAdapter.VIEW_TYPE_DATE)
&& historyListView.getAdapter().getItemViewType(position - 1 ) == HistoryAdapter.VIEW_TYPE_DATE) {
historyList.remove(position-1);
adapter.notifyItemRemoved(position-1);
if (historyList.size() == 0) {
noHistoryMessage.setVisibility(View.VISIBLE);
}
}
}
};

//callbacks for click and long click on items
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new HistoryItemTouchHelper(historyDatabase, adapter));
itemTouchHelper.attachToRecyclerView(historyListView);
historyListView.setAdapter(adapter);
historyListView.scrollToPosition(historyList.size()-1);
historyListView.scrollToPosition(adapter.getItemCount()-1);
}

private void prepareListData() {
//TODO historyDatabase.getHistoryItemsCount has to be modified to get the limit correctly;
historyList.clear();
final Cursor cursor = historyDatabase.getHistoryItemsForRecyclerView(0, historyDatabase.getHistoryItemsCount());
final int itemsCount = historyDatabase.getHistoryItemsCount();
final ArrayList<HistoryModel> historyList = new ArrayList<>(itemsCount);
final Cursor cursor = historyDatabase.getHistoryItemsForRecyclerView(0, itemsCount);
final int typeIndex = cursor.getColumnIndex("type");
final int idIndex = cursor.getColumnIndex("id");
final int urlIndex = cursor.getColumnIndex("url");
Expand All @@ -208,8 +139,10 @@ private void prepareListData() {
cursor.getInt(typeIndex)));
}
cursor.close();
adapter.setHistory(historyList);
}

/*
private void multiSelect(int position) {
if (adapter.multiSelectList.contains(position)) {
adapter.multiSelectList.remove(Integer.valueOf(position));
Expand Down Expand Up @@ -250,6 +183,7 @@ private void deleteSelectedItems() {
historyListView.scrollToPosition(historyList.size() - 1);
hideContextualMenu();
}
*/

private void showContextualMenu() {
//if its already visible dont execute the next lines
Expand All @@ -268,7 +202,7 @@ private void hideContextualMenu() {
return;
}
isMultiSelect = false;
adapter.multiSelectList.clear();
// adapter.multiSelectList.clear();
adapter.notifyDataSetChanged();
getParentFragment().setHasOptionsMenu(true);
setDisplayHomeAsUpEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public StartTabContainer(@NonNull Context context, @Nullable AttributeSet attrs)

public StartTabContainer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public void init(FragmentManager supportFragmentManager, PreferenceManager preferenceManager) {
mFreshtab = new Freshtab(getContext());
mFreshtab = new Freshtab(context);
addView(mFreshtab);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
*/
public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

static final int VIEW_TYPE_DATE = 0;
static final int VIEW_TYPE_HISTORY = 1;
static final int VIEW_TYPE_QUERY = 2;
public static final int VIEW_TYPE_DATE = 0;
public static final int VIEW_TYPE_HISTORY = 1;
public static final int VIEW_TYPE_QUERY = 2;
private final Bus bus;
private final Engine engine;
private final Handler handler;
Expand All @@ -49,7 +49,7 @@ public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder

// private ArrayList<Integer> multiSelectList = new ArrayList<>();

HistoryAdapter(Engine engine, Handler handler,
public HistoryAdapter(Engine engine, Handler handler,
Bus bus) {
this.historyList = new ArrayList<>();
this.engine = engine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import acr.browser.lightning.database.HistoryDatabase;

class HistoryItemTouchHelper extends ItemTouchHelper.SimpleCallback {
public class HistoryItemTouchHelper extends ItemTouchHelper.SimpleCallback {

private final HistoryDatabase db;
private final HistoryAdapter adapter;

HistoryItemTouchHelper(HistoryDatabase db, HistoryAdapter adapter) {
public HistoryItemTouchHelper(HistoryDatabase db, HistoryAdapter adapter) {
super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.db = db;
this.adapter = adapter;
Expand Down

0 comments on commit 93db29f

Please sign in to comment.