Search for library (not working)

This commit is contained in:
inorichi 2015-09-30 19:03:11 +02:00
parent 790ccdaba6
commit 289e53a7f5
20 changed files with 126 additions and 70 deletions

View file

@ -0,0 +1,18 @@
package eu.kanade.mangafeed.presenter;
import de.greenrobot.event.EventBus;
public class BasePresenter {
public void registerForStickyEvents() {
EventBus.getDefault().registerSticky(this);
}
public void registerForEvents() {
EventBus.getDefault().register(this);
}
public void unregisterForEvents() {
EventBus.getDefault().unregister(this);
}
}

View file

@ -10,11 +10,17 @@ import eu.kanade.mangafeed.data.helpers.PreferencesHelper;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.view.LibraryView;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import timber.log.Timber;
import uk.co.ribot.easyadapter.EasyAdapter;
import static rx.android.schedulers.AndroidSchedulers.mainThread;
public class LibraryPresenter {
public class LibraryPresenter extends BasePresenter {
private LibraryView view;
@ -24,6 +30,9 @@ public class LibraryPresenter {
@Inject
PreferencesHelper prefs;
private Subscription searchViewSubscription;
private PublishSubject<Observable<String>> searchViewPublishSubject;
public LibraryPresenter(LibraryView view) {
this.view = view;
App.getComponent(view.getActivity()).inject(this);
@ -45,10 +54,24 @@ public class LibraryPresenter {
view.getActivity().startActivity(intent);
}
public void initializeSearch() {
searchViewPublishSubject = PublishSubject.create();
searchViewSubscription = Observable.switchOnNext(searchViewPublishSubject)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Timber::w);
}
public void initializeMangas() {
db.manga.get()
.observeOn(mainThread())
.subscribe(view::setMangas);
}
public void onQueryTextChange(String query) {
if (searchViewPublishSubject != null) {
searchViewPublishSubject.onNext(Observable.just(query));
}
}
}

View file

@ -8,7 +8,7 @@ import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.view.MangaDetailView;
public class MangaDetailPresenter {
public class MangaDetailPresenter extends BasePresenter {
private MangaDetailView view;
@ -20,14 +20,6 @@ public class MangaDetailPresenter {
App.getComponent(view.getActivity()).inject(this);
}
public void onStart() {
EventBus.getDefault().registerSticky(this);
}
public void onStop() {
EventBus.getDefault().unregister(this);
}
public void onEventMainThread(Manga manga) {
view.loadManga(manga);
initializeChapters(manga);

View file

@ -89,22 +89,6 @@ public class MainActivity extends BaseActivity {
mSubscriptions.unsubscribe();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_github:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setFragment(Fragment fragment) {
try {
if (fragment != null && getSupportFragmentManager() != null) {

View file

@ -32,6 +32,12 @@ public class MangaDetailActivity extends BaseActivity implements MangaDetailView
@Bind(R.id.manga_chapters_list)
ListView list_chapters;
public static Intent newIntent(Context context, Manga manga) {
Intent intent = new Intent(context, MangaDetailActivity.class);
MangaDetailPresenter.newIntent(manga);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -42,44 +48,16 @@ public class MangaDetailActivity extends BaseActivity implements MangaDetailView
setupToolbar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_manga_detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static Intent newIntent(Context context, Manga manga) {
Intent intent = new Intent(context, MangaDetailActivity.class);
MangaDetailPresenter.newIntent(manga);
return intent;
}
@Override
public void onStart() {
super.onStart();
presenter.onStart();
presenter.registerForStickyEvents();
}
@Override
public void onStop() {
presenter.unregisterForEvents();
super.onStop();
presenter.onStop();
}
public void loadManga(Manga manga) {

View file

@ -0,0 +1,6 @@
package eu.kanade.mangafeed.ui.fragment;
import android.app.Fragment;
public class BaseFragment extends Fragment {
}

View file

@ -1,8 +1,10 @@
package eu.kanade.mangafeed.ui.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
@ -17,14 +19,16 @@ import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.ui.activity.MainActivity;
import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder;
import eu.kanade.mangafeed.view.LibraryView;
import timber.log.Timber;
import uk.co.ribot.easyadapter.EasyAdapter;
public class LibraryFragment extends Fragment implements LibraryView {
public class LibraryFragment extends BaseFragment implements LibraryView {
@Bind(R.id.gridView) GridView grid;
LibraryPresenter presenter;
EasyAdapter<Manga> adapter;
MainActivity activity;
public static LibraryFragment newInstance() {
LibraryFragment fragment = new LibraryFragment();
@ -36,8 +40,10 @@ public class LibraryFragment extends Fragment implements LibraryView {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
presenter = new LibraryPresenter(this);
activity = (MainActivity)getActivity();
}
@Override
@ -45,18 +51,49 @@ public class LibraryFragment extends Fragment implements LibraryView {
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_library, container, false);
((MainActivity)getActivity()).setToolbarTitle(getString(R.string.library_title));
activity.setToolbarTitle(getString(R.string.library_title));
ButterKnife.bind(this, view);
presenter.initializeMangas();
setMangaClickListener();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupToolbar();
setMangaClickListener();
presenter.initializeMangas();
presenter.initializeSearch();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.library, menu);
initializeSearch(menu);
}
private void initializeSearch(Menu menu) {
final SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
presenter.onQueryTextChange(newText);
return true;
}
});
}
// LibraryView
public void setMangas(List<Manga> mangas) {
if (adapter == null) {
adapter = new EasyAdapter<Manga>(
adapter = new EasyAdapter<>(
getActivity(),
MangaLibraryHolder.class,
mangas
@ -71,8 +108,12 @@ public class LibraryFragment extends Fragment implements LibraryView {
private void setMangaClickListener() {
grid.setOnItemClickListener(
(parent, view, position, id) ->
presenter.onMangaClick(adapter, position)
presenter.onMangaClick(adapter, position)
);
}
private void setupToolbar() {
//activity.getSupportActionBar().
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 422 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 448 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 787 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 744 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 967 B

View file

@ -0,0 +1,17 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
android:orderInCategory="100"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="@+id/action_refresh"
android:title="@string/action_refresh"
android:icon="@drawable/ic_action_refresh"
android:orderInCategory="1"
app:showAsAction="ifRoom" />
</menu>

View file

@ -1,6 +0,0 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_github" android:title="@string/action_github"
android:orderInCategory="100" app:showAsAction="never" />
</menu>

View file

@ -30,5 +30,8 @@
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="action_search">Search</string>
<string name="action_refresh">Refresh</string>
<string name="library_search_hint">Title or author...</string>
</resources>