Advertisement
minafaw3

home 2

Oct 11th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. package com.imaadv.leaynik.Activities;
  2.  
  3.  
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.res.Resources;
  8. import android.content.res.TypedArray;
  9. import android.os.Bundle;
  10. import android.support.v4.app.Fragment;
  11. import android.support.v4.app.FragmentManager;
  12. import android.support.v4.view.GravityCompat;
  13. import android.support.v4.widget.DrawerLayout;
  14. import android.support.v7.app.ActionBarDrawerToggle;
  15. import android.support.v7.app.AppCompatActivity;
  16. import android.support.v7.widget.LinearLayoutManager;
  17. import android.support.v7.widget.RecyclerView;
  18. import android.support.v7.widget.Toolbar;
  19. import android.view.GestureDetector;
  20. import android.view.MotionEvent;
  21. import android.view.View;
  22. import android.view.View.OnClickListener;
  23.  
  24. import com.imaadv.leaynik.R;
  25. import com.imaadv.leaynik.api.API;
  26. import com.imaadv.leaynik.api.AlertDialogManager;
  27. import com.imaadv.leaynik.defs.AppConstants;
  28. import com.imaadv.leaynik.defs.DrawerItem;
  29. import com.imaadv.leaynik.test.Test_Home;
  30. import com.imaadv.leaynik.ui.DrawerAdapter;
  31. import com.kobakei.ratethisapp.RateThisApp;
  32.  
  33. import java.util.ArrayList;
  34.  
  35.  
  36. public class Home extends AppCompatActivity {
  37.  
  38. private RecyclerView DrawerList;
  39. private DrawerLayout mDrawerLayout;
  40. private API api;
  41. private Toolbar _toolbar;
  42. private ActionBarDrawerToggle mDrawerToggle;
  43. private String[] nav_items;
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.home_view);
  49. api = new API(this);
  50.  
  51. // library to show rate app after 5days or 7 time app opened
  52. RateThisApp.init(new RateThisApp.Config(5, 7));
  53.  
  54.  
  55. // check if this first time app opened
  56. if (isFirstTime()) openAlert();
  57.  
  58. Resources res = getResources();
  59.  
  60. DrawerList = (RecyclerView) findViewById(R.id.DrawerList);
  61. DrawerList.setLayoutManager(new LinearLayoutManager(this));
  62. _toolbar = (Toolbar) findViewById(R.id.toolbar);
  63.  
  64. setSupportActionBar(_toolbar);
  65. getSupportActionBar().setHomeButtonEnabled(true);
  66. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  67.  
  68. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  69. ArrayList<DrawerItem> Items = new ArrayList<>(9);
  70.  
  71. nav_items = res.getStringArray(R.array.nav_items);
  72. TypedArray nav_icons = res.obtainTypedArray(R.array.nav_icon);
  73.  
  74. for (int i = 0; i < nav_items.length; i++) {
  75. Items.add(new DrawerItem(nav_items[i], nav_icons.getResourceId(i, -1)));
  76. }
  77. nav_icons.recycle();
  78.  
  79. DrawerAdapter adapter = new DrawerAdapter(Home.this, Items);
  80. DrawerList.setAdapter(adapter);
  81.  
  82. _toolbar.setOnClickListener(new OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
  86. mDrawerLayout.closeDrawer(GravityCompat.START);
  87. } else {
  88. mDrawerLayout.openDrawer(GravityCompat.START);
  89. }
  90. }
  91. });
  92.  
  93.  
  94. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, _toolbar, R.string.app_name, R.string.app_name) {
  95.  
  96. @Override
  97. public void onDrawerOpened(View drawerView) {
  98. super.onDrawerOpened(drawerView);
  99. }
  100.  
  101. @Override
  102. public void onDrawerClosed(View drawerView) {
  103. super.onDrawerClosed(drawerView);
  104. }
  105. };
  106.  
  107. mDrawerLayout.setDrawerListener(mDrawerToggle);
  108. mDrawerToggle.setDrawerIndicatorEnabled(true);
  109.  
  110.  
  111. DrawerList.addOnItemTouchListener(new RecycleOntouchListner(this, new ClickListner() {
  112. @Override
  113. public void OnClick(View view, int position) {
  114. SelectItem(position);
  115.  
  116. }
  117. }));
  118.  
  119.  
  120. if (savedInstanceState == null) {
  121. SelectItem(0);
  122. }
  123.  
  124.  
  125.  
  126. }
  127.  
  128. @Override
  129. protected void onStart() {
  130. super.onStart();
  131. RateThisApp.onStart(Home.this);
  132. RateThisApp.showRateDialogIfNeeded(Home.this);
  133. }
  134.  
  135. private void openAlert() {
  136. api.setSetting(AppConstants.ALERT, AppConstants.TRUE);
  137. AlertDialogManager.showAlertDialog(Home.this, getResources().getString(R.string.app_name), getResources().getString(R.string.sos), null, getResources().getString(R.string.ok));
  138. }
  139.  
  140.  
  141. private void Logout() {
  142.  
  143. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  144. Home.this);
  145.  
  146. alertDialogBuilder
  147. .setTitle(getResources().getString(R.string.app_name));
  148. alertDialogBuilder.setMessage(getResources().getString(
  149. R.string.logout_msg));
  150. // set positive button: Yes message
  151. alertDialogBuilder.setPositiveButton(
  152. getResources().getString(R.string.ok),
  153. new DialogInterface.OnClickListener() {
  154. public void onClick(DialogInterface dialog, int id) {
  155. if (api == null) {
  156. api = new API(Home.this);
  157. }
  158. api.clearSettings();
  159. finish();
  160. }
  161. });
  162. alertDialogBuilder.setNegativeButton(
  163. getResources().getString(R.string.cancel),
  164. new DialogInterface.OnClickListener() {
  165.  
  166. @Override
  167. public void onClick(DialogInterface arg0, int arg1) {
  168.  
  169. }
  170. });
  171.  
  172. AlertDialog alertDialog = alertDialogBuilder.create();
  173. // show alert
  174. alertDialog.show();
  175. }
  176.  
  177. public void SelectItem(int position) {
  178.  
  179. Fragment _fragment = null;
  180. switch (position) {
  181. case 0:
  182. _fragment = Test_Home.newInstance();
  183. break;
  184.  
  185. case 1:
  186. _fragment = Diases_Fragment.newInstance();
  187.  
  188. break;
  189.  
  190. case 2:
  191. _fragment = Questions_Fragment.newInstance();
  192. break;
  193.  
  194. case 3:
  195. _fragment = Clinc_Fragment.newInstance();
  196. break;
  197.  
  198. case 4:
  199. _fragment = Reminder_Fragment.newInstance();
  200. break;
  201.  
  202. case 5:
  203. _fragment = ArticalesFragment.newInstance();
  204. break;
  205.  
  206. case 6:
  207. _fragment = Profile_Fragment.newInstance();
  208. break;
  209.  
  210. case 7:
  211. _fragment = Competion_Fragment.newInstance();
  212. break;
  213.  
  214. case 8:
  215. Logout();
  216. break;
  217.  
  218. default:
  219. break;
  220.  
  221.  
  222. }
  223.  
  224. if (_fragment != null) {
  225. FragmentManager fragmentManager = getSupportFragmentManager();
  226. fragmentManager.beginTransaction().replace(R.id.view_content, _fragment).addToBackStack(null).commit();
  227. setTitle(nav_items[position]);
  228. mDrawerLayout.closeDrawer(GravityCompat.START);
  229. }
  230.  
  231. //DrawerList.setSelected(true);
  232.  
  233.  
  234.  
  235. }
  236.  
  237.  
  238. public class RecycleOntouchListner implements RecyclerView.OnItemTouchListener {
  239.  
  240. private GestureDetector mGestureDetector;
  241. private ClickListner mClickListner;
  242.  
  243. public RecycleOntouchListner(Context mContext, ClickListner clickListner) {
  244.  
  245. this.mClickListner = clickListner;
  246.  
  247. mGestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
  248. @Override
  249. public boolean onSingleTapUp(MotionEvent e) {
  250.  
  251. return true;
  252. }
  253. });
  254. }
  255.  
  256.  
  257. @Override
  258. public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
  259.  
  260. View child = rv.findChildViewUnder(e.getX(), e.getY());
  261. if (child != null && mClickListner != null && mGestureDetector.onTouchEvent(e)) {
  262. mClickListner.OnClick(child, rv.getChildAdapterPosition(child));
  263. return false;
  264.  
  265. }
  266. return false;
  267. }
  268.  
  269. @Override
  270. public void onTouchEvent(RecyclerView rv, MotionEvent e) {
  271.  
  272. }
  273.  
  274. @Override
  275. public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
  276.  
  277. }
  278. }
  279.  
  280. public static interface ClickListner {
  281. void OnClick(View view, int position);
  282. }
  283.  
  284. public boolean isFirstTime() {
  285. return !(api.getSetting(AppConstants.ALERT).contentEquals(AppConstants.TRUE));
  286.  
  287. }
  288.  
  289. @Override
  290. protected void onPostCreate(Bundle savedInstanceState) {
  291. super.onPostCreate(savedInstanceState);
  292. // Sync the toggle state after onRestoreInstanceState has occurred.
  293. mDrawerToggle.syncState();
  294. }
  295.  
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement