Move PlaceFilter to kmp

pull/2945/head
Alex Baker 4 months ago
parent 95bc9aa6f2
commit eef736a3c1

@ -101,6 +101,7 @@ import org.tasks.data.db.SuspendDbUtils.chunkedMap
import org.tasks.data.entity.Tag
import org.tasks.data.entity.Task
import org.tasks.data.listSettingsClass
import org.tasks.data.open
import org.tasks.data.sql.Join
import org.tasks.data.sql.QueryTemplate
import org.tasks.data.withTransaction
@ -601,7 +602,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL
true
}
R.id.menu_open_map -> {
(filter as PlaceFilter).openMap(context)
(filter as PlaceFilter).place.open(context)
true
}
R.id.menu_share -> {

@ -1,46 +1,13 @@
package org.tasks.data
import android.content.Context
import android.location.Location
import android.net.Uri
import org.tasks.data.entity.Place
import org.tasks.extensions.Context.openUri
import org.tasks.location.MapPosition
import java.util.regex.Pattern
import kotlin.math.abs
private val pattern = Pattern.compile("(\\d+):(\\d+):(\\d+\\.\\d+)")
private val COORDS = Pattern.compile("^\\d+°\\d+'\\d+\\.\\d+\"[NS] \\d+°\\d+'\\d+\\.\\d+\"[EW]$")
fun Place.open(context: Context?) =
context?.openUri("geo:$latitude,$longitude?q=${Uri.encode(displayName)}")
val Place.mapPosition: MapPosition
get() = MapPosition(latitude, longitude)
val Place.displayName: String
get() {
if (!name.isNullOrEmpty() && !COORDS.matcher(name!!).matches()) {
return name!!
}
return if (!address.isNullOrEmpty()) {
address!!
} else {
"${formatCoordinate(latitude, true)} ${formatCoordinate(longitude, false)}"
}
}
private fun formatCoordinate(coordinates: Double, latitude: Boolean): String {
val output = Location.convert(abs(coordinates), Location.FORMAT_SECONDS)
val matcher = pattern.matcher(output)
return if (matcher.matches()) {
val direction = if (latitude) {
if (coordinates > 0) "N" else "S"
} else {
if (coordinates > 0) "E" else "W"
}
"${matcher.group(1)}°${matcher.group(2)}'${matcher.group(3)}\"$direction"
} else {
coordinates.toString()
}
}

@ -16,15 +16,14 @@ import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.mikepenz.iconics.IconicsDrawable;
import org.tasks.icons.OutlinedGoogleMaterial;
import org.tasks.R;
import org.tasks.billing.Inventory;
import org.tasks.data.PlaceExtensionsKt;
import org.tasks.data.PlaceUsage;
import org.tasks.data.entity.Place;
import org.tasks.filters.FilterExtensionsKt;
import org.tasks.filters.PlaceFilter;
import org.tasks.icons.OutlinedGoogleMaterial;
import org.tasks.location.LocationPickerAdapter.PlaceViewHolder;
import org.tasks.themes.ColorProvider;
import org.tasks.themes.ThemeColor;
@ -108,7 +107,7 @@ public class LocationPickerAdapter extends ListAdapter<PlaceUsage, PlaceViewHold
void bind(PlaceUsage placeUsage, int color, String icon) {
place = placeUsage.place;
String name = PlaceExtensionsKt.getDisplayName(place);
String name = place.getDisplayName();
String address = place.getDisplayAddress();
Drawable drawable = new IconicsDrawable(
context,

@ -2,11 +2,14 @@
package org.tasks
import android.location.Location
import android.os.Parcelable
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.RawValue
import org.tasks.data.BuildConfig
import java.util.Date
import java.util.regex.Pattern
import kotlin.math.abs
actual typealias CommonParcelable = Parcelable
@ -17,3 +20,20 @@ actual typealias CommonIgnoredOnParcel = IgnoredOnParcel
actual val IS_DEBUG = BuildConfig.DEBUG
actual fun Long.printTimestamp(): String = Date(this).toString()
private val pattern = Pattern.compile("(\\d+):(\\d+):(\\d+\\.\\d+)")
actual fun formatCoordinates(coordinates: Double, latitude: Boolean): String {
val output = Location.convert(abs(coordinates), Location.FORMAT_SECONDS)
val matcher = pattern.matcher(output)
return if (matcher.matches()) {
val direction = if (latitude) {
if (coordinates > 0) "N" else "S"
} else {
if (coordinates > 0) "E" else "W"
}
"${matcher.group(1)}°${matcher.group(2)}'${matcher.group(3)}\"$direction"
} else {
coordinates.toString()
}
}

@ -19,3 +19,5 @@ expect interface CommonParcelable
expect val IS_DEBUG: Boolean
expect fun Long.printTimestamp(): String
expect fun formatCoordinates(coordinates: Double, latitude: Boolean): String

@ -12,6 +12,8 @@ import org.tasks.CommonParcelize
import org.tasks.data.NO_ORDER
import org.tasks.data.UUIDHelper
import org.tasks.data.db.Table
import org.tasks.formatCoordinates
import java.util.regex.Pattern
@Serializable
@CommonParcelize
@ -52,7 +54,20 @@ data class Place(
val displayAddress: String?
get() = if (address.isNullOrEmpty()) null else address.replace("$name, ", "")
val displayName: String
get() {
if (!name.isNullOrEmpty() && !COORDS.matcher(name).matches()) {
return name
}
return if (!address.isNullOrEmpty()) {
address
} else {
"${formatCoordinates(latitude, true)} ${formatCoordinates(longitude, false)}"
}
}
companion object {
private val COORDS = Pattern.compile("^\\d+°\\d+'\\d+\\.\\d+\"[NS] \\d+°\\d+'\\d+\\.\\d+\"[EW]$")
const val KEY = "place"
const val TABLE_NAME = "places"
@JvmField val TABLE = Table(TABLE_NAME)

@ -19,3 +19,5 @@ actual typealias CommonIgnoredOnParcel = IgnoredOnParcel
actual val IS_DEBUG = false
actual fun Long.printTimestamp(): String = this.toString()
actual fun formatCoordinates(coordinates: Double, latitude: Boolean) = coordinates.toString()

@ -1,21 +1,18 @@
package org.tasks.filters
import android.content.Context
import kotlinx.parcelize.Parcelize
import org.tasks.CommonParcelize
import org.tasks.data.NO_COUNT
import org.tasks.data.dao.TaskDao.TaskCriteria.activeAndVisible
import org.tasks.data.displayName
import org.tasks.data.entity.Geofence
import org.tasks.data.entity.Place
import org.tasks.data.entity.Task
import org.tasks.data.open
import org.tasks.data.sql.Criterion.Companion.and
import org.tasks.data.sql.Field.Companion.field
import org.tasks.data.sql.Join.Companion.inner
import org.tasks.data.sql.QueryTemplate
import org.tasks.themes.TasksIcons
@Parcelize
@CommonParcelize
data class PlaceFilter(
val place: Place,
override val count: Int = NO_COUNT,
@ -37,6 +34,7 @@ data class PlaceFilter(
override val title: String
get() = place.displayName
override val tint: Int
get() = place.color
@ -47,10 +45,6 @@ data class PlaceFilter(
return other is PlaceFilter && place.id == other.place.id
}
fun openMap(context: Context?) {
place.open(context)
}
companion object {
private val G2 = Geofence.TABLE.`as`("G2")
private val G2_PLACE = field("G2.place")
Loading…
Cancel
Save