David Morrissey | c930ef7 | 2017-11-02 18:27:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 David Morrissey |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
David Morrissey | 44e2492 | 2017-11-20 10:36:41 +0000 | [diff] [blame^] | 17 | package com.davemorrissey.labs.subscaleview.test; |
David Morrissey | c930ef7 | 2017-11-02 18:27:22 +0000 | [diff] [blame] | 18 | |
| 19 | import android.app.ActionBar; |
| 20 | import android.os.Bundle; |
| 21 | import android.support.annotation.Nullable; |
| 22 | import android.support.v4.app.FragmentActivity; |
| 23 | import android.view.MenuItem; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | public 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 | } |