1 package com.folioreader.ui.tableofcontents.presenter;
3 import com.folioreader.model.TOCLinkWrapper;
4 import com.folioreader.ui.base.ManifestCallBack;
5 import com.folioreader.ui.base.ManifestTask;
7 import org.readium.r2_streamer.model.publication.EpubPublication;
8 import org.readium.r2_streamer.model.publication.link.Link;
9 import org.readium.r2_streamer.model.tableofcontents.TOCLink;
11 import java.util.ArrayList;
12 import java.util.List;
15 * @author gautam chibde on 8/6/17.
18 public class TableOfContentsPresenter implements ManifestCallBack {
20 private TOCMvpView tocMvpView;
22 public TableOfContentsPresenter(TOCMvpView tocMvpView) {
23 this.tocMvpView = tocMvpView;
26 public void getTOCContent(String url) {
27 new ManifestTask(this).execute(url);
33 * function generates list of {@link TOCLinkWrapper} of TOC list from publication manifest
35 * @param tocLink table of content elements
36 * @param indentation level of hierarchy of the child elements
37 * @return generated {@link TOCLinkWrapper} list
39 private static TOCLinkWrapper createTocLinkWrapper(TOCLink tocLink, int indentation) {
40 TOCLinkWrapper tocLinkWrapper = new TOCLinkWrapper(tocLink, indentation);
41 if (tocLink.getTocLinks() != null && !tocLink.getTocLinks().isEmpty()) {
42 for (TOCLink tocLink1 : tocLink.getTocLinks()) {
43 TOCLinkWrapper tocLinkWrapper1 = createTocLinkWrapper(tocLink1, indentation + 1);
44 if (tocLinkWrapper1.getIndentation() != 3) {
45 tocLinkWrapper.addChild(tocLinkWrapper1);
49 return tocLinkWrapper;
52 private static ArrayList<TOCLinkWrapper> createTOCFromSpine(List<Link> spine) {
53 ArrayList<TOCLinkWrapper> tocLinkWrappers = new ArrayList<>();
54 for (Link link : spine) {
55 TOCLink tocLink = new TOCLink();
56 tocLink.bookTitle = link.bookTitle;
57 tocLink.href = link.href;
58 tocLinkWrappers.add(new TOCLinkWrapper(tocLink, 0));
60 return tocLinkWrappers;
64 public void onReceivePublication(EpubPublication publication) {
65 if (publication != null) {
66 if (publication.tableOfContents != null) {
67 ArrayList<TOCLinkWrapper> tocLinkWrappers = new ArrayList<>();
68 for (TOCLink tocLink : publication.tableOfContents) {
69 TOCLinkWrapper tocLinkWrapper = createTocLinkWrapper(tocLink, 0);
70 tocLinkWrappers.add(tocLinkWrapper);
72 tocMvpView.onLoadTOC(tocLinkWrappers);
74 tocMvpView.onLoadTOC(createTOCFromSpine(publication.spines));
82 public void onError() {