From 095c13172ca85d4611e9e0e71982062529ba5e2a Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Sun, 30 May 2021 01:00:27 +0200 Subject: [PATCH] share location to chat by button click Signed-off-by: Marcel Hibbe --- .../java/com/nextcloud/talk/api/NcApi.java | 8 +++ .../talk/controllers/ChatController.kt | 2 +- .../talk/controllers/LocationController.kt | 49 ++++++++++++++++++- .../com/nextcloud/talk/utils/ApiUtils.java | 4 ++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApi.java b/app/src/main/java/com/nextcloud/talk/api/NcApi.java index 2b16e13dd..85cb7b887 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -406,4 +406,12 @@ public interface NcApi { @GET Call downloadResizedImage(@Header("Authorization") String authorization, @Url String url); + + @FormUrlEncoded + @POST + Observable sendLocation(@Header("Authorization") String authorization, + @Url String url, + @Field("objectType") String objectType, + @Field("objectId") String objectId, + @Field("metaData") String metaData); } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index bb16795d4..da7a76610 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -822,7 +822,7 @@ class ChatController(args: Bundle) : Log.d(TAG, "showShareLocationScreen") val bundle = Bundle() - // bundle.putBoolean(, true) + bundle.putString(BundleKeys.KEY_ROOM_TOKEN,roomToken) router.pushController( RouterTransaction.with(LocationController(bundle)) .pushChangeHandler(HorizontalChangeHandler()) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/LocationController.kt b/app/src/main/java/com/nextcloud/talk/controllers/LocationController.kt index dd5872ad9..a3be5d8c2 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/LocationController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/LocationController.kt @@ -17,9 +17,18 @@ import androidx.preference.PreferenceManager import autodagger.AutoInjector import butterknife.BindView import com.nextcloud.talk.R +import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.controllers.base.BaseController +import com.nextcloud.talk.models.json.generic.GenericOverall +import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN +import com.nextcloud.talk.utils.database.user.UserUtils import com.nextcloud.talk.utils.preferences.AppPreferences +import io.reactivex.Observer +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers import org.osmdroid.config.Configuration.getInstance import org.osmdroid.tileprovider.tilesource.TileSourceFactory import org.osmdroid.util.GeoPoint @@ -32,6 +41,12 @@ import javax.inject.Inject @AutoInjector(NextcloudTalkApplication::class) class LocationController(args: Bundle) : BaseController(args) { + @Inject + lateinit var ncApi: NcApi + + @Inject + lateinit var userUtils: UserUtils + @Inject @JvmField var appPreferences: AppPreferences? = null @@ -52,9 +67,13 @@ class LocationController(args: Bundle) : BaseController(args) { @JvmField var btnSelectLocation: Button? = null + var roomToken : String? + init { NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context)) + + roomToken = args.getString(KEY_ROOM_TOKEN) } override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View { @@ -70,7 +89,35 @@ class LocationController(args: Bundle) : BaseController(args) { btnSelectLocation?.setOnClickListener { val selectedLat: Double? = map?.mapCenter?.latitude val selectedLon: Double? = map?.mapCenter?.longitude - Toast.makeText(activity, "Lat: $selectedLat Lon: $selectedLon", Toast.LENGTH_LONG).show() + + ncApi.sendLocation( + ApiUtils.getCredentials(userUtils.currentUser?.username, userUtils.currentUser?.token), + ApiUtils.getUrlToSendLocation(userUtils.currentUser?.baseUrl, roomToken), + "geo-location", + "geo:$selectedLat,$selectedLon", + "{\"type\":\"geo-location\",\"id\":\"geo:$selectedLat,$selectedLon\",\"latitude\":\"$selectedLat\"," + + "\"longitude\":\"$selectedLon\",\"name\":\"examplePlace\"}" + ) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(object : Observer { + override fun onSubscribe(d: Disposable) { + } + + override fun onNext(t: GenericOverall) { + Log.d(TAG, "shared location") + router.popCurrentController() + } + + override fun onError(e: Throwable) { + Log.e(TAG, "error when trying to share location", e) + Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show() + router.popCurrentController() + } + + override fun onComplete() { + } + }) } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java index ebb129776..218b68069 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java @@ -389,4 +389,8 @@ public class ApiUtils { public static String getUrlForUserFields(String baseUrl) { return baseUrl + ocsApiVersion + "/cloud/user/fields"; } + + public static String getUrlToSendLocation(String baseUrl, String roomToken) { + return baseUrl + ocsApiVersion + "/apps/spreed/api/v1/chat/" + roomToken + "/share"; + } }