blob: 1694d2a6ebb3ae7d94e86787176600d042b930f1 [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
David Morrissey44e24922017-11-20 10:36:41 +000017package com.davemorrissey.labs.subscaleview.test;
David Morrisseyc930ef72017-11-02 18:27:22 +000018
19import android.app.ActionBar;
20import android.os.Bundle;
21import android.support.annotation.Nullable;
22import android.support.v4.app.FragmentActivity;
23import android.view.MenuItem;
24
25import java.util.List;
26
27public abstract class AbstractFragmentsActivity extends FragmentActivity {
28
29 private static final String BUNDLE_PAGE = "page";
30
31 private int page;
32
33 private final int title;
34 private final int layout;
35 private final List<Page> notes;
36
37 protected abstract void onPageChanged(int page);
38
39 protected AbstractFragmentsActivity(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 if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_PAGE)) {
55 page = savedInstanceState.getInt(BUNDLE_PAGE);
56 }
57 }
58
59 @Override
60 protected void onResume() {
61 super.onResume();
62 updateNotes();
63 }
64
65 @Override
66 protected void onSaveInstanceState(Bundle outState) {
67 super.onSaveInstanceState(outState);
68 outState.putInt(BUNDLE_PAGE, page);
69 }
70
71 @Override
72 public boolean onOptionsItemSelected(MenuItem item) {
73 finish();
74 return true;
75 }
76
77 public void next() {
78 page++;
79 updateNotes();
80 }
81
82 public void previous() {
83 page--;
84 updateNotes();
85 }
86
87 private void updateNotes() {
88 if (page > notes.size() - 1) {
89 return;
90 }
91 ActionBar actionBar = getActionBar();
92 if (actionBar != null) {
93 actionBar.setSubtitle(notes.get(page).getSubtitle());
94 }
95 onPageChanged(page);
96 }
97
98}