Guest User

Untitled

a guest
Feb 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.32 KB | None | 0 0
  1. public NotificationTabs() {
  2. // Required empty public constructor
  3. }
  4.  
  5. public static NotificationTabs newInstance() {
  6. NotificationTabs fragment = new NotificationTabs();
  7. return fragment;
  8. }
  9.  
  10. public static NotificationTabs newInstance(int tab_no) {
  11. NotificationTabs fragment = new NotificationTabs();
  12. Bundle args = new Bundle();
  13. args.putInt("tab_no",tab_no);
  14. fragment.setArguments(args);
  15. return fragment;
  16. }
  17.  
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. if (getArguments() != null) {
  22. tab_no = getArguments().getInt("tab_no");
  23. }
  24. }
  25.  
  26. public void addNotificationChild(){
  27. notificationChildList.add(0,"energy");
  28. notificationChildList.add(1,"schedular");
  29. notificationChildList.add(2,"globalOff");
  30. notificationChildList.add(3,"lock");
  31. notificationChildList.add(4,"motionDetection");
  32. notificationChildList.add(5,"Information");
  33. }
  34.  
  35. @Override
  36. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  37. Bundle savedInstanceState) {
  38. addNotificationChild();
  39.  
  40. dialog = new SimpleArcDialog(getActivity());
  41. ArcConfiguration configuration = new ArcConfiguration(getActivity());
  42. configuration.setLoaderStyle(SimpleArcLoader.STYLE.SIMPLE_ARC);
  43. configuration.setText("Please wait...");
  44. configuration.setAnimationSpeedWithIndex(SimpleArcLoader.SPEED_SLOW);
  45. dialog.setConfiguration(configuration);
  46. dialog.setCanceledOnTouchOutside(false);
  47. dialog.setCancelable(false);
  48. // dialog.show();
  49.  
  50. View rootView = inflater.inflate(R.layout.fragment_notification_tabs, container, false);
  51.  
  52. return rootView;
  53. }
  54.  
  55.  
  56. @Override
  57. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  58. super.onViewCreated(view, savedInstanceState);
  59.  
  60. mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
  61.  
  62. tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
  63.  
  64. setupViewPager(mViewPager);
  65.  
  66. mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
  67.  
  68. tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  69.  
  70. @Override
  71. public void onTabSelected(TabLayout.Tab selectedTab) {
  72. mViewPager.setCurrentItem(selectedTab.getPosition());
  73. }
  74.  
  75. @Override
  76. public void onTabUnselected(TabLayout.Tab tab) {
  77.  
  78.  
  79. }
  80.  
  81. @Override
  82. public void onTabReselected(TabLayout.Tab tab) {
  83.  
  84. }
  85. });
  86.  
  87. tabLayout.setupWithViewPager(mViewPager);
  88. setupTabText();
  89.  
  90.  
  91. }
  92.  
  93. private void setupTabText() {
  94.  
  95. TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
  96. tabOne.setText(" Energy Notifications");
  97. tabLayout.getTabAt(0).setCustomView(tabOne);
  98.  
  99. TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
  100. tabTwo.setText(" Schedular Notifications ");
  101. tabLayout.getTabAt(1).setCustomView(tabTwo);
  102.  
  103. TextView tabThree = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
  104. tabThree.setText(" Global Off Notifications ");
  105. tabLayout.getTabAt(2).setCustomView(tabThree);
  106.  
  107. TextView tabFour = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
  108. tabFour.setText(" Lock Notifications ");
  109. tabLayout.getTabAt(3).setCustomView(tabFour);
  110.  
  111. TextView tabFive = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
  112. tabFive.setText(" Motion Detection Notifications ");
  113. tabLayout.getTabAt(4).setCustomView(tabFive);
  114.  
  115. TextView tabSix = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
  116. tabSix.setText(" Information Notifications ");
  117. tabLayout.getTabAt(5).setCustomView(tabSix);
  118.  
  119. }
  120.  
  121. private void setupViewPager(ViewPager viewPager) {
  122. PagerAdapter adapter = new PagerAdapter(getActivity().getFragmentManager());
  123. adapter.addFrag(" Energy ",notificationChildList.get(0));
  124. adapter.addFrag(" Schedular ",notificationChildList.get(1));
  125. adapter.addFrag(" Global Off ",notificationChildList.get(2));
  126. adapter.addFrag(" Lock ",notificationChildList.get(3));
  127. adapter.addFrag(" Motion Detection ",notificationChildList.get(4));
  128. adapter.addFrag(" Information ",notificationChildList.get(5));
  129. viewPager.setAdapter(adapter);
  130.  
  131. tabLayout.setTabsFromPagerAdapter(adapter);
  132.  
  133. }
  134.  
  135.  
  136. public class PagerAdapter extends FragmentPagerAdapter{
  137.  
  138. private final List<String> mFragmentTitleList = new ArrayList<>();
  139. private final List<String> mFragmentNotificationType = new ArrayList<>();
  140.  
  141.  
  142. public void addFrag(String title,String notificationType) {
  143.  
  144. mFragmentTitleList.add(title);
  145. mFragmentNotificationType.add(notificationType);
  146.  
  147. }
  148.  
  149.  
  150. public PagerAdapter(FragmentManager fm) {
  151. super(fm);
  152. //this.mNumOfTabs = NumOfTabs;
  153. }
  154.  
  155. @Override
  156. public TabFragment getItem(int i) {
  157.  
  158. if(dialog!= null && dialog.isShowing())
  159. dialog.dismiss();
  160. return TabFragment.newInstance(mFragmentNotificationType.get(i));
  161.  
  162. }
  163.  
  164. @Override
  165. public CharSequence getPageTitle(int position) {
  166. return super.getPageTitle(position);
  167. }
  168.  
  169. @Override
  170. public int getCount() {
  171. return mFragmentTitleList.size();
  172. }
  173. }
  174.  
  175.  
  176.  
  177. // TODO: Rename method, update argument and hook method into UI event
  178. public void onButtonPressed(Uri uri) {
  179. if (mListener != null) {
  180. mListener.onFragmentInteraction(uri);
  181. }
  182. }
  183.  
  184. @Override
  185. public void onAttach(Context context) {
  186. super.onAttach(context);
  187.  
  188. /* if (context instanceof OnFragmentInteractionListener) {
  189. mListener = (OnFragmentInteractionListener) context;
  190. } else {
  191. throw new RuntimeException(context.toString()
  192. + " must implement OnFragmentInteractionListener");
  193. }*/
  194. }
  195.  
  196. @Override
  197. public void onDetach() {
  198. super.onDetach();
  199. mListener = null;
  200. }
  201.  
  202.  
  203. @Override
  204. public void onStop() {
  205. super.onStop();
  206.  
  207. }
  208.  
  209.  
  210. @Override
  211. public void onStart() {
  212. super.onStart();
  213. }
  214. }
  215.  
  216. public class TabFragment extends Fragment {
  217. // TODO: Rename parameter arguments, choose names that match
  218. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  219. private static final String ARG_PARAM1 = "param1";
  220. private static final String ARG_PARAM2 = "param2";
  221. // private Context context;
  222. private SimpleArcDialog dialog;
  223. private View view;
  224. private RecyclerView rvNotification;
  225. private String TAG = TabFragment.class.getSimpleName();
  226. private TextView msgTv;
  227. private FirestoreRecyclerAdapter adapter;
  228. private boolean isStop = false;
  229.  
  230. /* private DatabaseReference localRef = null;
  231. private List<NotificationDetails> notificationList=null;
  232. private List<NotificationDetails> finalList = null;
  233. private ListView rvNotification;
  234. private String notificationDateTime, notificationContent;
  235. private UserNotificationAdapter adapterNotification;
  236. */ private SwipeRefreshLayout swipeRefreshLayout = null;
  237. private Snackbar offlineModeSnackBar, goOfflineSnackBar;
  238.  
  239. private boolean tabFlag = false;
  240.  
  241. // TODO: Rename and change types of parameters
  242. private String mParam1;
  243. private String mParam2;
  244. private GridLayoutManager gridLayoutManager;
  245.  
  246. private OnFragmentInteractionListener mListener;
  247.  
  248. public TabFragment() {
  249. // Required empty public constructor
  250. }
  251.  
  252. /**
  253. * Use this factory method to create a new instance of
  254. * this fragment using the provided parameters.
  255. *
  256. * @return A new instance of fragment TabFragment.
  257. */
  258. // TODO: Rename and change types and number of parameters
  259. public static TabFragment newInstance(String arg111) {
  260. TabFragment fragment = new TabFragment();
  261. Bundle args = new Bundle();
  262. args.putString(ARG_PARAM1, arg111);
  263. // args.putString(ARG_PARAM2, param2);
  264. fragment.setArguments(args);
  265. return fragment;
  266. }
  267.  
  268. @Override
  269. public void onCreate(Bundle savedInstanceState) {
  270. super.onCreate(savedInstanceState);
  271. if (getArguments() != null) {
  272. mParam1 = getArguments().getString(ARG_PARAM1);
  273. //mParam2 = getArguments().getString(ARG_PARAM2);
  274. }
  275. }
  276.  
  277. @Override
  278. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  279. Bundle savedInstanceState) {
  280. // Inflate the layout for this fragment
  281. view = null;
  282. try {
  283. view = inflater.inflate(R.layout.fragment_tab, container, false);
  284. rvNotification = (RecyclerView) view.findViewById(R.id.notification_rv);
  285.  
  286. msgTv = (TextView) view.findViewById(R.id.msg_tv);
  287.  
  288. setUpNotificationUI();
  289.  
  290.  
  291. } catch (Exception e) {
  292. Log.d(TAG, "----- Exception" + e.getMessage());
  293.  
  294. }
  295.  
  296. return view;
  297. }
  298.  
  299.  
  300. private void setUpNotificationUI() {
  301.  
  302. dialog = new SimpleArcDialog(getActivity());
  303. ArcConfiguration configuration = new ArcConfiguration(getActivity());
  304. configuration.setLoaderStyle(SimpleArcLoader.STYLE.SIMPLE_ARC);
  305. configuration.setText("Please wait...");
  306. configuration.setAnimationSpeedWithIndex(SimpleArcLoader.SPEED_SLOW);
  307. dialog.setConfiguration(configuration);
  308. dialog.setCanceledOnTouchOutside(false);
  309. dialog.setCancelable(false);
  310. dialog.show();
  311.  
  312.  
  313. getNotification(mParam1);
  314.  
  315.  
  316. }
  317.  
  318.  
  319. private void getNotification(String type) {
  320. CollectionReference collectionReference = null;
  321. Query query = null;
  322.  
  323. gridLayoutManager = new GridLayoutManager(getActivity(), 2);
  324. rvNotification.addItemDecoration(new GridSpacingItemDecoration(8));
  325. rvNotification.setLayoutManager(gridLayoutManager);
  326.  
  327.  
  328. try {
  329. if (type.equals("globalOff")) {
  330.  
  331. collectionReference = GlobalApplication.FireStore_db.collection("GlobalOffNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("globalOff");
  332.  
  333.  
  334. } else if (type.equals("energy")) {
  335.  
  336. collectionReference = GlobalApplication.FireStore_db.collection("EnergyNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("energy");
  337.  
  338. } else if (type.equals("schedular")) {
  339. collectionReference = GlobalApplication.FireStore_db.collection("SchedularNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("schedular");
  340.  
  341.  
  342. } else if (type.equals("lock")) {
  343.  
  344. collectionReference = GlobalApplication.FireStore_db.collection("LockNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("lock");
  345.  
  346. } else if (type.equals("motionDetection")) {
  347. collectionReference = GlobalApplication.FireStore_db.collection("MotionNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("motion");
  348.  
  349.  
  350. } else if (type.equals("Information")) {
  351.  
  352. collectionReference = GlobalApplication.FireStore_db.collection("InformationNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("information");
  353.  
  354. }
  355.  
  356. if (collectionReference != null) {
  357. query = collectionReference.orderBy("date", Query.Direction.DESCENDING);
  358.  
  359. }
  360.  
  361. // collectionReference = GlobalApplication.FireStore_db.collection("GlobalOffNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("globalOff");
  362. // query = collectionReference;
  363.  
  364.  
  365. if (query != null) {
  366.  
  367. FirestoreRecyclerOptions<Notification> response = new FirestoreRecyclerOptions.Builder<Notification>()
  368. .setQuery(query, Notification.class)
  369. .build();
  370.  
  371. adapter = new FirestoreRecyclerAdapter<Notification, TabFragment.NotificationViewHolder>(response) {
  372. @Override
  373. public void onBindViewHolder(final TabFragment.NotificationViewHolder holder, final int position, final Notification model) {
  374.  
  375. try {
  376.  
  377. if (dialog != null && dialog.isShowing()) {
  378. dialog.dismiss();
  379. }
  380.  
  381. holder.tvNotificationDateTime.setText("" + model.getDate());
  382. holder.tvNotificationText.setText("" + model.getMessage());
  383.  
  384.  
  385. } catch (Exception ee) {
  386.  
  387. Log.d(TAG, "------------- Exception : " + ee.getMessage());
  388. }
  389.  
  390. }
  391.  
  392. @Override
  393. public TabFragment.NotificationViewHolder onCreateViewHolder(ViewGroup group, int i) {
  394. View view = null;
  395. try {
  396. view = LayoutInflater.from(group.getContext())
  397. .inflate(R.layout.user_notification, group, false);
  398.  
  399. } catch (Exception e) {
  400. Log.e("error", e.getMessage());
  401.  
  402. }
  403.  
  404. return new TabFragment.NotificationViewHolder(view);
  405. }
  406.  
  407. @Override
  408. public void onDataChanged() {
  409.  
  410. if (dialog != null && dialog.isShowing()) {
  411. dialog.dismiss();
  412.  
  413. }
  414.  
  415. if (getItemCount() == 0) {
  416.  
  417. rvNotification.setVisibility(View.GONE);
  418. msgTv.setVisibility(View.VISIBLE);
  419. } else {
  420.  
  421. rvNotification.setVisibility(View.VISIBLE);
  422. msgTv.setVisibility(View.GONE);
  423. }
  424. }
  425.  
  426. @Override
  427. public int getItemCount() {
  428. return super.getItemCount();
  429. }
  430.  
  431. @Override
  432. public void onError(FirebaseFirestoreException e) {
  433. Log.e("error", e.getMessage());
  434. }
  435. };
  436.  
  437. //adapter.startListening();
  438. // adapter.notifyDataSetChanged();
  439. rvNotification.setAdapter(adapter);
  440.  
  441. }
  442.  
  443.  
  444. } catch (Exception e) {
  445.  
  446. Log.d(TAG, "------------- Exception : " + e.getMessage());
  447. }
  448.  
  449.  
  450. }
  451.  
  452. // TODO: Rename method, update argument and hook method into UI event
  453. public void onButtonPressed(Uri uri) {
  454. if (mListener != null) {
  455. mListener.onFragmentInteraction(uri);
  456. }
  457. }
  458.  
  459. @Override
  460. public void onAttach(Context context) {
  461. super.onAttach(context);
  462. /* if (context instanceof OnFragmentInteractionListener) {
  463. mListener = (OnFragmentInteractionListener) context;
  464. } else {
  465. throw new RuntimeException(context.toString()
  466. + " must implement OnFragmentInteractionListener");
  467. }*/
  468. }
  469.  
  470. @Override
  471. public void onDetach() {
  472. super.onDetach();
  473. mListener = null;
  474. }
  475.  
  476. @Override
  477. public void onStart() {
  478. super.onStart();
  479.  
  480. if (adapter != null) {
  481. adapter.startListening();
  482. }
  483. }
  484.  
  485. @Override
  486. public void onStop() {
  487. super.onStop();
  488.  
  489. if (adapter != null) {
  490. //isStop = true;
  491. adapter.stopListening();
  492. }
  493. }
  494.  
  495. @Override
  496. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  497. super.onActivityCreated(savedInstanceState);
  498.  
  499.  
  500. }
  501.  
  502.  
  503. /**
  504. * This interface must be implemented by activities that contain this
  505. * fragment to allow an interaction in this fragment to be communicated
  506. * to the activity and potentially other fragments contained in that
  507. * activity.
  508. * <p/>
  509. * See the Android Training lesson <a href=
  510. * "http://developer.android.com/training/basics/fragments/communicating.html"
  511. * >Communicating with Other Fragments</a> for more information.
  512. */
  513. public interface OnFragmentInteractionListener {
  514. // TODO: Update argument type and name
  515. void onFragmentInteraction(Uri uri);
  516. }
  517.  
  518. public static class NotificationViewHolder extends RecyclerView.ViewHolder {
  519.  
  520. public TextView tvNotificationDateTime;
  521. public TextView tvNotificationText;
  522. public CardView cvNotification;
  523. int position;
  524.  
  525. public NotificationViewHolder(View v) {
  526. super(v);
  527. cvNotification = (CardView) v.findViewById(R.id.card_view_notification);
  528. tvNotificationDateTime = (TextView) v.findViewById(R.id.tv_notification_date_time);
  529. tvNotificationText = (TextView) v.findViewById(R.id.tv_notification_content);
  530. }
  531. }
  532. }
Add Comment
Please, Sign In to add comment