Incomplete changes

This commit is contained in:
inorichi 2015-12-25 17:50:31 +01:00
parent 2aa36ed702
commit e3bc13630d
11 changed files with 241 additions and 108 deletions

View File

@ -39,6 +39,7 @@ import eu.kanade.mangafeed.data.database.models.MangaSyncStorIOSQLitePutResolver
import eu.kanade.mangafeed.data.database.resolvers.LibraryMangaGetResolver; import eu.kanade.mangafeed.data.database.resolvers.LibraryMangaGetResolver;
import eu.kanade.mangafeed.data.database.tables.CategoryTable; import eu.kanade.mangafeed.data.database.tables.CategoryTable;
import eu.kanade.mangafeed.data.database.tables.ChapterTable; import eu.kanade.mangafeed.data.database.tables.ChapterTable;
import eu.kanade.mangafeed.data.database.tables.MangaCategoryTable;
import eu.kanade.mangafeed.data.database.tables.MangaSyncTable; import eu.kanade.mangafeed.data.database.tables.MangaSyncTable;
import eu.kanade.mangafeed.data.database.tables.MangaTable; import eu.kanade.mangafeed.data.database.tables.MangaTable;
import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService; import eu.kanade.mangafeed.data.mangasync.base.MangaSyncService;
@ -98,7 +99,7 @@ public class DatabaseHelper {
.listOfObjects(Manga.class) .listOfObjects(Manga.class)
.withQuery(RawQuery.builder() .withQuery(RawQuery.builder()
.query(LibraryMangaGetResolver.QUERY) .query(LibraryMangaGetResolver.QUERY)
.observesTables(MangaTable.TABLE, ChapterTable.TABLE, CategoryTable.TABLE) .observesTables(MangaTable.TABLE, ChapterTable.TABLE, MangaCategoryTable.TABLE)
.build()) .build())
.withGetResolver(LibraryMangaGetResolver.INSTANCE) .withGetResolver(LibraryMangaGetResolver.INSTANCE)
.prepare(); .prepare();

View File

@ -11,7 +11,7 @@ import eu.kanade.mangafeed.data.database.tables.CategoryTable;
public class Category implements Serializable { public class Category implements Serializable {
@StorIOSQLiteColumn(name = CategoryTable.COLUMN_ID, key = true) @StorIOSQLiteColumn(name = CategoryTable.COLUMN_ID, key = true)
public Long id; public Integer id;
@StorIOSQLiteColumn(name = CategoryTable.COLUMN_NAME) @StorIOSQLiteColumn(name = CategoryTable.COLUMN_NAME)
public String name; public String name;

View File

@ -58,7 +58,7 @@ public class Manga implements Serializable {
public int unread; public int unread;
public long category; public int category;
public Manga() {} public Manga() {}

View File

@ -15,7 +15,7 @@ public class MangaCategory {
public long manga_id; public long manga_id;
@StorIOSQLiteColumn(name = MangaCategoryTable.COLUMN_CATEGORY_ID) @StorIOSQLiteColumn(name = MangaCategoryTable.COLUMN_CATEGORY_ID)
public long category_id; public int category_id;
public MangaCategory() {} public MangaCategory() {}

View File

@ -53,7 +53,7 @@ public class LibraryMangaGetResolver extends MangaStorIOSQLiteGetResolver {
manga.unread = cursor.getInt(unreadColumn); manga.unread = cursor.getInt(unreadColumn);
int categoryColumn = cursor.getColumnIndex(MangaTable.COLUMN_CATEGORY); int categoryColumn = cursor.getColumnIndex(MangaTable.COLUMN_CATEGORY);
manga.category = cursor.getLong(categoryColumn); manga.category = cursor.getInt(categoryColumn);
return manga; return manga;
} }

View File

@ -0,0 +1,69 @@
package eu.kanade.mangafeed.ui.library;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import java.util.List;
import java.util.Map;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.ui.reader.viewer.common.SmartFragmentStatePagerAdapter;
class LibraryCategoryAdapter extends SmartFragmentStatePagerAdapter {
private LibraryFragment fragment;
private List<Category> categories;
private Map<Integer, List<Manga>> mangas;
public LibraryCategoryAdapter(LibraryFragment fragment, FragmentManager fm) {
super(fm);
this.fragment = fragment;
}
@Override
public Fragment getItem(int position) {
Category category = categories.get(position);
return LibraryCategoryFragment.newInstance(fragment, category,
mangas != null ? mangas.get(category.id) : null);
}
@Override
public int getCount() {
return categories == null ? 0 : categories.size();
}
@Override
public CharSequence getPageTitle(int position) {
return categories.get(position).name;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
notifyDataSetChanged();
}
public void setMangasOnCategories(Map<Integer, List<Manga>> mangas) {
this.mangas = mangas;
for (Map.Entry<Integer, List<Manga>> entry : mangas.entrySet()) {
LibraryCategoryFragment fragment = getFragment(entry.getKey());
if (fragment != null) {
fragment.setMangas(entry.getValue());
}
}
}
@Nullable
public LibraryCategoryFragment getFragment(int categoryId) {
if (categories != null) {
for (int i = 0; i < categories.size(); i++) {
if (categories.get(i).id == categoryId) {
return (LibraryCategoryFragment) getRegisteredFragment(i);
}
}
}
return null;
}
}

View File

@ -0,0 +1,75 @@
package eu.kanade.mangafeed.ui.library;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.ui.base.fragment.BaseFragment;
import eu.kanade.mangafeed.ui.manga.MangaActivity;
public class LibraryCategoryFragment extends BaseFragment {
@Bind(R.id.gridView) GridView grid;
private LibraryFragment parent;
private LibraryAdapter adapter;
private Category category;
private List<Manga> mangas;
public static LibraryCategoryFragment newInstance(LibraryFragment parent, Category category,
List<Manga> mangas) {
LibraryCategoryFragment fragment = new LibraryCategoryFragment();
fragment.initialize(parent, category, mangas);
return fragment;
}
private void initialize(LibraryFragment parent, Category category, List<Manga> mangas) {
this.parent = parent;
this.category = category;
this.mangas = mangas;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_library_category, container, false);
ButterKnife.bind(this, view);
adapter = new LibraryAdapter(parent);
grid.setAdapter(adapter);
if (mangas != null) {
setMangas(mangas);
}
return view;
}
@OnItemClick(R.id.gridView)
protected void onMangaClick(int position) {
Intent intent = MangaActivity.newIntent(
getActivity(),
adapter.getItem(position)
);
parent.getPresenter().onOpenManga();
getActivity().startActivity(intent);
}
public void setMangas(List<Manga> mangas) {
adapter.setNewItems(mangas);
}
}

View File

@ -2,35 +2,34 @@ package eu.kanade.mangafeed.ui.library;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.SearchView; import android.support.design.widget.TabLayout;
import android.util.SparseBooleanArray; import android.support.v4.view.ViewPager;
import android.view.ActionMode;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnItemClick;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.sync.LibraryUpdateService; import eu.kanade.mangafeed.data.sync.LibraryUpdateService;
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
import eu.kanade.mangafeed.ui.manga.MangaActivity;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import rx.Observable;
@RequiresPresenter(LibraryPresenter.class) @RequiresPresenter(LibraryPresenter.class)
public class LibraryFragment extends BaseRxFragment<LibraryPresenter> { public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
@Bind(R.id.gridView) GridView grid; @Bind(R.id.tabs) TabLayout tabs;
private LibraryAdapter adapter; @Bind(R.id.view_pager) ViewPager categoriesPager;
private LibraryCategoryAdapter adapter;
public static LibraryFragment newInstance() { public static LibraryFragment newInstance() {
return new LibraryFragment(); return new LibraryFragment();
@ -50,8 +49,8 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
setToolbarTitle(getString(R.string.label_library)); setToolbarTitle(getString(R.string.label_library));
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
createAdapter(); adapter = new LibraryCategoryAdapter(this, getChildFragmentManager());
setMangaLongClickListener(); categoriesPager.setAdapter(adapter);
return view; return view;
} }
@ -59,7 +58,6 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.library, menu); inflater.inflate(R.menu.library, menu);
initializeSearch(menu);
} }
@Override @Override
@ -77,83 +75,21 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter> {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private void initializeSearch(Menu menu) { public void onNextMangas(Map<Integer, List<Manga>> mangas) {
final SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView(); adapter.setMangasOnCategories(mangas);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
} }
private void createAdapter() { public void onNextCategories(List<Category> categories) {
adapter = new LibraryAdapter(this); List<Category> actualCategories = new ArrayList<>();
grid.setAdapter(adapter);
}
public void onNextMangas(List<Manga> mangas) { // TODO should we always add this?
adapter.setNewItems(mangas); Category defaultCat = Category.create("Default");
} defaultCat.id = 0;
actualCategories.add(defaultCat);
@OnItemClick(R.id.gridView) actualCategories.addAll(categories);
protected void onMangaClick(int position) { adapter.setCategories(actualCategories);
Intent intent = MangaActivity.newIntent( tabs.setupWithViewPager(categoriesPager);
getActivity(),
adapter.getItem(position)
);
getActivity().startActivity(intent);
}
private void setMangaLongClickListener() {
grid.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
mode.setTitle(getResources().getString(R.string.library_selection_title)
+ ": " + grid.getCheckedItemCount());
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.library_selection, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
getPresenter().deleteMangas(getSelectedMangas());
mode.finish();
return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
private Observable<Manga> getSelectedMangas() {
SparseBooleanArray checkedItems = grid.getCheckedItemPositions();
return Observable.range(0, checkedItems.size())
.map(checkedItems::keyAt)
.map(adapter::getItem);
} }
} }

View File

@ -10,6 +10,7 @@ import javax.inject.Inject;
import eu.kanade.mangafeed.data.cache.CoverCache; import eu.kanade.mangafeed.data.cache.CoverCache;
import eu.kanade.mangafeed.data.database.DatabaseHelper; import eu.kanade.mangafeed.data.database.DatabaseHelper;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.preference.PreferencesHelper; import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.data.source.SourceManager; import eu.kanade.mangafeed.data.source.SourceManager;
@ -25,18 +26,31 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@Inject CoverCache coverCache; @Inject CoverCache coverCache;
@Inject SourceManager sourceManager; @Inject SourceManager sourceManager;
private static final int GET_MANGAS = 1; private static final int GET_CATEGORIES = 1;
private static final int GET_MANGAS = 2;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
super.onCreate(savedState); super.onCreate(savedState);
restartableLatestCache(GET_CATEGORIES,
() -> db.getCategories().createObservable(),
LibraryFragment::onNextCategories);
restartableLatestCache(GET_MANGAS, restartableLatestCache(GET_MANGAS,
() -> db.getLibraryMangas().createObservable() this::getLibraryMangasObservable,
.observeOn(AndroidSchedulers.mainThread()),
LibraryFragment::onNextMangas); LibraryFragment::onNextMangas);
start(GET_MANGAS); start(GET_CATEGORIES);
}
@Override
protected void onTakeView(LibraryFragment view) {
super.onTakeView(view);
if (!isSubscribed(GET_MANGAS)) {
start(GET_MANGAS);
}
} }
public void deleteMangas(Observable<Manga> selectedMangas) { public void deleteMangas(Observable<Manga> selectedMangas) {
@ -48,12 +62,17 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
.subscribe()); .subscribe());
} }
public Observable<Map<Long, List<Manga>>> getLibraryMangasObservable() { public Observable<Map<Integer, List<Manga>>> getLibraryMangasObservable() {
return db.getLibraryMangas().createObservable() return db.getLibraryMangas().createObservable()
.flatMap(mangas -> Observable.from(mangas) .flatMap(mangas -> Observable.from(mangas)
.groupBy(manga -> manga.category) .groupBy(manga -> manga.category)
.flatMap(group -> group.toList().map(list -> new Pair<>(group.getKey(), list))) .flatMap(group -> group.toList()
.toMap(pair -> pair.first, pair -> pair.second)); .map(list -> Pair.create(group.getKey(), list)))
.toMap(pair -> pair.first, pair -> pair.second))
.observeOn(AndroidSchedulers.mainThread());
} }
public void onOpenManga() {
stop(GET_MANGAS);
}
} }

View File

@ -1,13 +1,32 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"> android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView <android.support.design.widget.AppBarLayout
android:id="@+id/gridView" android:id="@+id/appbar"
style="@style/AppTheme.GridView" android:layout_width="match_parent"
android:choiceMode="multipleChoiceModal" android:layout_height="wrap_content"
android:columnWidth="140dp" android:theme="@style/ThemeOverlay.AppTheme.Dark">
tools:listitem="@layout/item_catalogue" />
</FrameLayout> <android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppTheme.Dark"
app:tabGravity="fill"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager">
</android.support.v4.view.ViewPager>
</LinearLayout>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridView"
style="@style/AppTheme.GridView"
android:choiceMode="multipleChoiceModal"
android:columnWidth="140dp"
tools:listitem="@layout/item_catalogue" />
</FrameLayout>