1 package com.folioreader.ui.tableofcontents.view;
\r
3 import android.app.Activity;
\r
4 import android.content.Intent;
\r
5 import android.os.Bundle;
\r
6 import android.support.annotation.Nullable;
\r
7 import android.support.v4.app.Fragment;
\r
8 import android.support.v4.content.ContextCompat;
\r
9 import android.support.v7.widget.DividerItemDecoration;
\r
10 import android.support.v7.widget.LinearLayoutManager;
\r
11 import android.support.v7.widget.RecyclerView;
\r
12 import android.view.LayoutInflater;
\r
13 import android.view.View;
\r
14 import android.view.ViewGroup;
\r
15 import android.widget.TextView;
\r
17 import com.folioreader.Config;
\r
18 import com.folioreader.Constants;
\r
19 import com.folioreader.R;
\r
20 import com.folioreader.model.TOCLinkWrapper;
\r
21 import com.folioreader.ui.tableofcontents.adapter.TOCAdapter;
\r
22 import com.folioreader.ui.tableofcontents.presenter.TOCMvpView;
\r
23 import com.folioreader.ui.tableofcontents.presenter.TableOfContentsPresenter;
\r
24 import com.folioreader.util.AppUtil;
\r
26 import java.util.ArrayList;
\r
28 import static com.folioreader.Constants.BOOK_TITLE;
\r
29 import static com.folioreader.Constants.CHAPTER_SELECTED;
\r
30 import static com.folioreader.Constants.SELECTED_CHAPTER_POSITION;
\r
31 import static com.folioreader.Constants.TYPE;
\r
33 public class TableOfContentFragment extends Fragment implements TOCMvpView, TOCAdapter.TOCCallback {
\r
34 private TOCAdapter mTOCAdapter;
\r
35 private RecyclerView mTableOfContentsRecyclerView;
\r
36 private TableOfContentsPresenter presenter;
\r
37 private TextView errorView;
\r
38 private Config mConfig;
\r
39 private String mBookTitle;
\r
41 public static TableOfContentFragment newInstance(String selectedChapterHref, String bookTitle) {
\r
42 TableOfContentFragment tableOfContentFragment = new TableOfContentFragment();
\r
43 Bundle args = new Bundle();
\r
44 args.putString(SELECTED_CHAPTER_POSITION, selectedChapterHref);
\r
45 args.putString(BOOK_TITLE, bookTitle);
\r
46 tableOfContentFragment.setArguments(args);
\r
47 return tableOfContentFragment;
\r
51 public void onCreate(@Nullable Bundle savedInstanceState) {
\r
52 super.onCreate(savedInstanceState);
\r
53 presenter = new TableOfContentsPresenter(this);
\r
57 public View onCreateView(LayoutInflater inflater,
\r
58 ViewGroup container, Bundle savedInstanceState) {
\r
59 View mRootView = inflater.inflate(R.layout.fragment_contents, container, false);
\r
60 mConfig = AppUtil.getSavedConfig(getActivity());
\r
61 mBookTitle = getArguments().getString(BOOK_TITLE);
\r
63 View recyclerView = mRootView.findViewById(R.id.recycler_view_menu);
\r
64 if (mConfig.isNightMode()) {
\r
65 recyclerView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.dark_night));
\r
67 recyclerView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));
\r
73 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
\r
74 super.onViewCreated(view, savedInstanceState);
\r
75 mTableOfContentsRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_menu);
\r
76 errorView = (TextView) view.findViewById(R.id.tv_error);
\r
77 String urlString = Constants.LOCALHOST + mBookTitle + "/manifest";
\r
79 configRecyclerViews();
\r
80 presenter.getTOCContent(urlString);
\r
83 public void configRecyclerViews() {
\r
84 mTableOfContentsRecyclerView.setHasFixedSize(true);
\r
85 mTableOfContentsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
\r
86 mTableOfContentsRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
\r
90 public void onLoadTOC(ArrayList<TOCLinkWrapper> tocLinkWrapperList) {
\r
91 mTOCAdapter = new TOCAdapter(getActivity(), tocLinkWrapperList, getArguments().getString(SELECTED_CHAPTER_POSITION), mConfig);
\r
92 mTOCAdapter.setCallback(this);
\r
93 mTableOfContentsRecyclerView.setAdapter(mTOCAdapter);
\r
97 public void onError() {
\r
98 errorView.setVisibility(View.VISIBLE);
\r
99 mTableOfContentsRecyclerView.setVisibility(View.GONE);
\r
100 errorView.setText("Table of content \n not found");
\r
104 public void onTocClicked(int position) {
\r
105 TOCLinkWrapper tocLinkWrapper = (TOCLinkWrapper) mTOCAdapter.getItemAt(position);
\r
106 Intent intent = new Intent();
\r
107 intent.putExtra(SELECTED_CHAPTER_POSITION, tocLinkWrapper.getTocLink().href);
\r
108 intent.putExtra(BOOK_TITLE, tocLinkWrapper.getTocLink().bookTitle);
\r
109 intent.putExtra(TYPE, CHAPTER_SELECTED);
\r
110 getActivity().setResult(Activity.RESULT_OK, intent);
\r
111 getActivity().finish();
\r
115 public void onExpanded(int position) {
\r
116 TOCLinkWrapper tocLinkWrapper = (TOCLinkWrapper) mTOCAdapter.getItemAt(position);
\r
117 if (tocLinkWrapper.getChildren() != null && tocLinkWrapper.getChildren().size() > 0) {
\r
118 mTOCAdapter.toggleGroup(position);
\r