Add checkbox online/offline users

This commit is contained in:
Mieszko Wrzeszczyński 2019-01-02 00:17:03 +01:00
parent 598e603dfb
commit 9dd2a62d43
7 changed files with 130 additions and 3 deletions

View File

@ -9,10 +9,15 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -28,8 +33,8 @@ import com.uam.wmi.findmytutor.model.User;
import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.TutorTabApi; import com.uam.wmi.findmytutor.service.TutorTabApi;
import com.uam.wmi.findmytutor.service.UserService; import com.uam.wmi.findmytutor.service.UserService;
import com.uam.wmi.findmytutor.utils.InfoHelperUtils;
import com.uam.wmi.findmytutor.utils.MyDividerItemDecoration; import com.uam.wmi.findmytutor.utils.MyDividerItemDecoration;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RecyclerTouchListener; import com.uam.wmi.findmytutor.utils.RecyclerTouchListener;
import com.uam.wmi.findmytutor.utils.RestApiHelper; import com.uam.wmi.findmytutor.utils.RestApiHelper;
@ -66,6 +71,7 @@ public class UsersListFragment extends Fragment {
private List<User> tutorsList = new ArrayList<>(); private List<User> tutorsList = new ArrayList<>();
private List<User> tutorsFiltered = new ArrayList<>(); private List<User> tutorsFiltered = new ArrayList<>();
private Collator plCollator = Collator.getInstance(Locale.forLanguageTag("pl-PL")); private Collator plCollator = Collator.getInstance(Locale.forLanguageTag("pl-PL"));
private Boolean fetchOnlyOnlineUsers = PrefUtils.getShowOnlyOnlineUsers(getApplicationContext());
public UsersListFragment() { public UsersListFragment() {
} }
@ -78,7 +84,7 @@ public class UsersListFragment extends Fragment {
mAdapter = new TutorsListAdapter(getActivity().getApplicationContext(), tutorsFiltered); mAdapter = new TutorsListAdapter(getActivity().getApplicationContext(), tutorsFiltered);
View view = inflater.inflate(R.layout.users_list, container, false); View view = inflater.inflate(R.layout.users_list, container, false);
view.setBackgroundColor(getResources().getColor(android.R.color.white)); view.setBackgroundColor(getResources().getColor(android.R.color.white));
setHasOptionsMenu(true);
return view; return view;
} }
@ -113,6 +119,50 @@ public class UsersListFragment extends Fragment {
} }
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
try {
menu.findItem(R.id.showOnlineUsersOnly).setChecked(fetchOnlyOnlineUsers);
}
catch(Exception e) {
Log.e(TAG, "onPrepareOptionsMenu error");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(fetchOnlyOnlineUsers);
switch (item.getItemId()){
case R.id.showOnlineUsersOnly:
if(item.isChecked()) {
fetchOnlyOnlineUsers = false;
item.setChecked(false);
}else{
item.setChecked(true);
fetchOnlyOnlineUsers = true;
}
Log.e(TAG, String.valueOf(fetchOnlyOnlineUsers));
PrefUtils.putShowOnlyOnlineUsers(getApplicationContext(), fetchOnlyOnlineUsers);
fetchAllTutors();
break;
}
return false;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.users_luist_menu, menu);
menu.getItem(0).setChecked(fetchOnlyOnlineUsers);
super.onCreateOptionsMenu(menu,inflater);
}
public void searchUser(String textToSearch) { public void searchUser(String textToSearch) {
tutorsFiltered.clear(); tutorsFiltered.clear();
@ -185,7 +235,9 @@ public class UsersListFragment extends Fragment {
private void fetchAllTutors() { private void fetchAllTutors() {
disposable.add( disposable.add(
userService.getAllTutors() (fetchOnlyOnlineUsers ?
userService.getAllOnlineTutors() :
userService.getAllOfflineTutors())
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.map(tutors -> { .map(tutors -> {
@ -234,6 +286,7 @@ public class UsersListFragment extends Fragment {
private void showError(Throwable e) { private void showError(Throwable e) {
String message; String message;
Log.e(TAG, String.valueOf(e));
if (e instanceof HttpException) { if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody(); ResponseBody responseBody = ((HttpException) e).response().errorBody();
@ -251,6 +304,11 @@ public class UsersListFragment extends Fragment {
noNotesView.setVisibility(View.GONE); noNotesView.setVisibility(View.GONE);
} else { } else {
noNotesView.setVisibility(View.VISIBLE); noNotesView.setVisibility(View.VISIBLE);
if(fetchOnlyOnlineUsers)
noNotesView.setText("Currently, there are no \nonline users.");
else{
noNotesView.setText("Currently, there are no offline users.");
}
} }
} }

View File

@ -30,6 +30,12 @@ public interface UserService {
@GET("api/users/tutors") @GET("api/users/tutors")
Single <List<User>> getAllTutors(); Single <List<User>> getAllTutors();
@GET("api/users/tutors/online")
Single <List<User>> getAllOnlineTutors();
@GET("api/users/tutors/offline")
Single <List<User>> getAllOfflineTutors();
@POST("api/users") @POST("api/users")
Completable createUser(@Body User user); Completable createUser(@Body User user);

View File

@ -197,4 +197,15 @@ public class PrefUtils {
return getSharedPreferences(context).getString("current_manual_location_name", null); return getSharedPreferences(context).getString("current_manual_location_name", null);
} }
public static void putShowOnlyOnlineUsers(Context context, Boolean flag) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("show_only_online_users_in_list", flag);
editor.apply();
}
public static Boolean getShowOnlyOnlineUsers(Context context) {
return getSharedPreferences(context).getBoolean("show_only_online_users_in_list", true);
}
} }

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Homw Tab"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>

View File

@ -10,6 +10,7 @@
<include layout="@layout/users_list_main" /> <include layout="@layout/users_list_main" />
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/lato_regular"
tools:context=".activity.UsersListFragment">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/showOnlineUsersOnly"
android:checkable="true"
android:title="Only online users"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="never" />
</menu>