From 6f409c0e3b2f31b9e35442a7e8d0f251bc20f663 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 25 Jan 2016 19:57:13 +0100 Subject: [PATCH] Add an alternative way to display the chapter title (#54) --- .../tachiyomi/data/database/models/Manga.java | 29 ++++++++---- .../ui/manga/chapter/ChaptersAdapter.java | 4 +- .../ui/manga/chapter/ChaptersFragment.java | 45 +++++++++++++++++++ .../ui/manga/chapter/ChaptersHolder.java | 34 +++++++++++--- .../ui/manga/chapter/ChaptersPresenter.java | 5 +++ .../tachiyomi/ui/reader/ReaderMenu.java | 2 +- app/src/main/res/menu/chapters.xml | 9 ++++ app/src/main/res/values/strings.xml | 3 ++ 8 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 app/src/main/res/menu/chapters.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java index 1128f0b1ee..a8acba4994 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.java @@ -68,9 +68,13 @@ public class Manga implements Serializable { public static final int COMPLETED = 2; public static final int LICENSED = 3; - public static final int SORT_AZ = 0; - public static final int SORT_ZA = 1; - public static final int SORT_MASK = 1; + public static final int SORT_AZ = 0x00000000; + public static final int SORT_ZA = 0x00000001; + public static final int SORT_MASK = 0x00000001; + + public static final int DISPLAY_NAME = 0x00000000; + public static final int DISPLAY_NUMBER = 0x00100000; + public static final int DISPLAY_MASK = 0x00100000; public Manga() {} @@ -124,16 +128,25 @@ public class Manga implements Serializable { } } - public void setFlags(int flag, int mask) { + public void setChapterOrder(int order) { + setFlags(order, SORT_MASK); + } + + public void setDisplayMode(int mode) { + setFlags(mode, DISPLAY_MASK); + } + + private void setFlags(int flag, int mask) { chapter_flags = (chapter_flags & ~mask) | (flag & mask); } - public boolean sortChaptersAZ () { - return (this.chapter_flags & SORT_MASK) == SORT_AZ; + public boolean sortChaptersAZ() { + return (chapter_flags & SORT_MASK) == SORT_AZ; } - public void setChapterOrder(int order) { - setFlags(order, SORT_MASK); + // Used to display the chapter's title one way or another + public int getDisplayMode() { + return chapter_flags & DISPLAY_MASK; } @Override diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.java index 0e0e64392a..ba7db26bcb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.java @@ -10,6 +10,7 @@ import java.util.List; import eu.davidea.flexibleadapter.FlexibleAdapter; import eu.kanade.tachiyomi.R; import eu.kanade.tachiyomi.data.database.models.Chapter; +import eu.kanade.tachiyomi.data.database.models.Manga; public class ChaptersAdapter extends FlexibleAdapter { @@ -33,7 +34,8 @@ public class ChaptersAdapter extends FlexibleAdapter { @Override public void onBindViewHolder(ChaptersHolder holder, int position) { final Chapter chapter = getItem(position); - holder.onSetValues(fragment.getActivity(), chapter); + final Manga manga = fragment.getPresenter().getManga(); + holder.onSetValues(chapter, manga); //When user scrolls this bind the correct selection status holder.itemView.setActivated(isSelected(position)); diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java index 6fb96fa16d..682aa34a50 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.java @@ -10,6 +10,7 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -62,6 +63,12 @@ public class ChaptersFragment extends BaseRxFragment implemen return new ChaptersFragment(); } + @Override + public void onCreate(Bundle bundle) { + super.onCreate(bundle); + setHasOptionsMenu(true); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -92,6 +99,21 @@ public class ChaptersFragment extends BaseRxFragment implemen return view; } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.chapters, menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_display_mode: + showDisplayModeDialog(); + return true; + } + return false; + } + public void onNextManga(Manga manga) { // Remove listeners before setting the values readCb.setOnCheckedChangeListener(null); @@ -157,6 +179,29 @@ public class ChaptersFragment extends BaseRxFragment implemen startActivity(intent); } + private void showDisplayModeDialog() { + final Manga manga = getPresenter().getManga(); + if (manga == null) + return; + + // Get available modes, ids and the selected mode + String[] modes = {getString(R.string.show_title), getString(R.string.show_chapter_number)}; + int[] ids = {Manga.DISPLAY_NAME, Manga.DISPLAY_NUMBER}; + int selectedIndex = manga.getDisplayMode() == Manga.DISPLAY_NAME ? 0 : 1; + + new MaterialDialog.Builder(getActivity()) + .items(modes) + .itemsIds(ids) + .itemsCallbackSingleChoice(selectedIndex, (dialog, itemView, which, text) -> { + // Save the new display mode + getPresenter().setDisplayMode(itemView.getId()); + // Refresh ui + adapter.notifyDataSetChanged(); + return true; + }) + .show(); + } + private void observeChapterDownloadProgress() { downloadProgressSubscription = getPresenter().getDownloadProgressObs() .subscribe(this::onDownloadProgressChange, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java index 373ace8dec..a8c76fa82f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersHolder.java @@ -7,6 +7,8 @@ import android.widget.PopupMenu; import android.widget.RelativeLayout; import android.widget.TextView; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.text.SimpleDateFormat; import java.util.Date; @@ -14,40 +16,60 @@ import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.tachiyomi.R; import eu.kanade.tachiyomi.data.database.models.Chapter; +import eu.kanade.tachiyomi.data.database.models.Manga; import eu.kanade.tachiyomi.data.download.model.Download; import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder; import rx.Observable; public class ChaptersHolder extends FlexibleViewHolder { - private final ChaptersAdapter adapter; - private Chapter item; - @Bind(R.id.chapter_title) TextView title; @Bind(R.id.download_text) TextView downloadText; @Bind(R.id.chapter_menu) RelativeLayout chapterMenu; @Bind(R.id.chapter_pages) TextView pages; @Bind(R.id.chapter_date) TextView date; - SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + private Context context; + + private final ChaptersAdapter adapter; + private Chapter item; private final int readColor; private final int unreadColor; + private final DecimalFormat decimalFormat; + private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + public ChaptersHolder(View view, ChaptersAdapter adapter, OnListItemClickListener listener) { super(view, adapter, listener); this.adapter = adapter; + context = view.getContext(); ButterKnife.bind(this, view); readColor = ContextCompat.getColor(view.getContext(), R.color.hint_text); unreadColor = ContextCompat.getColor(view.getContext(), R.color.primary_text); + DecimalFormatSymbols symbols = new DecimalFormatSymbols(); + symbols.setDecimalSeparator('.'); + decimalFormat = new DecimalFormat("#.###", symbols); + chapterMenu.setOnClickListener(v -> v.post(() -> showPopupMenu(v))); } - public void onSetValues(Context context, Chapter chapter) { + public void onSetValues(Chapter chapter, Manga manga) { this.item = chapter; - title.setText(chapter.name); + String name; + switch (manga.getDisplayMode()) { + case Manga.DISPLAY_NAME: + default: + name = chapter.name; + break; + case Manga.DISPLAY_NUMBER: + String formattedNumber = decimalFormat.format(chapter.chapter_number); + name = context.getString(R.string.display_mode_chapter, formattedNumber); + break; + } + title.setText(name); title.setTextColor(chapter.read ? readColor : unreadColor); if (!chapter.read && chapter.last_page_read > 0) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java index b87e35093b..e981ec0e7b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.java @@ -264,6 +264,11 @@ public class ChaptersPresenter extends BasePresenter { refreshChapters(); } + public void setDisplayMode(int mode) { + manga.setDisplayMode(mode); + db.insertManga(manga).executeAsBlocking(); + } + public boolean getSortOrder() { return manga.sortChaptersAZ(); } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java index 8f8a840a0f..938cc2b9f8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderMenu.java @@ -70,7 +70,7 @@ public class ReaderMenu { bottomMenu.setOnTouchListener((v, event) -> true); seekBar.setOnSeekBarChangeListener(new PageSeekBarChangeListener()); - decimalFormat = new DecimalFormat("#.##"); + decimalFormat = new DecimalFormat("#.###"); inverted = false; initializeOptions(); diff --git a/app/src/main/res/menu/chapters.xml b/app/src/main/res/menu/chapters.xml new file mode 100644 index 0000000000..1d56f62b39 --- /dev/null +++ b/app/src/main/res/menu/chapters.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7ba24db68d..bb72d2f5bc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -157,12 +157,15 @@ Chapters No title + Chapter %1$s Downloaded Queued Downloading Downloading (%1$d/%2$d) Error Error while fetching chapters + Show title + Show chapter number Reading