Skip to content

Commit ab90e6d

Browse files
committed
Convert Place to kotlin
1 parent fb44237 commit ab90e6d

25 files changed

+487
-677
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ dependencies {
252252
implementation(libs.androidx.material3)
253253
implementation(libs.androidx.foundation)
254254
implementation(libs.androidx.foundation.layout)
255+
implementation(libs.androidx.annotation)
255256
androidTestImplementation(platform(libs.androidx.compose.bom))
256257
androidTestImplementation(libs.androidx.ui.test.junit4)
257258
debugImplementation(libs.androidx.ui.tooling)

app/src/main/java/fr/free/nrw/commons/BaseMarker.kt

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,29 @@ import android.graphics.Bitmap
55
import android.graphics.Canvas
66
import android.graphics.drawable.BitmapDrawable
77
import android.graphics.drawable.Drawable
8+
import androidx.appcompat.content.res.AppCompatResources
89
import fr.free.nrw.commons.location.LatLng
910
import fr.free.nrw.commons.nearby.Place
11+
import androidx.core.graphics.createBitmap
1012

1113
class BaseMarker {
12-
private var _position: LatLng = LatLng(0.0, 0.0, 0f)
13-
private var _title: String = ""
14-
private var _place: Place = Place()
15-
private var _icon: Bitmap? = null
14+
var position: LatLng = LatLng(0.0, 0.0, 0f)
15+
var title: String = ""
16+
var icon: Bitmap? = null
17+
var place: Place = Place()
1618

17-
var position: LatLng
18-
get() = _position
19-
set(value) {
20-
_position = value
21-
}
22-
var title: String
23-
get() = _title
24-
set(value) {
25-
_title = value
26-
}
27-
28-
var place: Place
29-
get() = _place
30-
set(value) {
31-
_place = value
32-
}
33-
var icon: Bitmap?
34-
get() = _icon
35-
set(value) {
36-
_icon = value
37-
}
38-
39-
constructor() {
40-
}
41-
42-
fun fromResource(
43-
context: Context,
44-
drawableResId: Int,
45-
) {
46-
val drawable: Drawable = context.resources.getDrawable(drawableResId)
47-
icon =
48-
if (drawable is BitmapDrawable) {
49-
drawable.bitmap
50-
} else {
51-
val bitmap =
52-
Bitmap.createBitmap(
53-
drawable.intrinsicWidth,
54-
drawable.intrinsicHeight,
55-
Bitmap.Config.ARGB_8888,
56-
)
19+
fun fromResource(context: Context, drawableResId: Int) {
20+
val drawable: Drawable? = AppCompatResources.getDrawable(context, drawableResId)
21+
icon = when (drawable) {
22+
null -> null
23+
is BitmapDrawable -> drawable.bitmap
24+
else -> {
25+
val bitmap = createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight)
5726
val canvas = Canvas(bitmap)
5827
drawable.setBounds(0, 0, canvas.width, canvas.height)
5928
drawable.draw(canvas)
6029
bitmap
6130
}
31+
}
6232
}
6333
}

app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ abstract class BookmarkLocationsDao {
4343
* @return `true` if added, `false` if removed.
4444
*/
4545
suspend fun updateBookmarkLocation(bookmarkLocation: Place): Boolean {
46-
val exists = findBookmarkLocation(bookmarkLocation.name)
46+
val exists = findBookmarkLocation(bookmarkLocation.name!!)
4747

4848
if (exists) {
4949
deleteBookmarkLocation(bookmarkLocation.toBookmarksLocations())

app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarksLocations.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,34 @@ fun BookmarksLocations.toPlace(): Place {
3333
1F
3434
)
3535

36-
val builder = Sitelinks.Builder().apply {
37-
setWikipediaLink(locationWikipediaLink)
38-
setWikidataLink(locationWikidataLink)
39-
setCommonsLink(locationCommonsLink)
40-
}
41-
4236
return Place(
4337
locationLanguage,
4438
locationName,
4539
Label.fromText(locationLabelText),
4640
locationDescription,
4741
location,
4842
locationCategory,
49-
builder.build(),
43+
Sitelinks(locationWikipediaLink, locationCommonsLink, locationWikidataLink),
5044
locationPic,
5145
locationExists
5246
)
5347
}
5448

5549
fun Place.toBookmarksLocations(): BookmarksLocations {
5650
return BookmarksLocations(
57-
locationName = name,
58-
locationLanguage = language,
59-
locationDescription = longDescription,
60-
locationCategory = category,
61-
locationLat = location.latitude,
62-
locationLong = location.longitude,
51+
locationName = name!!,
52+
locationLanguage = language!!,
53+
locationDescription = longDescription!!,
54+
locationCategory = category!!,
55+
locationLat = location!!.latitude,
56+
locationLong = location!!.longitude,
6357
locationLabelText = label?.text ?: "",
6458
locationLabelIcon = label?.icon,
65-
locationImageUrl = pic,
66-
locationWikipediaLink = siteLinks.getWikipediaLink().toString(),
67-
locationWikidataLink = siteLinks.getWikidataLink().toString(),
68-
locationCommonsLink = siteLinks.getCommonsLink().toString(),
69-
locationPic = pic,
70-
locationExists = exists
59+
locationImageUrl = pic!!,
60+
locationWikipediaLink = siteLinks?.wikipediaLink ?: "",
61+
locationWikidataLink = siteLinks?.wikidataLink ?: "",
62+
locationCommonsLink = siteLinks?.commonsLink ?: "",
63+
locationPic = pic!!,
64+
locationExists = exists!!
7165
)
72-
}
66+
}

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
658658
var closestNearbyPlace: Place? = null
659659
// Find the first nearby place that has no image and exists
660660
for (place in nearbyPlacesInfo.placeList) {
661-
if (place.pic == "" && place.exists) {
661+
if (place.pic == "" && place.exists == true) {
662662
closestNearbyPlace = place
663663
break
664664
}
@@ -668,8 +668,8 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
668668
binding!!.cardViewNearby.visibility = View.GONE
669669
} else {
670670
val distance = formatDistanceBetween(currentLatLng, closestNearbyPlace.location)
671-
closestNearbyPlace.setDistance(distance)
672-
direction = computeBearing(currentLatLng!!, closestNearbyPlace.location).toFloat()
671+
closestNearbyPlace.distance = distance
672+
direction = computeBearing(currentLatLng!!, closestNearbyPlace.location!!).toFloat()
673673
binding!!.cardViewNearby.updateContent(closestNearbyPlace)
674674
}
675675
} else {

app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapController.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ class ExploreMapController @Inject constructor(
163163
for (explorePlace in placeList) {
164164
val baseMarker = BaseMarker()
165165
val distance = formatDistanceBetween(currentLatLng, explorePlace.location)
166-
explorePlace.setDistance(distance)
166+
explorePlace.distance = distance
167167

168168
baseMarker.title =
169-
explorePlace.name.substring(5, explorePlace.name.lastIndexOf("."))
169+
explorePlace.name!!.substring(5, explorePlace.name!!.lastIndexOf("."))
170170
baseMarker.position = LatLng(
171-
explorePlace.location.latitude,
172-
explorePlace.location.longitude, 0f
171+
explorePlace.location!!.latitude,
172+
explorePlace.location!!.longitude, 0f
173173
)
174174
baseMarker.place = explorePlace
175175

app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,12 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
648648
*/
649649
private fun passInfoToSheet(place: Place) {
650650
binding!!.bottomSheetDetailsBinding.directionsButton.setOnClickListener {
651-
handleGeoCoordinates(requireActivity(), place.getLocation(), binding!!.mapView.zoomLevelDouble)
651+
handleGeoCoordinates(requireActivity(), place.location!!, binding!!.mapView.zoomLevelDouble)
652652
}
653653

654654
binding!!.bottomSheetDetailsBinding.commonsButton.visibility = if (place.hasCommonsLink()) View.VISIBLE else View.GONE
655655
binding!!.bottomSheetDetailsBinding.commonsButton.setOnClickListener {
656-
handleWebUrl(requireContext(), place.siteLinks.getCommonsLink()!!)
656+
handleWebUrl(requireContext(), place.siteLinks!!.commonsUri!!)
657657
}
658658

659659
var index = 0
@@ -666,15 +666,14 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
666666
}
667667
index++
668668
}
669-
binding!!.bottomSheetDetailsBinding.title.text = place.name.substring(5, place.name.lastIndexOf("."))
669+
binding!!.bottomSheetDetailsBinding.title.text = place.name!!.substring(5, place.name!!.lastIndexOf("."))
670670
binding!!.bottomSheetDetailsBinding.category.text = place.distance
671671
// Remove label since it is double information
672-
var descriptionText = place.longDescription
673-
.replace(place.getName() + " (", "")
672+
var descriptionText = place.longDescription?.replace(place.name + " (", "")
674673
descriptionText = (if (descriptionText == place.longDescription)
675674
descriptionText
676675
else
677-
descriptionText.replaceFirst(".$".toRegex(), ""))
676+
descriptionText?.replaceFirst(".$".toRegex(), ""))
678677
// Set the short description after we remove place name from long description
679678
binding!!.bottomSheetDetailsBinding.description.text = descriptionText
680679
}
@@ -735,8 +734,8 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
735734
val items = mutableListOf<OverlayItem?>()
736735
val d: Drawable = nearbyBaseMarker.icon!!.toDrawable(resources)
737736
val point = GeoPoint(
738-
nearbyBaseMarker.place.location.latitude,
739-
nearbyBaseMarker.place.location.longitude
737+
nearbyBaseMarker.place.location!!.latitude,
738+
nearbyBaseMarker.place.location!!.longitude
740739
)
741740

742741
val markerMedia = getMediaFromImageURL(nearbyBaseMarker.place.pic)
@@ -752,7 +751,7 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
752751

753752
var title = nearbyBaseMarker.place.name
754753
// Remove "File:" if present at start
755-
if (title.startsWith("File:")) {
754+
if (title!!.startsWith("File:")) {
756755
title = title.substring(5)
757756
}
758757
// Remove extensions like .jpg, .jpeg, .png, .svg (case insensitive)
@@ -870,7 +869,7 @@ class ExploreMapFragment : CommonsDaggerSupportFragment(), ExploreMapContract.Vi
870869
*/
871870
private fun removeMarker(nearbyBaseMarker: BaseMarker?) {
872871
if (nearbyBaseMarker == null ||
873-
nearbyBaseMarker.place.getName() == null ||
872+
nearbyBaseMarker.place.name == null ||
874873
baseMarkerOverlayMap == null ||
875874
!baseMarkerOverlayMap!!.containsKey(nearbyBaseMarker)) {
876875
return

app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.kt

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,29 @@ class NearbyController @Inject constructor(
4747

4848
if (null != places && places.size > 0) {
4949
val boundaryCoordinates = arrayOf<LatLng>(
50-
places.get(0).location, // south
51-
places.get(0).location, // north
52-
places.get(0).location, // west
53-
places.get(0).location
50+
places.get(0).location!!, // south
51+
places.get(0).location!!, // north
52+
places.get(0).location!!, // west
53+
places.get(0).location!!
5454
) // east, init with a random location
5555

5656
if (currentLatLng != null) {
5757
Timber.d("Sorting places by distance...")
5858
val distances: MutableMap<Place, Double> = mutableMapOf()
5959
for (place in places) {
60-
distances.put(place, computeDistanceBetween(place.location, currentLatLng))
60+
distances.put(place, computeDistanceBetween(place.location!!, currentLatLng))
6161
// Find boundaries with basic find max approach
62-
if (place.location.latitude < boundaryCoordinates[0].latitude) {
63-
boundaryCoordinates[0] = place.location
62+
if (place.location!!.latitude < boundaryCoordinates[0].latitude) {
63+
boundaryCoordinates[0] = place.location!!
6464
}
65-
if (place.location.latitude > boundaryCoordinates[1].latitude) {
66-
boundaryCoordinates[1] = place.location
65+
if (place.location!!.latitude > boundaryCoordinates[1].latitude) {
66+
boundaryCoordinates[1] = place.location!!
6767
}
68-
if (place.location.longitude < boundaryCoordinates[2].longitude) {
69-
boundaryCoordinates[2] = place.location
68+
if (place.location!!.longitude < boundaryCoordinates[2].longitude) {
69+
boundaryCoordinates[2] = place.location!!
7070
}
71-
if (place.location.longitude > boundaryCoordinates[3].longitude) {
72-
boundaryCoordinates[3] = place.location
71+
if (place.location!!.longitude > boundaryCoordinates[3].longitude) {
72+
boundaryCoordinates[3] = place.location!!
7373
}
7474
}
7575
Collections.sort(
@@ -174,29 +174,29 @@ class NearbyController @Inject constructor(
174174

175175
if (null != places && places.size > 0) {
176176
val boundaryCoordinates = arrayOf<LatLng>(
177-
places.get(0).location, // south
178-
places.get(0).location, // north
179-
places.get(0).location, // west
180-
places.get(0).location
177+
places.get(0).location!!, // south
178+
places.get(0).location!!, // north
179+
places.get(0).location!!, // west
180+
places.get(0).location!!
181181
) // east, init with a random location
182182

183183
if (currentLatLng != null) {
184184
Timber.d("Sorting places by distance...")
185185
val distances: MutableMap<Place, Double> = mutableMapOf()
186186
for (place in places) {
187-
distances.put(place, computeDistanceBetween(place.location, currentLatLng))
187+
distances.put(place, computeDistanceBetween(place.location!!, currentLatLng))
188188
// Find boundaries with basic find max approach
189-
if (place.location.latitude < boundaryCoordinates[0].latitude) {
190-
boundaryCoordinates[0] = place.location
189+
if (place.location!!.latitude < boundaryCoordinates[0].latitude) {
190+
boundaryCoordinates[0] = place.location!!
191191
}
192-
if (place.location.latitude > boundaryCoordinates[1].latitude) {
193-
boundaryCoordinates[1] = place.location
192+
if (place.location!!.latitude > boundaryCoordinates[1].latitude) {
193+
boundaryCoordinates[1] = place.location!!
194194
}
195-
if (place.location.longitude < boundaryCoordinates[2].longitude) {
196-
boundaryCoordinates[2] = place.location
195+
if (place.location!!.longitude < boundaryCoordinates[2].longitude) {
196+
boundaryCoordinates[2] = place.location!!
197197
}
198-
if (place.location.longitude > boundaryCoordinates[3].longitude) {
199-
boundaryCoordinates[3] = place.location
198+
if (place.location!!.longitude > boundaryCoordinates[3].longitude) {
199+
boundaryCoordinates[3] = place.location!!
200200
}
201201
}
202202
Collections.sort<Place?>(
@@ -301,11 +301,11 @@ class NearbyController @Inject constructor(
301301
for (place in placeList) {
302302
val baseMarker = BaseMarker()
303303
val distance = formatDistanceBetween(currentLatLng, place.location)
304-
place.setDistance(distance)
305-
baseMarker.title = place.name
304+
place.distance = distance
305+
baseMarker.title = place.name!!
306306
baseMarker.position = LatLng(
307-
place.location.latitude,
308-
place.location.longitude, 0f
307+
place.location!!.latitude,
308+
place.location!!.longitude, 0f
309309
)
310310
baseMarker.place = place
311311
baseMarkersList.add(baseMarker)
@@ -325,7 +325,7 @@ class NearbyController @Inject constructor(
325325
val iter: MutableListIterator<MarkerPlaceGroup> = markerLabelList.listIterator()
326326
while (iter.hasNext()) {
327327
val markerPlaceGroup = iter.next()
328-
if (markerPlaceGroup.place.getWikiDataEntityId() == place.getWikiDataEntityId()) {
328+
if (markerPlaceGroup.place.wikiDataEntityId == place.wikiDataEntityId) {
329329
iter.set(MarkerPlaceGroup(isBookmarked, place))
330330
}
331331
}

0 commit comments

Comments
 (0)