Update library when changes to the DB are made and open manga detail activity

This commit is contained in:
inorichi 2015-09-26 18:58:01 +02:00
parent 8b6bdfdfdc
commit e567bc753f
7 changed files with 114 additions and 37 deletions

View File

@ -6,6 +6,7 @@ import javax.inject.Singleton;
import dagger.Component; import dagger.Component;
import eu.kanade.mangafeed.data.DataModule; import eu.kanade.mangafeed.data.DataModule;
import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.ui.activity.MainActivity; import eu.kanade.mangafeed.ui.activity.MainActivity;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.fragment.LibraryFragment; import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
@ -20,7 +21,7 @@ import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
public interface AppComponent { public interface AppComponent {
void inject(MainActivity mainActivity); void inject(MainActivity mainActivity);
void inject(LibraryFragment libraryFragment); void inject(LibraryPresenter libraryPresenter);
void inject(MangaDetailActivity mangaDetailActivity); void inject(MangaDetailActivity mangaDetailActivity);
Application application(); Application application();

View File

@ -0,0 +1,47 @@
package eu.kanade.mangafeed.presenter;
import android.content.Intent;
import android.widget.GridView;
import java.util.ArrayList;
import javax.inject.Inject;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.ui.activity.MangaDetailActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
import eu.kanade.mangafeed.view.LibraryView;
import static rx.android.schedulers.AndroidSchedulers.mainThread;
public class LibraryPresenter {
private LibraryView mLibraryView;
@Inject
public DatabaseHelper db;
public LibraryPresenter(LibraryView libraryView) {
mLibraryView = libraryView;
App.getComponent(libraryView.getActivity()).inject(this);
}
public void onMangaClick(LibraryAdapter adapter, int position) {
Intent intent = new Intent(mLibraryView.getActivity(), MangaDetailActivity.class);
EventBus.getDefault().postSticky(adapter.getItem(position));
mLibraryView.getActivity().startActivity(intent);
}
public void initializeMangas() {
db.manga.get()
.observeOn(mainThread())
.subscribe(
mangas -> {
mLibraryView.setMangas(new ArrayList<>(mangas));
}
);
}
}

View File

@ -11,6 +11,7 @@ import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import butterknife.Bind; import butterknife.Bind;
@ -28,9 +29,9 @@ public class LibraryAdapter extends ArrayAdapter<Manga> {
Context context; Context context;
int layoutResourceId; int layoutResourceId;
List<Manga> data; ArrayList<Manga> data;
public LibraryAdapter(Context context, int layoutResourceId, List<Manga> data) { public LibraryAdapter(Context context, int layoutResourceId, ArrayList<Manga> data) {
super(context, layoutResourceId, data); super(context, layoutResourceId, data);
this.context = context; this.context = context;
this.layoutResourceId = layoutResourceId; this.layoutResourceId = layoutResourceId;
@ -63,6 +64,12 @@ public class LibraryAdapter extends ArrayAdapter<Manga> {
return row; return row;
} }
public void setData(ArrayList<Manga> mangas) {
// Avoid calling dataSetChanged twice
data.clear();
addAll(mangas);
}
private String getImageUrl() { private String getImageUrl() {
return "http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg"; return "http://img1.wikia.nocookie.net/__cb20090524204255/starwars/images/thumb/1/1a/R2d2.jpg/400px-R2d2.jpg";
} }
@ -78,4 +85,6 @@ public class LibraryAdapter extends ArrayAdapter<Manga> {
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
} }
} }
} }

View File

@ -1,37 +1,29 @@
package eu.kanade.mangafeed.ui.fragment; package eu.kanade.mangafeed.ui.fragment;
import android.app.Fragment; import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.GridView; import android.widget.GridView;
import java.util.List; import java.util.ArrayList;
import javax.inject.Inject;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.helpers.DatabaseHelper;
import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.presenter.LibraryPresenter;
import eu.kanade.mangafeed.ui.activity.BaseActivity; import eu.kanade.mangafeed.ui.activity.BaseActivity;
import eu.kanade.mangafeed.ui.adapter.LibraryAdapter; import eu.kanade.mangafeed.ui.adapter.LibraryAdapter;
import rx.functions.Action1; import eu.kanade.mangafeed.view.LibraryView;
public class LibraryFragment extends Fragment {
@Bind(R.id.gridView) public class LibraryFragment extends Fragment implements LibraryView {
GridView grid;
@Inject @Bind(R.id.gridView) GridView grid;
DatabaseHelper db; LibraryPresenter mLibraryPresenter;
LibraryAdapter mAdapter;
List<Manga> mangas;
public static LibraryFragment newInstance() { public static LibraryFragment newInstance() {
LibraryFragment fragment = new LibraryFragment(); LibraryFragment fragment = new LibraryFragment();
@ -40,34 +32,45 @@ public class LibraryFragment extends Fragment {
return fragment; return fragment;
} }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLibraryPresenter = new LibraryPresenter(this);
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_library, container, false); View view = inflater.inflate(R.layout.fragment_library, container, false);
App.getComponent(getActivity()).inject(this);
((BaseActivity) getActivity()).getSupportActionBar().setTitle(R.string.library_title); ((BaseActivity) getActivity()).getSupportActionBar().setTitle(R.string.library_title);
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
db.manga.get().subscribe( mLibraryPresenter.initializeMangas();
result -> { setMangaClickListener();
mangas = result;
LibraryAdapter adapter = new LibraryAdapter(getActivity(),
R.layout.item_library, mangas);
grid.setAdapter(adapter);
grid.setOnItemClickListener(
(parent, v, position, id) -> {
Intent intent = new Intent(".ui.activity.MangaDetailActivity");
EventBus.getDefault().postSticky(adapter.getItem(position));
startActivity(intent);
}
);
}
);
return view; return view;
} }
public void setMangas(ArrayList<Manga> mangas) {
if (mAdapter == null) {
mAdapter = new LibraryAdapter(
getActivity(),
R.layout.item_library,
mangas);
grid.setAdapter(mAdapter);
} else {
mAdapter.setData(mangas);
}
}
private void setMangaClickListener() {
grid.setOnItemClickListener(
(parent, view, position, id) ->
mLibraryPresenter.onMangaClick(mAdapter, position)
);
}
} }

View File

@ -0,0 +1,7 @@
package eu.kanade.mangafeed.view;
import android.content.Context;
public interface BaseView {
Context getActivity();
}

View File

@ -0,0 +1,10 @@
package eu.kanade.mangafeed.view;
import java.util.ArrayList;
import eu.kanade.mangafeed.data.models.Manga;
public interface LibraryView extends BaseView {
void setMangas(ArrayList<Manga> mangas);
}

View File

@ -17,7 +17,7 @@
<LinearLayout <LinearLayout
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="42dp" android:layout_height="36dp"
android:id="@+id/footerLinearLayout" android:id="@+id/footerLinearLayout"
android:background="@color/md_blue_100"> android:background="@color/md_blue_100">