Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.alfianyusufabdullah.infofilm.ui.activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.alfianyusufabdullah.infofilm.App;
import com.alfianyusufabdullah.infofilm.R;
import com.alfianyusufabdullah.infofilm.base.BaseActivity;
import com.alfianyusufabdullah.infofilm.ui.fragment.movie.MoviePresenter;
import com.alfianyusufabdullah.infofilm.ui.fragment.FavoriteFragment;
import com.alfianyusufabdullah.infofilm.ui.fragment.FragmentCallback;
import com.alfianyusufabdullah.infofilm.ui.fragment.movie.MovieFragment;
import com.alfianyusufabdullah.infofilm.ui.fragment.SettingFragment;
import com.alfianyusufabdullah.infofilm.ui.fragment.search.SearchMovieFragment;
import com.alfianyusufabdullah.infofilm.ui.widget.FavoriteMovieWidget;
import com.alfianyusufabdullah.infofilm.utils.NotificationMovie;
import com.alfianyusufabdullah.infofilm.utils.PreferenceMovie;
import butterknife.BindView;
public class MainActivity extends BaseActivity implements FragmentCallback {
public static final String KEY_MOVIE_ID = "movie_id";
public static final String KEY_MOVIE = "movie";
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.mainDrawer)
DrawerLayout drawerLayout;
@BindView(R.id.mainNavigation)
NavigationView mainNavigation;
private SearchView searchView;
private String pageState = "";
private String titleState = "";
private int navigationState = 0;
private boolean fromDetail = false;
private String KEY_FRAGMENT_STATE = "fragment_state";
private String KEY_PAGE_STATE = "page_state";
private String KEY_TITLE_STATE = "title_state";
private String KEY_NAVIGATION_STATE = "navigation_state";
Fragment fragment;
Bundle savedInstances;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceMovie.setAppLastOpen();
savedInstances = savedInstanceState;
setContentView(R.layout.activity_main);
initView();
}
@Override
protected void onResume() {
super.onResume();
if (!fromDetail) {
if (savedInstances != null) {
navigationState = setNavigationMenuPosition(mainNavigation, savedInstances.getInt(KEY_NAVIGATION_STATE));
titleState = setToolbarTitle(toolbar, savedInstances.getString(KEY_TITLE_STATE));
fragment = replaceFragment(R.id.fragmentContainer, getSupportFragmentManager().getFragment(savedInstances, KEY_FRAGMENT_STATE), false);
pageState = savedInstances.getString(KEY_PAGE_STATE);
} else {
boolean openUpcomingFromNotification = getIntent().getBooleanExtra(NotificationMovie.OPEN_UPCOMING_FROM_NOTIFICATION, false);
if (openUpcomingFromNotification){
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_upcoming);
titleState = setToolbarTitle(toolbar, App.getStringFromResources(R.string.navigation_upcoming));
fragment = replaceFragment(R.id.fragmentContainer, MovieFragment.newInstance(MoviePresenter.MOVIE_UPCOMING), false);
pageState = MoviePresenter.MOVIE_UPCOMING;
} else {
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_now_playing);
titleState = setToolbarTitle(toolbar, App.getStringFromResources(R.string.navigation_now_playing));
fragment = replaceFragment(R.id.fragmentContainer, MovieFragment.newInstance(MoviePresenter.MOVIE_NOW_PLAYING), false);
pageState = MoviePresenter.MOVIE_NOW_PLAYING;
}
}
}
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(Gravity.START)) {
drawerLayout.closeDrawers();
} else {
super.onBackPressed();
}
}
private void initView() {
FavoriteMovieWidget.updateWidget(this);
toolbar.setTitle(App.getStringFromResources(R.string.navigation_now_playing));
setSupportActionBar(toolbar);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
mainNavigation.getLayoutParams().width = App.deviceWidth() / 2;
mainNavigation.requestLayout();
mainNavigation.setCheckedItem(R.id.menu_now_playing);
mainNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_now_playing:
if (!pageState.equals(MoviePresenter.MOVIE_NOW_PLAYING)) {
pageState = MoviePresenter.MOVIE_NOW_PLAYING;
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_now_playing);
fragment = replaceFragment(R.id.fragmentContainer, MovieFragment.newInstance(MoviePresenter.MOVIE_NOW_PLAYING), false);
}
break;
case R.id.menu_popular:
if (!pageState.equals(MoviePresenter.MOVIE_POPULAR)) {
pageState = MoviePresenter.MOVIE_POPULAR;
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_popular);
fragment = replaceFragment(R.id.fragmentContainer, MovieFragment.newInstance(MoviePresenter.MOVIE_POPULAR), false);
}
break;
case R.id.menu_upcoming:
if (!pageState.equals(MoviePresenter.MOVIE_UPCOMING)) {
pageState = MoviePresenter.MOVIE_UPCOMING;
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_upcoming);
fragment = replaceFragment(R.id.fragmentContainer, MovieFragment.newInstance(MoviePresenter.MOVIE_UPCOMING), false);
}
break;
case R.id.menu_setting:
if (!pageState.equals(STATE_SETTING)) {
pageState = STATE_SETTING;
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_setting);
fragment = replaceFragment(R.id.fragmentContainer, new SettingFragment(), false);
}
break;
case R.id.menu_favorite:
if (!pageState.equals(STATE_FAVORITE)) {
pageState = STATE_FAVORITE;
navigationState = setNavigationMenuPosition(mainNavigation, R.id.menu_favorite);
fragment = replaceFragment(R.id.fragmentContainer, new FavoriteFragment(), false);
}
break;
default:
Toast.makeText(MainActivity.this, App.getStringFromResources(R.string.next_update), Toast.LENGTH_SHORT).show();
break;
}
titleState = setToolbarTitle(toolbar, item.getTitle().toString());
drawerLayout.closeDrawers();
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
replaceFragment(R.id.fragmentContainer, new SearchMovieFragment(), true);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
onBackPressed();
return true;
}
});
searchView = (SearchView) item.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(App.getStringFromResources(R.string.fragment_search_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
SearchMovieFragment fragment = (SearchMovieFragment) getSupportFragmentManager().findFragmentByTag(SearchMovieFragment.class.getSimpleName());
fragment.doSearch(query);
searchView.clearFocus();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
@Override
public void notifiyToActivity() {
fromDetail = true;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString(KEY_PAGE_STATE, pageState);
outState.putString(KEY_TITLE_STATE, titleState);
outState.putInt(KEY_NAVIGATION_STATE, navigationState);
getSupportFragmentManager().putFragment(outState, KEY_FRAGMENT_STATE, fragment);
super.onSaveInstanceState(outState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.