Beispiel #1
0
 @Override
 public boolean onNavigationItemSelected(int position, long l) {
   Chapter previous = mViewPagerAdapter.getSelectedChapter();
   mViewPagerAdapter.setSelectedChapter(mSpinnerAdapter.getItem(position));
   if (previous == null || !previous.equals(mSpinnerAdapter.getItem(position))) {
     Timber.d("Switching chapter!");
     mViewPagerAdapter.notifyDataSetChanged();
   }
   return true;
 }
Beispiel #2
0
  /**
   * Called when the activity is first created.
   *
   * @param savedInstanceState If the activity is being re-initialized after previously being shut
   *     down then this Bundle contains the data it most recently supplied in
   *     onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Timber.i("onCreate");
    setContentView(R.layout.activity_main);

    mClient = new GroupDirectory();

    mLocationComparator = new ChapterComparator(mPreferences);

    mIndicator.setOnPageChangeListener(this);

    mViewPagerAdapter = new MyAdapter(this, getSupportFragmentManager());
    mSpinnerAdapter = new ChapterAdapter(MainActivity.this, android.R.layout.simple_list_item_1);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, MainActivity.this);

    mFetchChaptersTask =
        mClient.getDirectory(
            new Response.Listener<Directory>() {
              @Override
              public void onResponse(final Directory directory) {
                getSupportActionBar()
                    .setListNavigationCallbacks(mSpinnerAdapter, MainActivity.this);
                App.getInstance()
                    .getModelCache()
                    .putAsync(
                        "chapter_list",
                        directory,
                        DateTime.now().plusDays(1),
                        new ModelCache.CachePutListener() {
                          @Override
                          public void onPutIntoCache() {
                            ArrayList<Chapter> chapters = directory.getGroups();

                            initChapters(chapters);
                          }
                        });
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError volleyError) {
                Crouton.makeText(
                        MainActivity.this, getString(R.string.fetch_chapters_failed), Style.ALERT)
                    .show();
                Timber.e("Could'nt fetch chapter list", volleyError);
              }
            });

    if (savedInstanceState == null) {

      if (Utils.isOnline(this)) {
        App.getInstance()
            .getModelCache()
            .getAsync(
                "chapter_list",
                new ModelCache.CacheListener() {
                  @Override
                  public void onGet(Object item) {
                    Directory directory = (Directory) item;
                    initChapters(directory.getGroups());
                  }

                  @Override
                  public void onNotFound(String key) {
                    mFetchChaptersTask.execute();
                  }
                });
      } else {

        App.getInstance()
            .getModelCache()
            .getAsync(
                "chapter_list",
                false,
                new ModelCache.CacheListener() {
                  @Override
                  public void onGet(Object item) {
                    Directory directory = (Directory) item;
                    initChapters(directory.getGroups());
                  }

                  @Override
                  public void onNotFound(String key) {
                    Crouton.makeText(
                            MainActivity.this, getString(R.string.offline_alert), Style.ALERT)
                        .show();
                  }
                });
      }
    } else {

      if (savedInstanceState.containsKey("chapters")) {
        ArrayList<Chapter> chapters = savedInstanceState.getParcelableArrayList("chapters");
        mSpinnerAdapter.clear();
        mSpinnerAdapter.addAll(chapters);

        if (savedInstanceState.containsKey("selected_chapter")) {
          Chapter selectedChapter = savedInstanceState.getParcelable("selected_chapter");
          selectChapter(selectedChapter);
        } else {
          mViewPagerAdapter.setSelectedChapter(chapters.get(0));
        }

        mViewPager.setAdapter(mViewPagerAdapter);
        mIndicator.setViewPager(mViewPager);
      } else {
        mFetchChaptersTask.execute();
      }
    }

    Intent intent = getIntent();
    if (intent != null
        && intent.getAction() != null
        && intent.getAction().equals("finish_first_start")) {
      Timber.d("Completed FirstStartWizard");

      if (mPreferences.getBoolean(Const.SETTINGS_SIGNED_IN, false)) {
        mFirstStart = true;
      }

      Chapter homeGdgd = getIntent().getParcelableExtra("selected_chapter");
      getSupportActionBar().setSelectedNavigationItem(mSpinnerAdapter.getPosition(homeGdgd));
      mViewPagerAdapter.setSelectedChapter(homeGdgd);
    }

    Time now = new Time();
    now.setToNow();
    if ((mPreferences.getInt(Const.SETTINGS_SEASONS_GREETINGS, now.year - 1) < now.year)
        && (now.yearDay >= 354 && now.yearDay <= 366)) {
      mPreferences.edit().putInt(Const.SETTINGS_SEASONS_GREETINGS, now.year).commit();
      SeasonsGreetingsFragment seasonsGreetings = new SeasonsGreetingsFragment();
      seasonsGreetings.show(getSupportFragmentManager(), "dialog");
    }
  }
Beispiel #3
0
 private void selectChapter(Chapter chapter) {
   mViewPagerAdapter.setSelectedChapter(chapter);
   getSupportActionBar().setSelectedNavigationItem(mSpinnerAdapter.getPosition(chapter));
 }