Home | History | Annotate | Download | only in test
      1 package com.davemorrissey.labs.subscaleview.test;
      2 
      3 import android.app.ActionBar;
      4 import android.os.Bundle;
      5 import android.support.annotation.Nullable;
      6 import android.support.v4.app.FragmentActivity;
      7 import android.view.MenuItem;
      8 import android.view.View;
      9 import android.widget.TextView;
     10 
     11 import java.util.List;
     12 
     13 public abstract class AbstractPagesActivity extends FragmentActivity {
     14 
     15     private static final String BUNDLE_PAGE = "page";
     16 
     17     private int page;
     18 
     19     private final int title;
     20     private final int layout;
     21     private final List<Page> notes;
     22 
     23     protected AbstractPagesActivity(int title, int layout, List<Page> notes) {
     24         this.title = title;
     25         this.layout = layout;
     26         this.notes = notes;
     27     }
     28 
     29     @Override
     30     protected void onCreate(@Nullable Bundle savedInstanceState) {
     31         super.onCreate(savedInstanceState);
     32         setContentView(layout);
     33         ActionBar actionBar = getActionBar();
     34         if (actionBar != null) {
     35             actionBar.setTitle(getString(title));
     36             actionBar.setDisplayHomeAsUpEnabled(true);
     37         }
     38         findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
     39             @Override public void onClick(View v) { next(); }
     40         });
     41         findViewById(R.id.previous).setOnClickListener(new View.OnClickListener() {
     42             @Override public void onClick(View v) { previous(); }
     43         });
     44         if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_PAGE)) {
     45             page = savedInstanceState.getInt(BUNDLE_PAGE);
     46         }
     47     }
     48 
     49     @Override
     50     protected void onResume() {
     51         super.onResume();
     52         updateNotes();
     53     }
     54 
     55     @Override
     56     protected void onSaveInstanceState(Bundle outState) {
     57         super.onSaveInstanceState(outState);
     58         outState.putInt(BUNDLE_PAGE, page);
     59     }
     60 
     61     @Override
     62     public boolean onOptionsItemSelected(MenuItem item) {
     63         finish();
     64         return true;
     65     }
     66 
     67     private void next() {
     68         page++;
     69         updateNotes();
     70     }
     71 
     72     private void previous() {
     73         page--;
     74         updateNotes();
     75     }
     76 
     77     private void updateNotes() {
     78         if (page > notes.size() - 1) {
     79             return;
     80         }
     81         ActionBar actionBar = getActionBar();
     82         if (actionBar != null) {
     83             actionBar.setSubtitle(notes.get(page).getSubtitle());
     84         }
     85         ((TextView)findViewById(R.id.note)).setText(notes.get(page).getText());
     86         findViewById(R.id.next).setVisibility(page >= notes.size() - 1 ? View.INVISIBLE : View.VISIBLE);
     87         findViewById(R.id.previous).setVisibility(page <= 0 ? View.INVISIBLE : View.VISIBLE);
     88         onPageChanged(page);
     89     }
     90 
     91     protected final int getPage() {
     92         return page;
     93     }
     94 
     95     protected void onPageChanged(int page) {
     96 
     97     }
     98 
     99 }
    100