blob: de0711f46311434c8cf0023fec2321151028a955 [file] [log] [blame]
David Morrisseyc930ef72017-11-02 18:27:22 +00001/*
2Copyright 2017 David Morrissey
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package com.davemorrissey.labs.subscaleview.sample;
18
19import android.app.ActionBar;
20import android.os.Bundle;
21import android.support.annotation.Nullable;
22import android.support.v4.app.FragmentActivity;
23import android.view.MenuItem;
24import android.view.View;
25import android.widget.TextView;
26
27import java.util.List;
28
29public abstract class AbstractPagesActivity extends FragmentActivity {
30
31 private static final String BUNDLE_PAGE = "page";
32
33 private int page;
34
35 private final int title;
36 private final int layout;
37 private final List<Page> notes;
38
39 protected AbstractPagesActivity(int title, int layout, List<Page> notes) {
40 this.title = title;
41 this.layout = layout;
42 this.notes = notes;
43 }
44
45 @Override
46 protected void onCreate(@Nullable Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48 setContentView(layout);
49 ActionBar actionBar = getActionBar();
50 if (actionBar != null) {
51 actionBar.setTitle(getString(title));
52 actionBar.setDisplayHomeAsUpEnabled(true);
53 }
54 findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
55 @Override public void onClick(View v) { next(); }
56 });
57 findViewById(R.id.previous).setOnClickListener(new View.OnClickListener() {
58 @Override public void onClick(View v) { previous(); }
59 });
60 if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_PAGE)) {
61 page = savedInstanceState.getInt(BUNDLE_PAGE);
62 }
63 }
64
65 @Override
66 protected void onResume() {
67 super.onResume();
68 updateNotes();
69 }
70
71 @Override
72 protected void onSaveInstanceState(Bundle outState) {
73 super.onSaveInstanceState(outState);
74 outState.putInt(BUNDLE_PAGE, page);
75 }
76
77 @Override
78 public boolean onOptionsItemSelected(MenuItem item) {
79 finish();
80 return true;
81 }
82
83 private void next() {
84 page++;
85 updateNotes();
86 }
87
88 private void previous() {
89 page--;
90 updateNotes();
91 }
92
93 private void updateNotes() {
94 if (page > notes.size() - 1) {
95 return;
96 }
97 ActionBar actionBar = getActionBar();
98 if (actionBar != null) {
99 actionBar.setSubtitle(notes.get(page).getSubtitle());
100 }
101 ((TextView)findViewById(R.id.note)).setText(notes.get(page).getText());
102 findViewById(R.id.next).setVisibility(page >= notes.size() - 1 ? View.INVISIBLE : View.VISIBLE);
103 findViewById(R.id.previous).setVisibility(page <= 0 ? View.INVISIBLE : View.VISIBLE);
104 onPageChanged(page);
105 }
106
107 protected final int getPage() {
108 return page;
109 }
110
111 protected void onPageChanged(int page) {
112
113 }
114
115}