Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / ui / tableofcontents / presenter / TableOfContentsPresenter.java
1 package com.folioreader.ui.tableofcontents.presenter;
2
3 import com.folioreader.model.TOCLinkWrapper;
4 import com.folioreader.ui.base.ManifestCallBack;
5 import com.folioreader.ui.base.ManifestTask;
6
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;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  * @author gautam chibde on 8/6/17.
16  */
17
18 public class TableOfContentsPresenter implements ManifestCallBack {
19
20     private TOCMvpView tocMvpView;
21
22     public TableOfContentsPresenter(TOCMvpView tocMvpView) {
23         this.tocMvpView = tocMvpView;
24     }
25
26     public void getTOCContent(String url) {
27         new ManifestTask(this).execute(url);
28     }
29
30     /**
31      * [RECURSIVE]
32      * <p>
33      * function generates list of {@link TOCLinkWrapper} of TOC list from publication manifest
34      *
35      * @param tocLink     table of content elements
36      * @param indentation level of hierarchy of the child elements
37      * @return generated {@link TOCLinkWrapper} list
38      */
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);
46                 }
47             }
48         }
49         return tocLinkWrapper;
50     }
51
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));
59         }
60         return tocLinkWrappers;
61     }
62
63     @Override
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);
71                 }
72                 tocMvpView.onLoadTOC(tocLinkWrappers);
73             } else {
74                 tocMvpView.onLoadTOC(createTOCFromSpine(publication.spines));
75             }
76         } else {
77             tocMvpView.onError();
78         }
79     }
80
81     @Override
82     public void onError() {
83         tocMvpView.onError();
84     }
85 }