diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/NotificationsActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/NotificationsActivity.java index b7f5e3d..e7baf28 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/NotificationsActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/NotificationsActivity.java @@ -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 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>() { + @Override + public void onSuccess(List 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 { + // 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; } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/network/RetrofitClientInstance.java b/app/src/main/java/com/uam/wmi/findmytutor/network/RetrofitClientInstance.java new file mode 100644 index 0000000..a3e3ee3 --- /dev/null +++ b/app/src/main/java/com/uam/wmi/findmytutor/network/RetrofitClientInstance.java @@ -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 createService(Class serviceClass) { + if (!httpClient.interceptors().contains(logging)) { + httpClient.addInterceptor(logging); + builder.client(httpClient.build()); + retrofit = builder.build(); + } + return retrofit.create(serviceClass); + } + + public static S createService(Class 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); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java index 877bec2..28ecc7b 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java @@ -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> getAllCoordinates(); + Single> getAllCoordinates(); @GET("api/coordinates/{id}") - Call getCoordinatesById(@Path("id") String id); + Single getCoordinatesById(@Path("id") String id); @GET("api/coordinates/user/{userId}") - Call> getCoordinatesByUserId(@Path("userId") String userId); + Single> getCoordinatesByUserId(@Path("userId") String userId); @GET("api/coordinates/userTop/{userId}") - Call> getTopCoordinatesByUserId(@Path("userId") String userId); + Single> getTopCoordinatesByUserId(@Path("userId") String userId); @GET("api/coordinates/top") - Call> getTopCoordinates(); + Single> getTopCoordinates(); @GET("api/coordinates/top/online") - Call> getOnlineCoordinates(); + Single> getOnlineCoordinates(); @POST("api/coordinates") - Call postCoordinate(@Body Coordinate coordinate); + Single postCoordinate(@Body Coordinate coordinate); @PUT("api/coordinates/{id}") - Call putCoordinatesById(@Path("id") String id); + Single putCoordinatesById(@Path("id") String id); @DELETE("api/coordinates/{id}") - Call deleteCoordinatesById(@Path("id") String id); + Single deleteCoordinatesById(@Path("id") String id); } - diff --git a/app/src/main/res/layout/activity_notifications.xml b/app/src/main/res/layout/activity_notifications.xml index 4020763..be82ea7 100644 --- a/app/src/main/res/layout/activity_notifications.xml +++ b/app/src/main/res/layout/activity_notifications.xml @@ -1,10 +1,24 @@ - + + + + - \ No newline at end of file + \ No newline at end of file