mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 22:03:55 +01:00
Migrated location picker module from Java to Kotlin
This commit is contained in:
parent
6f460c72aa
commit
fcec8e5fbd
4 changed files with 539 additions and 573 deletions
|
|
@ -1,14 +1,15 @@
|
|||
package fr.free.nrw.commons.LocationPicker;
|
||||
package fr.free.nrw.commons.LocationPicker
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import fr.free.nrw.commons.CameraPosition
|
||||
import fr.free.nrw.commons.Media
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import fr.free.nrw.commons.CameraPosition;
|
||||
import fr.free.nrw.commons.Media;
|
||||
|
||||
/**
|
||||
* Helper class for starting the activity
|
||||
*/
|
||||
public final class LocationPicker {
|
||||
object LocationPicker {
|
||||
|
||||
/**
|
||||
* Getting camera position from the intent using constants
|
||||
|
|
@ -16,30 +17,26 @@ public final class LocationPicker {
|
|||
* @param data intent
|
||||
* @return CameraPosition
|
||||
*/
|
||||
public static CameraPosition getCameraPosition(final Intent data) {
|
||||
return data.getParcelableExtra(LocationPickerConstants.MAP_CAMERA_POSITION);
|
||||
@JvmStatic
|
||||
fun getCameraPosition(data: Intent): CameraPosition? {
|
||||
return data.getParcelableExtra(LocationPickerConstants.MAP_CAMERA_POSITION)
|
||||
}
|
||||
|
||||
public static class IntentBuilder {
|
||||
class IntentBuilder
|
||||
/**
|
||||
* Creates a new builder that creates an intent to launch the place picker activity.
|
||||
*/() {
|
||||
|
||||
private final Intent intent;
|
||||
|
||||
/**
|
||||
* Creates a new builder that creates an intent to launch the place picker activity.
|
||||
*/
|
||||
public IntentBuilder() {
|
||||
intent = new Intent();
|
||||
}
|
||||
private val intent: Intent = Intent()
|
||||
|
||||
/**
|
||||
* Gets and puts location in intent
|
||||
* @param position CameraPosition
|
||||
* @return LocationPicker.IntentBuilder
|
||||
*/
|
||||
public LocationPicker.IntentBuilder defaultLocation(
|
||||
final CameraPosition position) {
|
||||
intent.putExtra(LocationPickerConstants.MAP_CAMERA_POSITION, position);
|
||||
return this;
|
||||
fun defaultLocation(position: CameraPosition): IntentBuilder {
|
||||
intent.putExtra(LocationPickerConstants.MAP_CAMERA_POSITION, position)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -47,10 +44,9 @@ public final class LocationPicker {
|
|||
* @param activity activity key
|
||||
* @return LocationPicker.IntentBuilder
|
||||
*/
|
||||
public LocationPicker.IntentBuilder activityKey(
|
||||
final String activity) {
|
||||
intent.putExtra(LocationPickerConstants.ACTIVITY_KEY, activity);
|
||||
return this;
|
||||
fun activityKey(activity: String): IntentBuilder {
|
||||
intent.putExtra(LocationPickerConstants.ACTIVITY_KEY, activity)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -58,20 +54,19 @@ public final class LocationPicker {
|
|||
* @param media Media
|
||||
* @return LocationPicker.IntentBuilder
|
||||
*/
|
||||
public LocationPicker.IntentBuilder media(
|
||||
final Media media) {
|
||||
intent.putExtra(LocationPickerConstants.MEDIA, media);
|
||||
return this;
|
||||
}
|
||||
fun media(media: Media): IntentBuilder {
|
||||
intent.putExtra(LocationPickerConstants.MEDIA, media)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets and sets the activity
|
||||
* @param activity Activity
|
||||
* @return Intent
|
||||
*/
|
||||
public Intent build(final Activity activity) {
|
||||
intent.setClass(activity, LocationPickerActivity.class);
|
||||
return intent;
|
||||
fun build(activity: Activity): Intent {
|
||||
intent.setClass(activity, LocationPickerActivity::class.java)
|
||||
return intent
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,20 +1,13 @@
|
|||
package fr.free.nrw.commons.LocationPicker;
|
||||
package fr.free.nrw.commons.LocationPicker
|
||||
|
||||
/**
|
||||
* Constants need for location picking
|
||||
*/
|
||||
public final class LocationPickerConstants {
|
||||
object LocationPickerConstants {
|
||||
|
||||
public static final String ACTIVITY_KEY
|
||||
= "location.picker.activity";
|
||||
const val ACTIVITY_KEY = "location.picker.activity"
|
||||
|
||||
public static final String MAP_CAMERA_POSITION
|
||||
= "location.picker.cameraPosition";
|
||||
const val MAP_CAMERA_POSITION = "location.picker.cameraPosition"
|
||||
|
||||
public static final String MEDIA
|
||||
= "location.picker.media";
|
||||
|
||||
|
||||
private LocationPickerConstants() {
|
||||
}
|
||||
}
|
||||
const val MEDIA = "location.picker.media"
|
||||
}
|
||||
|
|
@ -1,34 +1,25 @@
|
|||
package fr.free.nrw.commons.LocationPicker;
|
||||
package fr.free.nrw.commons.LocationPicker
|
||||
|
||||
import android.app.Application;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import fr.free.nrw.commons.CameraPosition;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import timber.log.Timber;
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import fr.free.nrw.commons.CameraPosition
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Observes live camera position data
|
||||
*/
|
||||
public class LocationPickerViewModel extends AndroidViewModel implements Callback<CameraPosition> {
|
||||
class LocationPickerViewModel(
|
||||
application: Application
|
||||
): AndroidViewModel(application), Callback<CameraPosition> {
|
||||
|
||||
/**
|
||||
* Wrapping CameraPosition with MutableLiveData
|
||||
*/
|
||||
private final MutableLiveData<CameraPosition> result = new MutableLiveData<>();
|
||||
|
||||
/**
|
||||
* Constructor for this class
|
||||
*
|
||||
* @param application Application
|
||||
*/
|
||||
public LocationPickerViewModel(@NonNull final Application application) {
|
||||
super(application);
|
||||
}
|
||||
val result = MutableLiveData<CameraPosition?>()
|
||||
|
||||
/**
|
||||
* Responses on camera position changing
|
||||
|
|
@ -36,28 +27,18 @@ public class LocationPickerViewModel extends AndroidViewModel implements Callbac
|
|||
* @param call Call<CameraPosition>
|
||||
* @param response Response<CameraPosition>
|
||||
*/
|
||||
@Override
|
||||
public void onResponse(final @NotNull Call<CameraPosition> call,
|
||||
final Response<CameraPosition> response) {
|
||||
if (response.body() == null) {
|
||||
result.setValue(null);
|
||||
return;
|
||||
override fun onResponse(
|
||||
call: Call<CameraPosition>,
|
||||
response: Response<CameraPosition>
|
||||
) {
|
||||
if(response.body() == null) {
|
||||
result.value = null
|
||||
return
|
||||
}
|
||||
result.setValue(response.body());
|
||||
result.value = response.body()
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(final @NotNull Call<CameraPosition> call, final @NotNull Throwable t) {
|
||||
Timber.e(t);
|
||||
override fun onFailure(call: Call<CameraPosition>, t: Throwable) {
|
||||
Timber.e(t)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets live CameraPosition
|
||||
*
|
||||
* @return MutableLiveData<CameraPosition>
|
||||
*/
|
||||
public MutableLiveData<CameraPosition> getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue