From d6710a54065d59b8d402886fd71eb3334306a4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Tue, 30 Oct 2018 23:55:21 +0100 Subject: [PATCH] Rewrite activity to fragment --- .../findmytutor/activity/BaseActivity.java | 10 ++- .../findmytutor/activity/ListViewAdapter.java | 5 +- .../activity/UsersListActivity.java | 70 ++++++++++++------- app/src/main/res/layout/users_list.xml | 12 +--- app/src/main/res/layout/users_list_main.xml | 1 + 5 files changed, 61 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java index 7c43a75..58c8e4c 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java @@ -42,6 +42,7 @@ public abstract class BaseActivity private ArrayList stringArrayList; private ListViewAdapter adapter; private SharingFragment sharingFragment; + private Fragment userListFragment; @Override @@ -52,6 +53,7 @@ public abstract class BaseActivity navigationView = findViewById(R.id.navigation); navigationView.setOnNavigationItemSelectedListener(this); sharingFragment = new SharingFragment(); + userListFragment = new UsersListActivity(); } @@ -73,7 +75,7 @@ public abstract class BaseActivity private void setUpNav() { - drawerLayout = (DrawerLayout) findViewById(R.id.activity_container); + drawerLayout = findViewById(R.id.activity_container); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(actionBarDrawerToggle); @@ -165,16 +167,20 @@ public abstract class BaseActivity navigationView.postDelayed(() -> { int itemId = item.getItemId(); + if (itemId == R.id.nav_map) { removeFragment(sharingFragment); + removeFragment(userListFragment); // startActivity(new Intent(this, MapActivity.class)); } else if (itemId == R.id.nav_profile) { // startActivity(new Intent(this, ProfileActivity.class)); setFragment(sharingFragment); + removeFragment(userListFragment); } else if (itemId == R.id.nav_user_list) { removeFragment(sharingFragment); - startActivity(new Intent(this, UsersListActivity.class)); + setFragment(userListFragment); + //startActivity(new Intent(this, UsersListActivity.class)); } //finish(); }, 300); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/ListViewAdapter.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/ListViewAdapter.java index 51592d1..c5bbf12 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/ListViewAdapter.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/ListViewAdapter.java @@ -1,6 +1,7 @@ package com.uam.wmi.findmytutor.activity; import android.app.Activity; +import android.content.Context; import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.View; @@ -21,11 +22,11 @@ import java.util.Objects; public class ListViewAdapter extends ArrayAdapter { - private UsersListActivity activity; + private Context activity; private List friendList; private List searchList; - public ListViewAdapter(UsersListActivity context, int resource, List objects) { + public ListViewAdapter(Context context, int resource, List objects) { super(context, resource, objects); this.activity = context; this.friendList = objects; diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/UsersListActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/UsersListActivity.java index 71415ee..22012a8 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/UsersListActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/UsersListActivity.java @@ -1,10 +1,10 @@ package com.uam.wmi.findmytutor.activity; import android.annotation.SuppressLint; +import android.app.Fragment; import android.graphics.Color; -import android.os.Build; import android.os.Bundle; -import android.support.annotation.RequiresApi; +import android.support.annotation.NonNull; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v7.app.AlertDialog; @@ -14,6 +14,7 @@ import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; @@ -44,7 +45,9 @@ import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; -public class UsersListActivity extends BaseActivity { +import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; + +public class UsersListActivity extends Fragment { private static final String TAG = UsersListActivity.class.getSimpleName(); @BindView(R.id.coordinator_layout) CoordinatorLayout coordinatorLayout; @@ -52,35 +55,43 @@ public class UsersListActivity extends BaseActivity { RecyclerView recyclerView; @BindView(R.id.txt_empty_notes_view) TextView noNotesView; + private UserService userService; private TutorTabApi tutorTabService; private CompositeDisposable disposable = new CompositeDisposable(); private TutorsAdapter mAdapter; private List tutorsList = new ArrayList<>(); - @RequiresApi(api = Build.VERSION_CODES.N) - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.users_list); - ButterKnife.bind(this); + public UsersListActivity() { + } + + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + mAdapter = new TutorsAdapter(getActivity().getApplicationContext(), tutorsList); + View view = inflater.inflate(R.layout.users_list, container, false); + view.setBackgroundColor(getResources().getColor(android.R.color.white)); + return view; + } + + public void onViewCreated(View view, Bundle savedInstanceState) { + + ButterKnife.bind(this, view); userService = ApiClient.getClient(getApplicationContext()) .create(UserService.class); - tutorTabService = ApiClient.getClient(getApplicationContext()) + tutorTabService = ApiClient.getClient(getActivity().getApplicationContext()) .create(TutorTabApi.class); - mAdapter = new TutorsAdapter(this, tutorsList); - RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); + + RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); - recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16)); + recyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, 16)); recyclerView.setAdapter(mAdapter); fetchAllTutors(); - recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, + recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() { @Override public void onClick(View view, final int position) { @@ -93,11 +104,12 @@ public class UsersListActivity extends BaseActivity { })); } + private void showNoteDialog(final boolean shouldUpdate, final User user, final int position) { - LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext()); + LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getActivity().getApplicationContext()); View view = layoutInflaterAndroid.inflate(R.layout.note_dialog, null); - AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(this); + AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(getActivity().getApplicationContext()); alertDialogBuilderUserInput.setView(view); alertDialogBuilderUserInput.setNegativeButton(R.string.cancel, (dialog, id) -> { @@ -128,7 +140,7 @@ public class UsersListActivity extends BaseActivity { userEmail.setText(String.format("%s: %s", getString(R.string.userEmail), tutorTabViewModel.getEmailTutorTab())); userNote.setText(String.format("%s: %s", getString(R.string.userNote), tutorTabViewModel.getNote())); - final ArrayAdapter arrayAdapter = new ArrayAdapter<>(UsersListActivity.this, + final ArrayAdapter arrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_activated_1, dutyHoursList); userDutyHours.setAdapter(arrayAdapter); @@ -172,10 +184,11 @@ public class UsersListActivity extends BaseActivity { .subscribeWith(new DisposableSingleObserver>() { @Override public void onSuccess(List users) { + toggleEmptyNotes(); tutorsList.clear(); tutorsList.addAll(users); mAdapter.notifyDataSetChanged(); - toggleEmptyNotes(); + } @Override @@ -214,21 +227,30 @@ public class UsersListActivity extends BaseActivity { } @Override - protected void onDestroy() { + public void onDestroy() { super.onDestroy(); disposable.dispose(); } - - protected int getContentViewId() { - return R.layout.users_list; + @Override + public void onResume() { + super.onResume(); + fetchAllTutors(); + Log.e(TAG,"onResume"); } + @Override + public void onPause() { + super.onPause(); + Log.e(TAG,"onPause"); - protected int getNavigationMenuItemId() { - return R.id.nav_user_list; } + @Override + public void onStop() { + super.onStop(); + Log.e(TAG,"onStop"); + } public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); diff --git a/app/src/main/res/layout/users_list.xml b/app/src/main/res/layout/users_list.xml index 074de2b..5cc4880 100644 --- a/app/src/main/res/layout/users_list.xml +++ b/app/src/main/res/layout/users_list.xml @@ -1,21 +1,15 @@ - + + diff --git a/app/src/main/res/layout/users_list_main.xml b/app/src/main/res/layout/users_list_main.xml index 4266a83..f8945d4 100644 --- a/app/src/main/res/layout/users_list_main.xml +++ b/app/src/main/res/layout/users_list_main.xml @@ -1,5 +1,6 @@ +