--- /dev/null
+package com.folioreader.ui.tableofcontents.view;\r
+\r
+import android.app.Activity;\r
+import android.content.Intent;\r
+import android.os.Bundle;\r
+import android.support.annotation.Nullable;\r
+import android.support.v4.app.Fragment;\r
+import android.support.v4.content.ContextCompat;\r
+import android.support.v7.widget.DividerItemDecoration;\r
+import android.support.v7.widget.LinearLayoutManager;\r
+import android.support.v7.widget.RecyclerView;\r
+import android.view.LayoutInflater;\r
+import android.view.View;\r
+import android.view.ViewGroup;\r
+import android.widget.TextView;\r
+\r
+import com.folioreader.Config;\r
+import com.folioreader.Constants;\r
+import com.folioreader.R;\r
+import com.folioreader.model.TOCLinkWrapper;\r
+import com.folioreader.ui.tableofcontents.adapter.TOCAdapter;\r
+import com.folioreader.ui.tableofcontents.presenter.TOCMvpView;\r
+import com.folioreader.ui.tableofcontents.presenter.TableOfContentsPresenter;\r
+import com.folioreader.util.AppUtil;\r
+\r
+import java.util.ArrayList;\r
+\r
+import static com.folioreader.Constants.BOOK_TITLE;\r
+import static com.folioreader.Constants.CHAPTER_SELECTED;\r
+import static com.folioreader.Constants.SELECTED_CHAPTER_POSITION;\r
+import static com.folioreader.Constants.TYPE;\r
+\r
+public class TableOfContentFragment extends Fragment implements TOCMvpView, TOCAdapter.TOCCallback {\r
+ private TOCAdapter mTOCAdapter;\r
+ private RecyclerView mTableOfContentsRecyclerView;\r
+ private TableOfContentsPresenter presenter;\r
+ private TextView errorView;\r
+ private Config mConfig;\r
+ private String mBookTitle;\r
+\r
+ public static TableOfContentFragment newInstance(String selectedChapterHref, String bookTitle) {\r
+ TableOfContentFragment tableOfContentFragment = new TableOfContentFragment();\r
+ Bundle args = new Bundle();\r
+ args.putString(SELECTED_CHAPTER_POSITION, selectedChapterHref);\r
+ args.putString(BOOK_TITLE, bookTitle);\r
+ tableOfContentFragment.setArguments(args);\r
+ return tableOfContentFragment;\r
+ }\r
+\r
+ @Override\r
+ public void onCreate(@Nullable Bundle savedInstanceState) {\r
+ super.onCreate(savedInstanceState);\r
+ presenter = new TableOfContentsPresenter(this);\r
+ }\r
+\r
+ @Override\r
+ public View onCreateView(LayoutInflater inflater,\r
+ ViewGroup container, Bundle savedInstanceState) {\r
+ View mRootView = inflater.inflate(R.layout.fragment_contents, container, false);\r
+ mConfig = AppUtil.getSavedConfig(getActivity());\r
+ mBookTitle = getArguments().getString(BOOK_TITLE);\r
+\r
+ View recyclerView = mRootView.findViewById(R.id.recycler_view_menu);\r
+ if (mConfig.isNightMode()) {\r
+ recyclerView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.dark_night));\r
+ }else{\r
+ recyclerView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));\r
+ }\r
+ return mRootView;\r
+ }\r
+\r
+ @Override\r
+ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {\r
+ super.onViewCreated(view, savedInstanceState);\r
+ mTableOfContentsRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_menu);\r
+ errorView = (TextView) view.findViewById(R.id.tv_error);\r
+ String urlString = Constants.LOCALHOST + mBookTitle + "/manifest";\r
+\r
+ configRecyclerViews();\r
+ presenter.getTOCContent(urlString);\r
+ }\r
+\r
+ public void configRecyclerViews() {\r
+ mTableOfContentsRecyclerView.setHasFixedSize(true);\r
+ mTableOfContentsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));\r
+ mTableOfContentsRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));\r
+ }\r
+\r
+ @Override\r
+ public void onLoadTOC(ArrayList<TOCLinkWrapper> tocLinkWrapperList) {\r
+ mTOCAdapter = new TOCAdapter(getActivity(), tocLinkWrapperList, getArguments().getString(SELECTED_CHAPTER_POSITION), mConfig);\r
+ mTOCAdapter.setCallback(this);\r
+ mTableOfContentsRecyclerView.setAdapter(mTOCAdapter);\r
+ }\r
+\r
+ @Override\r
+ public void onError() {\r
+ errorView.setVisibility(View.VISIBLE);\r
+ mTableOfContentsRecyclerView.setVisibility(View.GONE);\r
+ errorView.setText("Table of content \n not found");\r
+ }\r
+\r
+ @Override\r
+ public void onTocClicked(int position) {\r
+ TOCLinkWrapper tocLinkWrapper = (TOCLinkWrapper) mTOCAdapter.getItemAt(position);\r
+ Intent intent = new Intent();\r
+ intent.putExtra(SELECTED_CHAPTER_POSITION, tocLinkWrapper.getTocLink().href);\r
+ intent.putExtra(BOOK_TITLE, tocLinkWrapper.getTocLink().bookTitle);\r
+ intent.putExtra(TYPE, CHAPTER_SELECTED);\r
+ getActivity().setResult(Activity.RESULT_OK, intent);\r
+ getActivity().finish();\r
+ }\r
+\r
+ @Override\r
+ public void onExpanded(int position) {\r
+ TOCLinkWrapper tocLinkWrapper = (TOCLinkWrapper) mTOCAdapter.getItemAt(position);\r
+ if (tocLinkWrapper.getChildren() != null && tocLinkWrapper.getChildren().size() > 0) {\r
+ mTOCAdapter.toggleGroup(position);\r
+ }\r
+ }\r
+}
\ No newline at end of file