Java tutorial
/* * * Copyright 2015, Jose Calles * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.josecalles.tistiq.mvp.view; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; import android.content.res.Resources; import android.content.res.TypedArray; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.widget.BaseAdapter; import android.widget.FrameLayout; import android.widget.HeaderViewListAdapter; import android.widget.ListView; import android.widget.Toast; import butterknife.Bind; import butterknife.ButterKnife; import com.josecalles.tistiq.TistiqApplication; import com.josecalles.tistiq.R; import com.josecalles.tistiq.event.StatsUnavailableEvent; import com.josecalles.tistiq.event.SyncCompleteEvent; import com.josecalles.tistiq.injection.activity.DaggerMainComponent; import com.josecalles.tistiq.injection.activity.MainComponent; import com.josecalles.tistiq.injection.activity.MainModule; import com.josecalles.tistiq.mvp.contract.MainView; import com.josecalles.tistiq.mvp.presenter.MainPresenter; import com.josecalles.tistiq.type.FragmentType; import com.josecalles.tistiq.type.ThemeType; import java.util.List; import javax.inject.Inject; import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; public class MainActivity extends AppCompatActivity implements MainView { @Bind(R.id.drawer_layout) DrawerLayout mDrawerLayout; @Bind(R.id.nvView) NavigationView mNavigationView; @Bind(R.id.fragment_container) FrameLayout mFragmentContainer; @Bind(R.id.toolbar) Toolbar mToolbar; @Inject MainPresenter mMainPresenter; private static final int BACK_PRESS_INTERVAL = 2000; private long mBackPressedDuration; private int mSelectedTheme; private MainComponent mMainComponent; private SubMenu submenu; private ActionBarDrawerToggle mDrawerToggle; private ProgressDialog mProgressDialog; private TypedArray mBackgrounds, mPrimaryColors, mPrimaryDarkColors, mAccents; private AlertDialog mAlertDialog; @Override protected void onCreate(Bundle savedInstanceState) { injectPresenter(); mMainPresenter.getSelectedTheme(); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); getThemeColorPalette(); setUpToolbarAndDrawer(); setViewColors(); } @Override protected void onStart() { super.onStart(); mMainPresenter.onActivityStarted(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); if (mAlertDialog != null) { if (mAlertDialog.isShowing()) mAlertDialog.dismiss(); } if (mProgressDialog != null) { if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); } mMainPresenter.onActivityPaused(); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); } @Override public void onBackPressed() { if (mBackPressedDuration + BACK_PRESS_INTERVAL > System.currentTimeMillis()) { super.onBackPressed(); return; } else { Toast.makeText(this, R.string.back_again_exit, Toast.LENGTH_SHORT).show(); } mBackPressedDuration = System.currentTimeMillis(); } @Override public void updateContactListView(List<String> contacts) { submenu.clear(); int i = 0; for (String name : contacts) { switch (i) { case 0: submenu.add(Menu.NONE, Menu.NONE, Menu.NONE, name).setIcon(R.drawable.ic_one_rank); break; case 1: submenu.add(Menu.NONE, Menu.NONE, Menu.NONE, name).setIcon(R.drawable.ic_two_rank); break; case 2: submenu.add(Menu.NONE, Menu.NONE, Menu.NONE, name).setIcon(R.drawable.ic_three_rank); break; default: submenu.add(Menu.NONE, Menu.NONE, Menu.NONE, name); break; } i++; } refreshContactList(); dismissProgressDialog(); } private void refreshContactList() { for (int j = 0, count = mNavigationView.getChildCount(); j < count; j++) { final View child = mNavigationView.getChildAt(j); if (child != null && child instanceof ListView) { final ListView menuView = (ListView) child; final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter(); final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter(); wrapped.notifyDataSetChanged(); } } } @Override public void startFragment(int fragmentType) { if (fragmentType == FragmentType.SETTINGS) { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); } else { StatDisplayFragment contactStatFragment = new StatDisplayFragment(); Bundle bundle = new Bundle(); bundle.putInt("FRAGMENT_TYPE", fragmentType); contactStatFragment.setArguments(bundle); getFragmentManager().beginTransaction().replace(R.id.fragment_container, contactStatFragment).commit(); } mDrawerLayout.closeDrawers(); } private void setUpToolbarAndDrawer() { //mToolbar.setTitle(""); setSupportActionBar(mToolbar); final ActionBar actionBar = getSupportActionBar(); actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); actionBar.setDisplayHomeAsUpEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close); mDrawerLayout.setDrawerListener(mDrawerToggle); setupDrawerContent(mNavigationView); Menu mNavMenu = mNavigationView.getMenu(); submenu = mNavMenu.addSubMenu(R.string.individual_header); } private void injectPresenter() { TistiqApplication app = (TistiqApplication) getApplication(); mMainComponent = DaggerMainComponent.builder().appComponent(app.getAppComponent()) .mainModule(new MainModule(this)).build(); mMainComponent.inject(this); } public MainComponent getMainComponent() { return mMainComponent; } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { selectDrawerItem(menuItem); return true; } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } switch (item.getItemId()) { case android.R.id.home: mDrawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } public void selectDrawerItem(MenuItem menuItem) { mMainPresenter.onMenuItemSelected(menuItem); mDrawerLayout.closeDrawers(); } @Override public void showProgressDialog() { mProgressDialog = ProgressDialog.show(this, null, getResources().getString(R.string.updating_stats)); } @Override public void dismissProgressDialog() { if (mProgressDialog != null) { mProgressDialog.dismiss(); new Handler().postDelayed(new Runnable() { @Override public void run() { mDrawerLayout.openDrawer(GravityCompat.START); } }, 250); } } @Override public void setActivityTheme(int themeID) { mSelectedTheme = themeID; switch (mSelectedTheme) { case ThemeType.INDIGO: setTheme(R.style.IndigoAppTheme); break; case ThemeType.ORANGE: setTheme(R.style.OrangeAppTheme); break; case ThemeType.GREEN: setTheme(R.style.GreenAppTheme); break; case ThemeType.PINK: setTheme(R.style.PinkAppTheme); break; case ThemeType.RED: setTheme(R.style.RedAppTheme); break; case ThemeType.BLUE: setTheme(R.style.BlueAppTheme); break; } } private void setViewColors() { mFragmentContainer.setBackground(mBackgrounds.getDrawable(mSelectedTheme)); mToolbar.setBackgroundColor(mPrimaryColors.getColor(mSelectedTheme, 0)); mNavigationView.setBackgroundColor(mPrimaryDarkColors.getColor(mSelectedTheme, 0)); mNavigationView.setItemIconTintList(ColorStateList.valueOf(mAccents.getColor(mSelectedTheme, 0))); } private void getThemeColorPalette() { Resources resources = getResources(); mBackgrounds = resources.obtainTypedArray(R.array.backgrounds); mPrimaryColors = resources.obtainTypedArray(R.array.primary_colors); mPrimaryDarkColors = resources.obtainTypedArray(R.array.primary_dark_colors); mAccents = resources.obtainTypedArray(R.array.accent_colors); } public void onEvent(SyncCompleteEvent event) { mMainPresenter.updateContactList(); } public void onEvent(StatsUnavailableEvent event) { mProgressDialog.dismiss(); mAlertDialog = new AlertDialog.Builder(this).setTitle(R.string.uh_oh).setMessage(R.string.error_message) .setCancelable(false).show(); } }