Fix retrofit rxjava

This commit is contained in:
Mieszko Wrzeszczyński 2018-11-27 11:45:50 +01:00
parent 2b19229552
commit 6dbe50761a
2 changed files with 111 additions and 113 deletions

View File

@ -1,16 +1,10 @@
package com.uam.wmi.findmytutor.activity; package com.uam.wmi.findmytutor.activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -26,137 +20,141 @@ import com.uam.wmi.findmytutor.service.UserService;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper; import com.uam.wmi.findmytutor.utils.RestApiHelper;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Response; import retrofit2.Response;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class TutorTab extends AppCompatActivity { public class TutorTab extends AppCompatActivity {
private TutorTabApi tutorTabService; private TutorTabApi tutorTabService;
private UserService userService; private UserService userService;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
private TextView userName; private TextView userName;
private TextView userDutyHours; private TextView userDutyHours;
private TextView userNote; private TextView userNote;
private TextView userRoom; private TextView userRoom;
private TextView userEmail; private TextView userEmail;
private TextView department; private TextView department;
private Button saveButon; private Button saveButon;
@Override
protected void onCreate(Bundle savedInstanceState) { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tutorTabService = ApiClient.getClient(getApplicationContext())
.create(TutorTabApi.class);
userService = ApiClient.getClient(getApplicationContext())
.create(UserService.class);
setContentView(R.layout.content_tutor_tab);
TextView userName = findViewById(R.id.userName);
TextView userDutyHours = findViewById(R.id.userDutyHours);
TextView userNote = findViewById(R.id.userNote);
TextView userRoom = findViewById(R.id.userRoom);
TextView userEmail = findViewById(R.id.userEmail);
TextView department = findViewById(R.id.userDepartment);
Button saveButon = findViewById(R.id.saveButon);
userName.setText(String.format("%s %s", PrefUtils.getUserFirstName(getApplicationContext()), PrefUtils.getUserLastName(getApplicationContext())));
disposable.add(
tutorTabService.apiUsersTutorTabByTutorIdGet(PrefUtils.getUserId(getApplicationContext()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<TutorTabViewModel>() {
@Override
public void onSuccess(TutorTabViewModel tutorTabViewModel) {
List<String> dutyHoursList = Stream.of(tutorTabViewModel.getDutyHours())
.map(DutyHourViewModel::getSummary).toList();
Log.d("TUTORTAB", Arrays.toString(dutyHoursList.toArray()));
userRoom.setText(String.format("%s: %s", getString(R.string.userRoom), tutorTabViewModel.getRoom()));
userEmail.setText(String.format("%s: %s", getString(R.string.userEmail), tutorTabViewModel.getEmailTutorTab()));
if (!tutorTabViewModel.getNote().equals("")) {
userNote.setText(String.format("%s", tutorTabViewModel.getNote()));
}
userDutyHours.setText(String.format("%s: %s", getString(R.string.userDutyHoursHeader), Arrays.toString(dutyHoursList.toArray())));
super.onCreate(savedInstanceState);
tutorTabService = ApiClient.getClient(getApplicationContext())
.create(TutorTabApi.class);
userService = ApiClient.getClient(getApplicationContext())
.create(UserService.class);
setContentView(R.layout.content_tutor_tab);
TextView userName = findViewById(R.id.userName);
TextView userDutyHours = findViewById(R.id.userDutyHours);
TextView userNote = findViewById(R.id.userNote);
TextView userRoom = findViewById(R.id.userRoom);
TextView userEmail = findViewById(R.id.userEmail);
TextView department = findViewById(R.id.userDepartment);
Button saveButon = findViewById(R.id.saveButon);
userName.setText(String.format("%s %s", PrefUtils.getUserFirstName(getApplicationContext()), PrefUtils.getUserLastName(getApplicationContext())));
disposable.add(
tutorTabService.apiUsersTutorTabByTutorIdGet(PrefUtils.getUserId(getApplicationContext()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<TutorTabViewModel>() {
@Override
public void onSuccess(TutorTabViewModel tutorTabViewModel) {
List<String> dutyHoursList = Stream.of(tutorTabViewModel.getDutyHours())
.map(DutyHourViewModel::getSummary).toList();
Log.d("TUTORTAB",Arrays.toString(dutyHoursList.toArray()));
userRoom.setText(String.format("%s: %s", getString(R.string.userRoom), tutorTabViewModel.getRoom()));
userEmail.setText(String.format("%s: %s", getString(R.string.userEmail), tutorTabViewModel.getEmailTutorTab()));
if (!tutorTabViewModel.getNote().equals("")) {
userNote.setText(String.format("%s", tutorTabViewModel.getNote()));
} }
userDutyHours.setText(String.format("%s: %s", getString(R.string.userDutyHoursHeader),Arrays.toString(dutyHoursList.toArray())));
} @Override
public void onError(Throwable e) {
@Override showError(e);
public void onError(Throwable e) { }
showError(e); }));
} disposable.add(
}));
disposable.add(
// userService.getUserById(PrefUtils.getUserId(getApplicationContext())) // userService.getUserById(PrefUtils.getUserId(getApplicationContext()))
userService.getUserById(PrefUtils.getUserId(getApplicationContext())) userService.getUserById(PrefUtils.getUserId(getApplicationContext()))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver <User>() { .subscribeWith(new DisposableSingleObserver<User>() {
@Override @Override
public void onSuccess(User user) { public void onSuccess(User user) {
department.setText(user.getDepartment()); department.setText(user.getDepartment());
Log.d("TUTORTAB",(user.getDepartment())); Log.d("TUTORTAB", (user.getDepartment()));
} }
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
showError(e); showError(e);
} }
})); }));
// setUpSaveListener(); // setUpSaveListener();
} }
/* private void setUpSaveListener(){
saveButon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TutorTabViewModel newTab = new TutorTabViewModel();
disposable.add(tutorTabService.apiUsersTutorTabByTutorIdPut(PrefUtils.getUserId(getApplicationContext()),newTab)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, this::handleError));
}
});
}*/
private void handleResponse(Response<Void> resp) {
Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.modal_feedback_thankyou), Toast.LENGTH_SHORT).show();
}
private void handleError(Throwable error) { private void setUpSaveListener() {
if (error instanceof HttpException) { saveButon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TutorTabViewModel newTab = new TutorTabViewModel();
putUserTab(newTab);
}
});
}
ResponseBody responseBody = ((HttpException) error).response().errorBody(); private void putUserTab(TutorTabViewModel tutorTabViewModel) {
Toast.makeText(getApplicationContext(), disposable.add(tutorTabService.apiUsersTutorTabByTutorIdPut(PrefUtils.getUserId(getApplicationContext()), tutorTabViewModel)
RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, this::handleError));
}
} else { private void handleResponse(TutorTabViewModel tutorTabViewModel) {
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.modal_feedback_thankyou), Toast.LENGTH_SHORT).show();
"Network error " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("FEEDBACK",error.getMessage());
}
}
private void showError(Throwable e) {
String message;
if (e instanceof HttpException) { }
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
}else{ private void handleError(Throwable error) {
message = "Network Error !"; if (error instanceof HttpException) {
}
ResponseBody responseBody = ((HttpException) error).response().errorBody();
Toast.makeText(getApplicationContext(),
RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Network error " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("FEEDBACK", error.getMessage());
}
}
private void showError(Throwable e) {
String message;
if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
} else {
message = "Network Error !";
}
// Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG) // Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
// .show(); // .show();
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
} }
} }

View File

@ -51,7 +51,7 @@ public interface TutorTabApi {
"Content-Type:application/json" "Content-Type:application/json"
}) })
@PUT("api/users/tutorTab/{tutorId}") @PUT("api/users/tutorTab/{tutorId}")
Observable<Void> apiUsersTutorTabByTutorIdPut( Observable<TutorTabViewModel> apiUsersTutorTabByTutorIdPut(
@retrofit2.http.Path("tutorId") String tutorId, @retrofit2.http.Body TutorTabViewModel tutorTab @retrofit2.http.Path("tutorId") String tutorId, @retrofit2.http.Body TutorTabViewModel tutorTab
); );