Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public NotificationTabs() {
- // Required empty public constructor
- }
- public static NotificationTabs newInstance() {
- NotificationTabs fragment = new NotificationTabs();
- return fragment;
- }
- public static NotificationTabs newInstance(int tab_no) {
- NotificationTabs fragment = new NotificationTabs();
- Bundle args = new Bundle();
- args.putInt("tab_no",tab_no);
- fragment.setArguments(args);
- return fragment;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getArguments() != null) {
- tab_no = getArguments().getInt("tab_no");
- }
- }
- public void addNotificationChild(){
- notificationChildList.add(0,"energy");
- notificationChildList.add(1,"schedular");
- notificationChildList.add(2,"globalOff");
- notificationChildList.add(3,"lock");
- notificationChildList.add(4,"motionDetection");
- notificationChildList.add(5,"Information");
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- addNotificationChild();
- dialog = new SimpleArcDialog(getActivity());
- ArcConfiguration configuration = new ArcConfiguration(getActivity());
- configuration.setLoaderStyle(SimpleArcLoader.STYLE.SIMPLE_ARC);
- configuration.setText("Please wait...");
- configuration.setAnimationSpeedWithIndex(SimpleArcLoader.SPEED_SLOW);
- dialog.setConfiguration(configuration);
- dialog.setCanceledOnTouchOutside(false);
- dialog.setCancelable(false);
- // dialog.show();
- View rootView = inflater.inflate(R.layout.fragment_notification_tabs, container, false);
- return rootView;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
- tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
- setupViewPager(mViewPager);
- mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
- tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
- @Override
- public void onTabSelected(TabLayout.Tab selectedTab) {
- mViewPager.setCurrentItem(selectedTab.getPosition());
- }
- @Override
- public void onTabUnselected(TabLayout.Tab tab) {
- }
- @Override
- public void onTabReselected(TabLayout.Tab tab) {
- }
- });
- tabLayout.setupWithViewPager(mViewPager);
- setupTabText();
- }
- private void setupTabText() {
- TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
- tabOne.setText(" Energy Notifications");
- tabLayout.getTabAt(0).setCustomView(tabOne);
- TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
- tabTwo.setText(" Schedular Notifications ");
- tabLayout.getTabAt(1).setCustomView(tabTwo);
- TextView tabThree = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
- tabThree.setText(" Global Off Notifications ");
- tabLayout.getTabAt(2).setCustomView(tabThree);
- TextView tabFour = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
- tabFour.setText(" Lock Notifications ");
- tabLayout.getTabAt(3).setCustomView(tabFour);
- TextView tabFive = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
- tabFive.setText(" Motion Detection Notifications ");
- tabLayout.getTabAt(4).setCustomView(tabFive);
- TextView tabSix = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_item, null);
- tabSix.setText(" Information Notifications ");
- tabLayout.getTabAt(5).setCustomView(tabSix);
- }
- private void setupViewPager(ViewPager viewPager) {
- PagerAdapter adapter = new PagerAdapter(getActivity().getFragmentManager());
- adapter.addFrag(" Energy ",notificationChildList.get(0));
- adapter.addFrag(" Schedular ",notificationChildList.get(1));
- adapter.addFrag(" Global Off ",notificationChildList.get(2));
- adapter.addFrag(" Lock ",notificationChildList.get(3));
- adapter.addFrag(" Motion Detection ",notificationChildList.get(4));
- adapter.addFrag(" Information ",notificationChildList.get(5));
- viewPager.setAdapter(adapter);
- tabLayout.setTabsFromPagerAdapter(adapter);
- }
- public class PagerAdapter extends FragmentPagerAdapter{
- private final List<String> mFragmentTitleList = new ArrayList<>();
- private final List<String> mFragmentNotificationType = new ArrayList<>();
- public void addFrag(String title,String notificationType) {
- mFragmentTitleList.add(title);
- mFragmentNotificationType.add(notificationType);
- }
- public PagerAdapter(FragmentManager fm) {
- super(fm);
- //this.mNumOfTabs = NumOfTabs;
- }
- @Override
- public TabFragment getItem(int i) {
- if(dialog!= null && dialog.isShowing())
- dialog.dismiss();
- return TabFragment.newInstance(mFragmentNotificationType.get(i));
- }
- @Override
- public CharSequence getPageTitle(int position) {
- return super.getPageTitle(position);
- }
- @Override
- public int getCount() {
- return mFragmentTitleList.size();
- }
- }
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- /* if (context instanceof OnFragmentInteractionListener) {
- mListener = (OnFragmentInteractionListener) context;
- } else {
- throw new RuntimeException(context.toString()
- + " must implement OnFragmentInteractionListener");
- }*/
- }
- @Override
- public void onDetach() {
- super.onDetach();
- mListener = null;
- }
- @Override
- public void onStop() {
- super.onStop();
- }
- @Override
- public void onStart() {
- super.onStart();
- }
- }
- public class TabFragment extends Fragment {
- // TODO: Rename parameter arguments, choose names that match
- // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
- private static final String ARG_PARAM1 = "param1";
- private static final String ARG_PARAM2 = "param2";
- // private Context context;
- private SimpleArcDialog dialog;
- private View view;
- private RecyclerView rvNotification;
- private String TAG = TabFragment.class.getSimpleName();
- private TextView msgTv;
- private FirestoreRecyclerAdapter adapter;
- private boolean isStop = false;
- /* private DatabaseReference localRef = null;
- private List<NotificationDetails> notificationList=null;
- private List<NotificationDetails> finalList = null;
- private ListView rvNotification;
- private String notificationDateTime, notificationContent;
- private UserNotificationAdapter adapterNotification;
- */ private SwipeRefreshLayout swipeRefreshLayout = null;
- private Snackbar offlineModeSnackBar, goOfflineSnackBar;
- private boolean tabFlag = false;
- // TODO: Rename and change types of parameters
- private String mParam1;
- private String mParam2;
- private GridLayoutManager gridLayoutManager;
- private OnFragmentInteractionListener mListener;
- public TabFragment() {
- // Required empty public constructor
- }
- /**
- * Use this factory method to create a new instance of
- * this fragment using the provided parameters.
- *
- * @return A new instance of fragment TabFragment.
- */
- // TODO: Rename and change types and number of parameters
- public static TabFragment newInstance(String arg111) {
- TabFragment fragment = new TabFragment();
- Bundle args = new Bundle();
- args.putString(ARG_PARAM1, arg111);
- // args.putString(ARG_PARAM2, param2);
- fragment.setArguments(args);
- return fragment;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getArguments() != null) {
- mParam1 = getArguments().getString(ARG_PARAM1);
- //mParam2 = getArguments().getString(ARG_PARAM2);
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- view = null;
- try {
- view = inflater.inflate(R.layout.fragment_tab, container, false);
- rvNotification = (RecyclerView) view.findViewById(R.id.notification_rv);
- msgTv = (TextView) view.findViewById(R.id.msg_tv);
- setUpNotificationUI();
- } catch (Exception e) {
- Log.d(TAG, "----- Exception" + e.getMessage());
- }
- return view;
- }
- private void setUpNotificationUI() {
- dialog = new SimpleArcDialog(getActivity());
- ArcConfiguration configuration = new ArcConfiguration(getActivity());
- configuration.setLoaderStyle(SimpleArcLoader.STYLE.SIMPLE_ARC);
- configuration.setText("Please wait...");
- configuration.setAnimationSpeedWithIndex(SimpleArcLoader.SPEED_SLOW);
- dialog.setConfiguration(configuration);
- dialog.setCanceledOnTouchOutside(false);
- dialog.setCancelable(false);
- dialog.show();
- getNotification(mParam1);
- }
- private void getNotification(String type) {
- CollectionReference collectionReference = null;
- Query query = null;
- gridLayoutManager = new GridLayoutManager(getActivity(), 2);
- rvNotification.addItemDecoration(new GridSpacingItemDecoration(8));
- rvNotification.setLayoutManager(gridLayoutManager);
- try {
- if (type.equals("globalOff")) {
- collectionReference = GlobalApplication.FireStore_db.collection("GlobalOffNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("globalOff");
- } else if (type.equals("energy")) {
- collectionReference = GlobalApplication.FireStore_db.collection("EnergyNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("energy");
- } else if (type.equals("schedular")) {
- collectionReference = GlobalApplication.FireStore_db.collection("SchedularNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("schedular");
- } else if (type.equals("lock")) {
- collectionReference = GlobalApplication.FireStore_db.collection("LockNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("lock");
- } else if (type.equals("motionDetection")) {
- collectionReference = GlobalApplication.FireStore_db.collection("MotionNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("motion");
- } else if (type.equals("Information")) {
- collectionReference = GlobalApplication.FireStore_db.collection("InformationNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("information");
- }
- if (collectionReference != null) {
- query = collectionReference.orderBy("date", Query.Direction.DESCENDING);
- }
- // collectionReference = GlobalApplication.FireStore_db.collection("GlobalOffNotification").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("globalOff");
- // query = collectionReference;
- if (query != null) {
- FirestoreRecyclerOptions<Notification> response = new FirestoreRecyclerOptions.Builder<Notification>()
- .setQuery(query, Notification.class)
- .build();
- adapter = new FirestoreRecyclerAdapter<Notification, TabFragment.NotificationViewHolder>(response) {
- @Override
- public void onBindViewHolder(final TabFragment.NotificationViewHolder holder, final int position, final Notification model) {
- try {
- if (dialog != null && dialog.isShowing()) {
- dialog.dismiss();
- }
- holder.tvNotificationDateTime.setText("" + model.getDate());
- holder.tvNotificationText.setText("" + model.getMessage());
- } catch (Exception ee) {
- Log.d(TAG, "------------- Exception : " + ee.getMessage());
- }
- }
- @Override
- public TabFragment.NotificationViewHolder onCreateViewHolder(ViewGroup group, int i) {
- View view = null;
- try {
- view = LayoutInflater.from(group.getContext())
- .inflate(R.layout.user_notification, group, false);
- } catch (Exception e) {
- Log.e("error", e.getMessage());
- }
- return new TabFragment.NotificationViewHolder(view);
- }
- @Override
- public void onDataChanged() {
- if (dialog != null && dialog.isShowing()) {
- dialog.dismiss();
- }
- if (getItemCount() == 0) {
- rvNotification.setVisibility(View.GONE);
- msgTv.setVisibility(View.VISIBLE);
- } else {
- rvNotification.setVisibility(View.VISIBLE);
- msgTv.setVisibility(View.GONE);
- }
- }
- @Override
- public int getItemCount() {
- return super.getItemCount();
- }
- @Override
- public void onError(FirebaseFirestoreException e) {
- Log.e("error", e.getMessage());
- }
- };
- //adapter.startListening();
- // adapter.notifyDataSetChanged();
- rvNotification.setAdapter(adapter);
- }
- } catch (Exception e) {
- Log.d(TAG, "------------- Exception : " + e.getMessage());
- }
- }
- // TODO: Rename method, update argument and hook method into UI event
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- /* if (context instanceof OnFragmentInteractionListener) {
- mListener = (OnFragmentInteractionListener) context;
- } else {
- throw new RuntimeException(context.toString()
- + " must implement OnFragmentInteractionListener");
- }*/
- }
- @Override
- public void onDetach() {
- super.onDetach();
- mListener = null;
- }
- @Override
- public void onStart() {
- super.onStart();
- if (adapter != null) {
- adapter.startListening();
- }
- }
- @Override
- public void onStop() {
- super.onStop();
- if (adapter != null) {
- //isStop = true;
- adapter.stopListening();
- }
- }
- @Override
- public void onActivityCreated(@Nullable Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
- /**
- * This interface must be implemented by activities that contain this
- * fragment to allow an interaction in this fragment to be communicated
- * to the activity and potentially other fragments contained in that
- * activity.
- * <p/>
- * See the Android Training lesson <a href=
- * "http://developer.android.com/training/basics/fragments/communicating.html"
- * >Communicating with Other Fragments</a> for more information.
- */
- public interface OnFragmentInteractionListener {
- // TODO: Update argument type and name
- void onFragmentInteraction(Uri uri);
- }
- public static class NotificationViewHolder extends RecyclerView.ViewHolder {
- public TextView tvNotificationDateTime;
- public TextView tvNotificationText;
- public CardView cvNotification;
- int position;
- public NotificationViewHolder(View v) {
- super(v);
- cvNotification = (CardView) v.findViewById(R.id.card_view_notification);
- tvNotificationDateTime = (TextView) v.findViewById(R.id.tv_notification_date_time);
- tvNotificationText = (TextView) v.findViewById(R.id.tv_notification_content);
- }
- }
- }
Add Comment
Please, Sign In to add comment