Background task refactor & fetch user profile during login proccess #19
@ -1,9 +1,176 @@
|
||||
package com.uam.wmi.findmytutor.activity;
|
||||
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.animation.TypeEvaluator;
|
||||
import android.animation.ValueAnimator;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.uam.wmi.findmytutor.R;
|
||||
import com.uam.wmi.findmytutor.model.Coordinate;
|
||||
import com.uam.wmi.findmytutor.network.RetrofitClientInstance;
|
||||
import com.uam.wmi.findmytutor.service.CoordinateService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.observers.DisposableSingleObserver;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
|
||||
public class NotificationsActivity extends BaseActivity {
|
||||
|
||||
private MapView mapView;
|
||||
private CoordinateService coordinateService;
|
||||
private CompositeDisposable disposable = new CompositeDisposable();
|
||||
private List<Coordinate> coordinatesList = new ArrayList<>();
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mapInit(savedInstanceState);
|
||||
|
||||
// coordinateService = ApiClient.getClient(getApplicationContext())
|
||||
// .create(CoordinateService.class);
|
||||
final SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
|
||||
final String authToken = sharedPref.getString("authToken",null);
|
||||
|
||||
coordinateService = RetrofitClientInstance.createService(CoordinateService.class,"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiI5YzFkOGM0Ni1jMmY1LTQ2OGItOTRkNS03ZDZhODIyZDc3YjUiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiIwYTRhYzVjYy1mMmJmLTRkZDctODZlOC0wNWZiMmIyZDczNjMiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJUdXRvciIsImV4cCI6MTU0MTYxNTk1NSwiaXNzIjoiaHR0cDovL2ZpbmRteXR1dG9yLmNvbSIsImF1ZCI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20ifQ.aH9g0MhSoKHyHOL0IV06b5WxH5-Ch4gVTd-91aDKe3k");
|
||||
|
||||
fetchTopCoords();
|
||||
}
|
||||
|
||||
private void mapInit(Bundle savedInstanceState) {
|
||||
// Mapbox access token is configured here. This needs to be called either in your application
|
||||
// object or in the same activity which contains the mapview.
|
||||
Mapbox.getInstance(this, getString(R.string.access_token));
|
||||
// This contains the MapView in XML and needs to be called after the access token is configured.
|
||||
// setContentView(R.layout.activity_notifications);
|
||||
|
||||
mapView = (MapView) findViewById(R.id.mapView2);
|
||||
mapView.onCreate(savedInstanceState);
|
||||
mapView.getMapAsync(new OnMapReadyCallback() {
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
|
||||
final Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
|
||||
.position(new LatLng(64.900932, -18.167040)));
|
||||
|
||||
mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
|
||||
@Override
|
||||
public void onMapClick(@NonNull LatLng point) {
|
||||
|
||||
// When the user clicks on the map, we want to animate the marker to that
|
||||
// location.
|
||||
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
|
||||
new LatLngEvaluator(), marker.getPosition(), point);
|
||||
markerAnimator.setDuration(2000);
|
||||
markerAnimator.start();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void fetchTopCoords() {
|
||||
disposable.add(
|
||||
coordinateService.getTopCoordinates()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribeWith(new DisposableSingleObserver<List<Coordinate>>() {
|
||||
@Override
|
||||
public void onSuccess(List<Coordinate> coordsList) {
|
||||
|
||||
Log.d("mapTag", "co?");
|
||||
for (Coordinate element:
|
||||
coordsList) {
|
||||
Log.d("mapTag", element.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("Error",e.toString());
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
mapView.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
mapView.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
mapView.onLowMemory();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mapView.onDestroy();
|
||||
disposable.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
mapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
private static class LatLngEvaluator implements TypeEvaluator<LatLng> {
|
||||
// Method is used to interpolate the marker animation.
|
||||
|
||||
private LatLng latLng = new LatLng();
|
||||
|
||||
@Override
|
||||
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
|
||||
latLng.setLatitude(startValue.getLatitude()
|
||||
+ ((endValue.getLatitude() - startValue.getLatitude()) * fraction));
|
||||
latLng.setLongitude(startValue.getLongitude()
|
||||
+ ((endValue.getLongitude() - startValue.getLongitude()) * fraction));
|
||||
return latLng;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getContentViewId() { return R.layout.activity_notifications; }
|
||||
|
||||
|
@ -0,0 +1,54 @@
|
||||
package com.uam.wmi.findmytutor.network;
|
||||
|
||||
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class RetrofitClientInstance {
|
||||
|
||||
private static final String BASE_URL = "https://s416084.projektstudencki.pl/develop/";
|
||||
|
||||
private static Retrofit.Builder builder
|
||||
= new Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.addConverterFactory(GsonConverterFactory.create());
|
||||
|
||||
private static Retrofit retrofit = builder.build();
|
||||
|
||||
private static OkHttpClient.Builder httpClient
|
||||
= new OkHttpClient.Builder();
|
||||
|
||||
private static HttpLoggingInterceptor logging
|
||||
= new HttpLoggingInterceptor()
|
||||
.setLevel(HttpLoggingInterceptor.Level.BASIC);
|
||||
|
||||
public static <S> S createService(Class<S> serviceClass) {
|
||||
if (!httpClient.interceptors().contains(logging)) {
|
||||
httpClient.addInterceptor(logging);
|
||||
builder.client(httpClient.build());
|
||||
retrofit = builder.build();
|
||||
}
|
||||
return retrofit.create(serviceClass);
|
||||
}
|
||||
|
||||
public static <S> S createService(Class<S> serviceClass, final String token) {
|
||||
if (token != null) {
|
||||
httpClient.interceptors().clear();
|
||||
httpClient.addInterceptor(chain -> {
|
||||
Request original = chain.request();
|
||||
Request.Builder builder1 = original.newBuilder()
|
||||
.header("Authorization", "Bearer " + token);
|
||||
Request request = builder1.build();
|
||||
return chain.proceed(request);
|
||||
});
|
||||
builder.client(httpClient.build());
|
||||
retrofit = builder.build();
|
||||
}
|
||||
return retrofit.create(serviceClass);
|
||||
}
|
||||
}
|
@ -4,8 +4,7 @@ import com.uam.wmi.findmytutor.model.Coordinate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Call;
|
||||
import io.reactivex.Single;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.DELETE;
|
||||
import retrofit2.http.GET;
|
||||
@ -16,31 +15,30 @@ import retrofit2.http.Path;
|
||||
public interface CoordinateService {
|
||||
|
||||
@GET("api/coordinates")
|
||||
Call<List<Coordinate>> getAllCoordinates();
|
||||
Single<List<Coordinate>> getAllCoordinates();
|
||||
|
||||
@GET("api/coordinates/{id}")
|
||||
Call<Coordinate> getCoordinatesById(@Path("id") String id);
|
||||
Single<Coordinate> getCoordinatesById(@Path("id") String id);
|
||||
|
||||
@GET("api/coordinates/user/{userId}")
|
||||
Call<List<Coordinate>> getCoordinatesByUserId(@Path("userId") String userId);
|
||||
Single<List<Coordinate>> getCoordinatesByUserId(@Path("userId") String userId);
|
||||
|
||||
@GET("api/coordinates/userTop/{userId}")
|
||||
Call<List<Coordinate>> getTopCoordinatesByUserId(@Path("userId") String userId);
|
||||
Single<List<Coordinate>> getTopCoordinatesByUserId(@Path("userId") String userId);
|
||||
|
||||
@GET("api/coordinates/top")
|
||||
Call<List<Coordinate>> getTopCoordinates();
|
||||
Single<List<Coordinate>> getTopCoordinates();
|
||||
|
||||
@GET("api/coordinates/top/online")
|
||||
Call<List<Coordinate>> getOnlineCoordinates();
|
||||
Single<List<Coordinate>> getOnlineCoordinates();
|
||||
|
||||
@POST("api/coordinates")
|
||||
Call<ResponseBody> postCoordinate(@Body Coordinate coordinate);
|
||||
Single <Coordinate> postCoordinate(@Body Coordinate coordinate);
|
||||
|
||||
@PUT("api/coordinates/{id}")
|
||||
Call<Coordinate> putCoordinatesById(@Path("id") String id);
|
||||
Single<Coordinate> putCoordinatesById(@Path("id") String id);
|
||||
|
||||
@DELETE("api/coordinates/{id}")
|
||||
Call<Coordinate> deleteCoordinatesById(@Path("id") String id);
|
||||
Single<Coordinate> deleteCoordinatesById(@Path("id") String id);
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".activity.NotificationsActivity">
|
||||
<!--tools:context=".examples.annotations.AnimatedMarkerActivity">-->
|
||||
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@+id/mapView2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:visibility="visible"
|
||||
mapbox:mapbox_cameraTargetLat="52.466799"
|
||||
mapbox:mapbox_cameraTargetLng="16.927002"
|
||||
mapbox:mapbox_cameraZoom="17"
|
||||
mapbox:mapbox_styleUrl="mapbox://styles/domagalsky/cjiyzrqjp05l72rmj6ntvv2n8">
|
||||
</com.mapbox.mapboxsdk.maps.MapView>
|
||||
|
||||
<include
|
||||
android:id="@+id/navigation"
|
||||
@ -15,4 +29,4 @@
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
</RelativeLayout>
|
Loading…
Reference in New Issue
Block a user