Rewrite activity to fragment

This commit is contained in:
Mieszko Wrzeszczyński 2018-10-30 23:55:21 +01:00
parent f5edf67c15
commit d6710a5406
5 changed files with 61 additions and 37 deletions

View File

@ -42,6 +42,7 @@ public abstract class BaseActivity
private ArrayList<String> stringArrayList; private ArrayList<String> stringArrayList;
private ListViewAdapter adapter; private ListViewAdapter adapter;
private SharingFragment sharingFragment; private SharingFragment sharingFragment;
private Fragment userListFragment;
@Override @Override
@ -52,6 +53,7 @@ public abstract class BaseActivity
navigationView = findViewById(R.id.navigation); navigationView = findViewById(R.id.navigation);
navigationView.setOnNavigationItemSelectedListener(this); navigationView.setOnNavigationItemSelectedListener(this);
sharingFragment = new SharingFragment(); sharingFragment = new SharingFragment();
userListFragment = new UsersListActivity();
} }
@ -73,7 +75,7 @@ public abstract class BaseActivity
private void setUpNav() { 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); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle); drawerLayout.addDrawerListener(actionBarDrawerToggle);
@ -165,16 +167,20 @@ public abstract class BaseActivity
navigationView.postDelayed(() -> { navigationView.postDelayed(() -> {
int itemId = item.getItemId(); int itemId = item.getItemId();
if (itemId == R.id.nav_map) { if (itemId == R.id.nav_map) {
removeFragment(sharingFragment); removeFragment(sharingFragment);
removeFragment(userListFragment);
// startActivity(new Intent(this, MapActivity.class)); // startActivity(new Intent(this, MapActivity.class));
} else if (itemId == R.id.nav_profile) { } else if (itemId == R.id.nav_profile) {
// startActivity(new Intent(this, ProfileActivity.class)); // startActivity(new Intent(this, ProfileActivity.class));
setFragment(sharingFragment); setFragment(sharingFragment);
removeFragment(userListFragment);
} else if (itemId == R.id.nav_user_list) { } else if (itemId == R.id.nav_user_list) {
removeFragment(sharingFragment); removeFragment(sharingFragment);
startActivity(new Intent(this, UsersListActivity.class)); setFragment(userListFragment);
//startActivity(new Intent(this, UsersListActivity.class));
} }
//finish(); //finish();
}, 300); }, 300);

View File

@ -1,6 +1,7 @@
package com.uam.wmi.findmytutor.activity; package com.uam.wmi.findmytutor.activity;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -21,11 +22,11 @@ import java.util.Objects;
public class ListViewAdapter extends ArrayAdapter<String> { public class ListViewAdapter extends ArrayAdapter<String> {
private UsersListActivity activity; private Context activity;
private List<String> friendList; private List<String> friendList;
private List<String> searchList; private List<String> searchList;
public ListViewAdapter(UsersListActivity context, int resource, List<String> objects) { public ListViewAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects); super(context, resource, objects);
this.activity = context; this.activity = context;
this.friendList = objects; this.friendList = objects;

View File

@ -1,10 +1,10 @@
package com.uam.wmi.findmytutor.activity; package com.uam.wmi.findmytutor.activity;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Fragment;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build;
import android.os.Bundle; 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.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -14,6 +14,7 @@ import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
@ -44,7 +45,9 @@ import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody; 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(); private static final String TAG = UsersListActivity.class.getSimpleName();
@BindView(R.id.coordinator_layout) @BindView(R.id.coordinator_layout)
CoordinatorLayout coordinatorLayout; CoordinatorLayout coordinatorLayout;
@ -52,35 +55,43 @@ public class UsersListActivity extends BaseActivity {
RecyclerView recyclerView; RecyclerView recyclerView;
@BindView(R.id.txt_empty_notes_view) @BindView(R.id.txt_empty_notes_view)
TextView noNotesView; TextView noNotesView;
private UserService userService; private UserService userService;
private TutorTabApi tutorTabService; private TutorTabApi tutorTabService;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
private TutorsAdapter mAdapter; private TutorsAdapter mAdapter;
private List<User> tutorsList = new ArrayList<>(); private List<User> tutorsList = new ArrayList<>();
@RequiresApi(api = Build.VERSION_CODES.N) public UsersListActivity() {
@Override }
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setContentView(R.layout.users_list); mAdapter = new TutorsAdapter(getActivity().getApplicationContext(), tutorsList);
ButterKnife.bind(this); 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()) userService = ApiClient.getClient(getApplicationContext())
.create(UserService.class); .create(UserService.class);
tutorTabService = ApiClient.getClient(getApplicationContext()) tutorTabService = ApiClient.getClient(getActivity().getApplicationContext())
.create(TutorTabApi.class); .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.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16)); recyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(mAdapter); recyclerView.setAdapter(mAdapter);
fetchAllTutors(); fetchAllTutors();
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(),
recyclerView, new RecyclerTouchListener.ClickListener() { recyclerView, new RecyclerTouchListener.ClickListener() {
@Override @Override
public void onClick(View view, final int position) { 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) { 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); 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.setView(view);
alertDialogBuilderUserInput.setNegativeButton(R.string.cancel, (dialog, id) -> { 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())); userEmail.setText(String.format("%s: %s", getString(R.string.userEmail), tutorTabViewModel.getEmailTutorTab()));
userNote.setText(String.format("%s: %s", getString(R.string.userNote), tutorTabViewModel.getNote())); userNote.setText(String.format("%s: %s", getString(R.string.userNote), tutorTabViewModel.getNote()));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(UsersListActivity.this, final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_activated_1, dutyHoursList); android.R.layout.simple_list_item_activated_1, dutyHoursList);
userDutyHours.setAdapter(arrayAdapter); userDutyHours.setAdapter(arrayAdapter);
@ -172,10 +184,11 @@ public class UsersListActivity extends BaseActivity {
.subscribeWith(new DisposableSingleObserver<List<User>>() { .subscribeWith(new DisposableSingleObserver<List<User>>() {
@Override @Override
public void onSuccess(List<User> users) { public void onSuccess(List<User> users) {
toggleEmptyNotes();
tutorsList.clear(); tutorsList.clear();
tutorsList.addAll(users); tutorsList.addAll(users);
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
toggleEmptyNotes();
} }
@Override @Override
@ -214,21 +227,30 @@ public class UsersListActivity extends BaseActivity {
} }
@Override @Override
protected void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
disposable.dispose(); disposable.dispose();
} }
@Override
protected int getContentViewId() { public void onResume() {
return R.layout.users_list; 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) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);

View File

@ -1,21 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coordinator_layout"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".activity.UsersListActivity"> tools:context=".activity.UsersListActivity">
<!--
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
-->
<include layout="@layout/users_list_main" /> <include layout="@layout/users_list_main" />
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"