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

@ -1,46 +1,13 @@
package org.tasks.data package org.tasks.data
import android.content.Context import android.content.Context
import android.location.Location
import android.net.Uri import android.net.Uri
import org.tasks.data.entity.Place import org.tasks.data.entity.Place
import org.tasks.extensions.Context.openUri import org.tasks.extensions.Context.openUri
import org.tasks.location.MapPosition 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?) = fun Place.open(context: Context?) =
context?.openUri("geo:$latitude,$longitude?q=${Uri.encode(displayName)}") context?.openUri("geo:$latitude,$longitude?q=${Uri.encode(displayName)}")
val Place.mapPosition: MapPosition val Place.mapPosition: MapPosition
get() = MapPosition(latitude, longitude) 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 androidx.recyclerview.widget.RecyclerView;
import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.iconics.IconicsDrawable;
import org.tasks.icons.OutlinedGoogleMaterial;
import org.tasks.R; import org.tasks.R;
import org.tasks.billing.Inventory; import org.tasks.billing.Inventory;
import org.tasks.data.PlaceExtensionsKt;
import org.tasks.data.PlaceUsage; import org.tasks.data.PlaceUsage;
import org.tasks.data.entity.Place; import org.tasks.data.entity.Place;
import org.tasks.filters.FilterExtensionsKt; import org.tasks.filters.FilterExtensionsKt;
import org.tasks.filters.PlaceFilter; import org.tasks.filters.PlaceFilter;
import org.tasks.icons.OutlinedGoogleMaterial;
import org.tasks.location.LocationPickerAdapter.PlaceViewHolder; import org.tasks.location.LocationPickerAdapter.PlaceViewHolder;
import org.tasks.themes.ColorProvider; import org.tasks.themes.ColorProvider;
import org.tasks.themes.ThemeColor; import org.tasks.themes.ThemeColor;
@ -108,7 +107,7 @@ public class LocationPickerAdapter extends ListAdapter<PlaceUsage, PlaceViewHold
void bind(PlaceUsage placeUsage, int color, String icon) { void bind(PlaceUsage placeUsage, int color, String icon) {
place = placeUsage.place; place = placeUsage.place;
String name = PlaceExtensionsKt.getDisplayName(place); String name = place.getDisplayName();
String address = place.getDisplayAddress(); String address = place.getDisplayAddress();
Drawable drawable = new IconicsDrawable( Drawable drawable = new IconicsDrawable(
context, context,

@ -2,11 +2,14 @@
package org.tasks package org.tasks
import android.location.Location
import android.os.Parcelable import android.os.Parcelable
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.RawValue import kotlinx.parcelize.RawValue
import org.tasks.data.BuildConfig import org.tasks.data.BuildConfig
import java.util.Date import java.util.Date
import java.util.regex.Pattern
import kotlin.math.abs
actual typealias CommonParcelable = Parcelable actual typealias CommonParcelable = Parcelable
@ -16,4 +19,21 @@ actual typealias CommonIgnoredOnParcel = IgnoredOnParcel
actual val IS_DEBUG = BuildConfig.DEBUG actual val IS_DEBUG = BuildConfig.DEBUG
actual fun Long.printTimestamp(): String = Date(this).toString() 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()
}
}

@ -18,4 +18,6 @@ expect interface CommonParcelable
expect val IS_DEBUG: Boolean expect val IS_DEBUG: Boolean
expect fun Long.printTimestamp(): String 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.NO_ORDER
import org.tasks.data.UUIDHelper import org.tasks.data.UUIDHelper
import org.tasks.data.db.Table import org.tasks.data.db.Table
import org.tasks.formatCoordinates
import java.util.regex.Pattern
@Serializable @Serializable
@CommonParcelize @CommonParcelize
@ -52,7 +54,20 @@ data class Place(
val displayAddress: String? val displayAddress: String?
get() = if (address.isNullOrEmpty()) null else address.replace("$name, ", "") 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 { companion object {
private val COORDS = Pattern.compile("^\\d+°\\d+'\\d+\\.\\d+\"[NS] \\d+°\\d+'\\d+\\.\\d+\"[EW]$")
const val KEY = "place" const val KEY = "place"
const val TABLE_NAME = "places" const val TABLE_NAME = "places"
@JvmField val TABLE = Table(TABLE_NAME) @JvmField val TABLE = Table(TABLE_NAME)
@ -60,4 +75,4 @@ data class Place(
@JvmField val NAME = TABLE.column("name") @JvmField val NAME = TABLE.column("name")
@JvmField val ADDRESS = TABLE.column("address") @JvmField val ADDRESS = TABLE.column("address")
} }
} }

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

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