From d3b6b8721edf64f84a09e7e8c7b8513a85363287 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 28 Sep 2015 12:42:16 +0200 Subject: [PATCH] Set toolbar in BaseActivity. Show title in MangaDetailActivity --- .../mangafeed/data/managers/MangaManager.java | 14 ++++++++++ .../mangafeed/presenter/LibraryPresenter.java | 17 +++++------ .../mangafeed/ui/activity/BaseActivity.java | 10 +++++++ .../mangafeed/ui/activity/MainActivity.java | 12 ++++---- .../ui/activity/MangaDetailActivity.java | 28 +++++++++++++++++-- .../ui/fragment/LibraryFragment.java | 6 ++-- .../eu/kanade/mangafeed/view/LibraryView.java | 3 +- app/src/main/res/layout/activity_main.xml | 10 ++----- .../main/res/layout/activity_manga_detail.xml | 8 +++--- app/src/main/res/layout/toolbar.xml | 11 ++++++++ app/src/main/res/values/styles.xml | 2 +- 11 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 app/src/main/res/layout/toolbar.xml diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java index 6a6519b0a5..4e5db42b5e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java @@ -28,10 +28,24 @@ public class MangaManager extends BaseManager { } public Observable insert(Manga manga) { + return db.put() .object(manga) .prepare() .createObservable(); } + public void createDummyManga() { + Manga m = new Manga(); + m.url="http://example.com"; + m.artist="Eiichiro Oda"; + m.author="Eiichiro Oda"; + m.description="..."; + m.genre="Action, Drama"; + m.status="Ongoing"; + m.thumbnail_url="http://example.com/pic.png"; + m.title="Berserk"; + insert(m).subscribe(); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java index 7f4e0367ff..ca39755897 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -2,13 +2,12 @@ package eu.kanade.mangafeed.presenter; import android.content.Intent; -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.data.models.Manga; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.view.LibraryView; import uk.co.ribot.easyadapter.EasyAdapter; @@ -27,20 +26,18 @@ public class LibraryPresenter { App.getComponent(libraryView.getActivity()).inject(this); } - public void onMangaClick(EasyAdapter adapter, int position) { - Intent intent = new Intent(mLibraryView.getActivity(), MangaDetailActivity.class); - EventBus.getDefault().postSticky(adapter.getItem(position)); + public void onMangaClick(EasyAdapter adapter, int position) { + Intent intent = MangaDetailActivity.newIntent( + mLibraryView.getActivity(), + adapter.getItem(position) + ); mLibraryView.getActivity().startActivity(intent); } public void initializeMangas() { db.manga.get() .observeOn(mainThread()) - .subscribe( - mangas -> { - mLibraryView.setMangas(new ArrayList<>(mangas)); - } - ); + .subscribe(mLibraryView::setMangas); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java index 6f96cdea91..1b014121b0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/BaseActivity.java @@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.activity; import android.app.FragmentManager; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; import android.view.MenuItem; import eu.kanade.mangafeed.App; @@ -25,6 +26,15 @@ public class BaseActivity extends AppCompatActivity { } } + protected void setupToolbar(Toolbar toolbar) { + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + } + + public void setToolbarTitle(String title) { + getSupportActionBar().setTitle(title); + } + protected AppComponent applicationComponent() { return App.get(this).getComponent(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java index 36ca43a989..66f972f70d 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MainActivity.java @@ -29,19 +29,17 @@ public class MainActivity extends BaseActivity { @Bind(R.id.drawer_container) FrameLayout container; - @Inject DatabaseHelper mDb; private Drawer drawer; private CompositeSubscription mSubscriptions; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - applicationComponent().inject(this); setContentView(R.layout.activity_main); ButterKnife.bind(this); mSubscriptions = new CompositeSubscription(); - setupToolbar(); + setupToolbar(toolbar); drawer = new DrawerBuilder() .withActivity(this) @@ -107,10 +105,6 @@ public class MainActivity extends BaseActivity { } } - private void setupToolbar() { - setSupportActionBar(toolbar); - } - private void setFragment(Fragment fragment) { try { if (fragment != null && getSupportFragmentManager() != null) { @@ -125,4 +119,8 @@ public class MainActivity extends BaseActivity { } } + public void setToolbarTitle(int titleResource) { + getSupportActionBar().setTitle(getString(titleResource)); + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java index 183e18cbf5..a1942556a2 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/MangaDetailActivity.java @@ -1,22 +1,33 @@ package eu.kanade.mangafeed.ui.activity; -import android.support.v7.app.AppCompatActivity; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; +import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; +import butterknife.Bind; +import butterknife.ButterKnife; import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Manga; -public class MangaDetailActivity extends AppCompatActivity { +public class MangaDetailActivity extends BaseActivity { Manga manga; + @Bind(R.id.toolbar) + Toolbar toolbar; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_manga_detail); + ButterKnife.bind(this); + + setupToolbar(toolbar); + EventBus.getDefault().registerSticky(this); } @@ -49,8 +60,19 @@ public class MangaDetailActivity extends AppCompatActivity { super.onDestroy(); } - public void onEvent(Manga manga) { + public static Intent newIntent(Context context, Manga manga) { + Intent intent = new Intent(context, MangaDetailActivity.class); + EventBus.getDefault().postSticky(manga); + return intent; + } + + public void onEventMainThread(Manga manga) { this.manga = manga; + loadManga(); //loadChapters(); } + + private void loadManga() { + setToolbarTitle(manga.title); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java index 0a0777b80f..60a5d7b4f4 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/LibraryFragment.java @@ -8,6 +8,7 @@ import android.view.ViewGroup; import android.widget.GridView; import java.util.ArrayList; +import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; @@ -15,6 +16,7 @@ import eu.kanade.mangafeed.R; 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.MainActivity; import eu.kanade.mangafeed.ui.adapter.MangaLibraryHolder; import eu.kanade.mangafeed.view.LibraryView; import uk.co.ribot.easyadapter.EasyAdapter; @@ -45,7 +47,7 @@ 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); - ((BaseActivity) getActivity()).getSupportActionBar().setTitle(R.string.library_title); + ((MainActivity)getActivity()).setToolbarTitle(getString(R.string.library_title)); ButterKnife.bind(this, view); mLibraryPresenter.initializeMangas(); @@ -54,7 +56,7 @@ public class LibraryFragment extends Fragment implements LibraryView { return view; } - public void setMangas(ArrayList mangas) { + public void setMangas(List mangas) { if (mEasyAdapter == null) { mEasyAdapter = new EasyAdapter( getActivity(), diff --git a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java index ef83cecfa5..d6a6b50301 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/LibraryView.java @@ -1,10 +1,11 @@ package eu.kanade.mangafeed.view; import java.util.ArrayList; +import java.util.List; import eu.kanade.mangafeed.data.models.Manga; public interface LibraryView extends BaseView { - void setMangas(ArrayList mangas); + void setMangas(List mangas); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5c2fccf464..f0c9060325 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,17 +1,11 @@ - + layout="@layout/toolbar"/> + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 9f848adfab..54d2e38dd6 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,6 +1,6 @@ -