Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.imaadv.leaynik.Activities;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.res.Resources;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.view.GravityCompat;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.Toolbar;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import com.imaadv.leaynik.R;
- import com.imaadv.leaynik.api.API;
- import com.imaadv.leaynik.api.AlertDialogManager;
- import com.imaadv.leaynik.defs.AppConstants;
- import com.imaadv.leaynik.defs.DrawerItem;
- import com.imaadv.leaynik.test.Test_Home;
- import com.imaadv.leaynik.ui.DrawerAdapter;
- import com.kobakei.ratethisapp.RateThisApp;
- import java.util.ArrayList;
- public class Home extends AppCompatActivity {
- private RecyclerView DrawerList;
- private DrawerLayout mDrawerLayout;
- private API api;
- private Toolbar _toolbar;
- private ActionBarDrawerToggle mDrawerToggle;
- private String[] nav_items;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.home_view);
- api = new API(this);
- // library to show rate app after 5days or 7 time app opened
- RateThisApp.init(new RateThisApp.Config(5, 7));
- // check if this first time app opened
- if (isFirstTime()) openAlert();
- Resources res = getResources();
- DrawerList = (RecyclerView) findViewById(R.id.DrawerList);
- DrawerList.setLayoutManager(new LinearLayoutManager(this));
- _toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(_toolbar);
- getSupportActionBar().setHomeButtonEnabled(true);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
- ArrayList<DrawerItem> Items = new ArrayList<>(9);
- nav_items = res.getStringArray(R.array.nav_items);
- TypedArray nav_icons = res.obtainTypedArray(R.array.nav_icon);
- for (int i = 0; i < nav_items.length; i++) {
- Items.add(new DrawerItem(nav_items[i], nav_icons.getResourceId(i, -1)));
- }
- nav_icons.recycle();
- DrawerAdapter adapter = new DrawerAdapter(Home.this, Items);
- DrawerList.setAdapter(adapter);
- _toolbar.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
- mDrawerLayout.closeDrawer(GravityCompat.START);
- } else {
- mDrawerLayout.openDrawer(GravityCompat.START);
- }
- }
- });
- mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, _toolbar, R.string.app_name, R.string.app_name) {
- @Override
- public void onDrawerOpened(View drawerView) {
- super.onDrawerOpened(drawerView);
- }
- @Override
- public void onDrawerClosed(View drawerView) {
- super.onDrawerClosed(drawerView);
- }
- };
- mDrawerLayout.setDrawerListener(mDrawerToggle);
- mDrawerToggle.setDrawerIndicatorEnabled(true);
- DrawerList.addOnItemTouchListener(new RecycleOntouchListner(this, new ClickListner() {
- @Override
- public void OnClick(View view, int position) {
- SelectItem(position);
- }
- }));
- if (savedInstanceState == null) {
- SelectItem(0);
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- RateThisApp.onStart(Home.this);
- RateThisApp.showRateDialogIfNeeded(Home.this);
- }
- private void openAlert() {
- api.setSetting(AppConstants.ALERT, AppConstants.TRUE);
- AlertDialogManager.showAlertDialog(Home.this, getResources().getString(R.string.app_name), getResources().getString(R.string.sos), null, getResources().getString(R.string.ok));
- }
- private void Logout() {
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
- Home.this);
- alertDialogBuilder
- .setTitle(getResources().getString(R.string.app_name));
- alertDialogBuilder.setMessage(getResources().getString(
- R.string.logout_msg));
- // set positive button: Yes message
- alertDialogBuilder.setPositiveButton(
- getResources().getString(R.string.ok),
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- if (api == null) {
- api = new API(Home.this);
- }
- api.clearSettings();
- finish();
- }
- });
- alertDialogBuilder.setNegativeButton(
- getResources().getString(R.string.cancel),
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- // show alert
- alertDialog.show();
- }
- public void SelectItem(int position) {
- Fragment _fragment = null;
- switch (position) {
- case 0:
- _fragment = Test_Home.newInstance();
- break;
- case 1:
- _fragment = Diases_Fragment.newInstance();
- break;
- case 2:
- _fragment = Questions_Fragment.newInstance();
- break;
- case 3:
- _fragment = Clinc_Fragment.newInstance();
- break;
- case 4:
- _fragment = Reminder_Fragment.newInstance();
- break;
- case 5:
- _fragment = ArticalesFragment.newInstance();
- break;
- case 6:
- _fragment = Profile_Fragment.newInstance();
- break;
- case 7:
- _fragment = Competion_Fragment.newInstance();
- break;
- case 8:
- Logout();
- break;
- default:
- break;
- }
- if (_fragment != null) {
- FragmentManager fragmentManager = getSupportFragmentManager();
- fragmentManager.beginTransaction().replace(R.id.view_content, _fragment).addToBackStack(null).commit();
- setTitle(nav_items[position]);
- mDrawerLayout.closeDrawer(GravityCompat.START);
- }
- //DrawerList.setSelected(true);
- }
- public class RecycleOntouchListner implements RecyclerView.OnItemTouchListener {
- private GestureDetector mGestureDetector;
- private ClickListner mClickListner;
- public RecycleOntouchListner(Context mContext, ClickListner clickListner) {
- this.mClickListner = clickListner;
- mGestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- return true;
- }
- });
- }
- @Override
- public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
- View child = rv.findChildViewUnder(e.getX(), e.getY());
- if (child != null && mClickListner != null && mGestureDetector.onTouchEvent(e)) {
- mClickListner.OnClick(child, rv.getChildAdapterPosition(child));
- return false;
- }
- return false;
- }
- @Override
- public void onTouchEvent(RecyclerView rv, MotionEvent e) {
- }
- @Override
- public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
- }
- }
- public static interface ClickListner {
- void OnClick(View view, int position);
- }
- public boolean isFirstTime() {
- return !(api.getSetting(AppConstants.ALERT).contentEquals(AppConstants.TRUE));
- }
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- // Sync the toggle state after onRestoreInstanceState has occurred.
- mDrawerToggle.syncState();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement