mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +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 |  * Helper class for starting the activity | ||||||
|  */ |  */ | ||||||
| public final class LocationPicker { | object LocationPicker { | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Getting camera position from the intent using constants |      * Getting camera position from the intent using constants | ||||||
|  | @ -16,30 +17,26 @@ public final class LocationPicker { | ||||||
|      * @param data intent |      * @param data intent | ||||||
|      * @return CameraPosition |      * @return CameraPosition | ||||||
|      */ |      */ | ||||||
|     public static CameraPosition getCameraPosition(final Intent data) { |     @JvmStatic | ||||||
|         return data.getParcelableExtra(LocationPickerConstants.MAP_CAMERA_POSITION); |     fun getCameraPosition(data: Intent): CameraPosition? { | ||||||
|  |         return data.getParcelableExtra(LocationPickerConstants.MAP_CAMERA_POSITION) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static class IntentBuilder { |     class IntentBuilder | ||||||
| 
 |  | ||||||
|         private final Intent intent; |  | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * Creates a new builder that creates an intent to launch the place picker activity. |      * 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 |          * Gets and puts location in intent | ||||||
|          * @param position CameraPosition |          * @param position CameraPosition | ||||||
|          * @return LocationPicker.IntentBuilder |          * @return LocationPicker.IntentBuilder | ||||||
|          */ |          */ | ||||||
|         public LocationPicker.IntentBuilder defaultLocation( |         fun defaultLocation(position: CameraPosition): IntentBuilder { | ||||||
|             final CameraPosition position) { |             intent.putExtra(LocationPickerConstants.MAP_CAMERA_POSITION, position) | ||||||
|           intent.putExtra(LocationPickerConstants.MAP_CAMERA_POSITION, position); |             return this | ||||||
|           return this; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /** |         /** | ||||||
|  | @ -47,10 +44,9 @@ public final class LocationPicker { | ||||||
|          * @param activity activity key |          * @param activity activity key | ||||||
|          * @return LocationPicker.IntentBuilder |          * @return LocationPicker.IntentBuilder | ||||||
|          */ |          */ | ||||||
|         public LocationPicker.IntentBuilder activityKey( |         fun activityKey(activity: String): IntentBuilder { | ||||||
|             final String activity) { |             intent.putExtra(LocationPickerConstants.ACTIVITY_KEY, activity) | ||||||
|           intent.putExtra(LocationPickerConstants.ACTIVITY_KEY, activity); |             return this | ||||||
|           return this; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /** |         /** | ||||||
|  | @ -58,10 +54,9 @@ public final class LocationPicker { | ||||||
|          * @param media Media |          * @param media Media | ||||||
|          * @return LocationPicker.IntentBuilder |          * @return LocationPicker.IntentBuilder | ||||||
|          */ |          */ | ||||||
|         public LocationPicker.IntentBuilder media( |         fun media(media: Media): IntentBuilder { | ||||||
|                 final Media media) { |             intent.putExtra(LocationPickerConstants.MEDIA, media) | ||||||
|               intent.putExtra(LocationPickerConstants.MEDIA, media); |             return this | ||||||
|               return this; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /** |         /** | ||||||
|  | @ -69,9 +64,9 @@ public final class LocationPicker { | ||||||
|          * @param activity Activity |          * @param activity Activity | ||||||
|          * @return Intent |          * @return Intent | ||||||
|          */ |          */ | ||||||
|        public Intent build(final Activity activity) { |         fun build(activity: Activity): Intent { | ||||||
|           intent.setClass(activity, LocationPickerActivity.class); |             intent.setClass(activity, LocationPickerActivity::class.java) | ||||||
|           return intent; |             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 |  * Constants need for location picking | ||||||
|  */ |  */ | ||||||
| public final class LocationPickerConstants { | object LocationPickerConstants { | ||||||
| 
 | 
 | ||||||
|     public static final String ACTIVITY_KEY |     const val ACTIVITY_KEY = "location.picker.activity" | ||||||
|         = "location.picker.activity"; |  | ||||||
| 
 | 
 | ||||||
|     public static final String MAP_CAMERA_POSITION |     const val MAP_CAMERA_POSITION = "location.picker.cameraPosition" | ||||||
|         = "location.picker.cameraPosition"; |  | ||||||
| 
 | 
 | ||||||
|     public static final String MEDIA |     const val MEDIA = "location.picker.media" | ||||||
|         = "location.picker.media"; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     private LocationPickerConstants() { |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | @ -1,34 +1,25 @@ | ||||||
| package fr.free.nrw.commons.LocationPicker; | package fr.free.nrw.commons.LocationPicker | ||||||
| 
 | 
 | ||||||
| import android.app.Application; | import android.app.Application | ||||||
| import androidx.annotation.NonNull; | import androidx.lifecycle.AndroidViewModel | ||||||
| import androidx.lifecycle.AndroidViewModel; | import androidx.lifecycle.MutableLiveData | ||||||
| import androidx.lifecycle.MutableLiveData; | import fr.free.nrw.commons.CameraPosition | ||||||
| import fr.free.nrw.commons.CameraPosition; | import retrofit2.Call | ||||||
| import org.jetbrains.annotations.NotNull; | import retrofit2.Callback | ||||||
| import retrofit2.Call; | import retrofit2.Response | ||||||
| import retrofit2.Callback; | import timber.log.Timber | ||||||
| import retrofit2.Response; |  | ||||||
| import timber.log.Timber; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Observes live camera position data |  * 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 |      * Wrapping CameraPosition with MutableLiveData | ||||||
|      */ |      */ | ||||||
|     private final MutableLiveData<CameraPosition> result = new MutableLiveData<>(); |     val result = MutableLiveData<CameraPosition?>() | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Constructor for this class |  | ||||||
|      * |  | ||||||
|      * @param application Application |  | ||||||
|      */ |  | ||||||
|     public LocationPickerViewModel(@NonNull final Application application) { |  | ||||||
|         super(application); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Responses on camera position changing |      * Responses on camera position changing | ||||||
|  | @ -36,28 +27,18 @@ public class LocationPickerViewModel extends AndroidViewModel implements Callbac | ||||||
|      * @param call     Call<CameraPosition> |      * @param call     Call<CameraPosition> | ||||||
|      * @param response Response<CameraPosition> |      * @param response Response<CameraPosition> | ||||||
|      */ |      */ | ||||||
|     @Override |     override fun onResponse( | ||||||
|     public void onResponse(final @NotNull Call<CameraPosition> call, |         call: Call<CameraPosition>, | ||||||
|         final Response<CameraPosition> response) { |         response: Response<CameraPosition> | ||||||
|         if (response.body() == null) { |     ) { | ||||||
|             result.setValue(null); |         if(response.body() == null) { | ||||||
|             return; |             result.value = null | ||||||
|  |             return | ||||||
|         } |         } | ||||||
|         result.setValue(response.body()); |         result.value = response.body() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     override fun onFailure(call: Call<CameraPosition>, t: Throwable) { | ||||||
|     public void onFailure(final @NotNull Call<CameraPosition> call, final @NotNull Throwable t) { |         Timber.e(t) | ||||||
|         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
	
	 Saifuddin
						Saifuddin