Convert Place to data class

pull/2266/head
Alex Baker 2 years ago
parent 2c697fd5b9
commit e41b9561dc

@ -20,7 +20,6 @@ import org.tasks.dialogs.IconPickerDialog
import org.tasks.dialogs.IconPickerDialog.IconPickerCallback
import org.tasks.injection.ThemedInjectingAppCompatActivity
import org.tasks.themes.ColorProvider
import org.tasks.themes.CustomIcons
import org.tasks.themes.CustomIcons.getIconResId
import org.tasks.themes.DrawableUtil
import org.tasks.themes.ThemeColor
@ -37,6 +36,7 @@ abstract class BaseListSettingsActivity : ThemedInjectingAppCompatActivity(), Ic
private lateinit var icon: TextView
protected lateinit var toolbar: Toolbar
protected lateinit var colorRow: ViewGroup
protected abstract val defaultIcon: Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -149,10 +149,7 @@ abstract class BaseListSettingsActivity : ThemedInjectingAppCompatActivity(), Ic
clear.visibility = View.VISIBLE
}
themeColor.applyToNavigationBar(this)
var icon = getIconResId(selectedIcon)
if (icon == null) {
icon = getIconResId(CustomIcons.LIST)
}
val icon = getIconResId(selectedIcon) ?: getIconResId(defaultIcon)
DrawableUtil.setLeftDrawable(this, this.icon, icon!!)
DrawableUtil.getLeftDrawable(this.icon).setTint(getColor(R.color.icon_tint_with_alpha))
}

@ -41,6 +41,7 @@ import org.tasks.databinding.FilterSettingsActivityBinding
import org.tasks.db.QueryUtils
import org.tasks.extensions.Context.openUri
import org.tasks.filters.FilterCriteriaProvider
import org.tasks.themes.CustomIcons
import java.util.*
import javax.inject.Inject
import kotlin.math.max
@ -60,6 +61,7 @@ class FilterSettingsActivity : BaseListSettingsActivity() {
private var filter: CustomFilter? = null
private lateinit var adapter: CustomFilterAdapter
private var criteria: MutableList<CriterionInstance> = ArrayList()
override val defaultIcon: Int = CustomIcons.FILTER
override fun onCreate(savedInstanceState: Bundle?) {
filter = intent.getParcelableExtra(TOKEN_FILTER)

@ -26,6 +26,7 @@ import org.tasks.data.CaldavCalendar
import org.tasks.data.GoogleTaskListDao
import org.tasks.databinding.ActivityGoogleTaskListSettingsBinding
import org.tasks.extensions.Context.toast
import org.tasks.themes.CustomIcons
import timber.log.Timber
import javax.inject.Inject
@ -42,6 +43,7 @@ class GoogleTaskListSettingsActivity : BaseListSettingsActivity() {
private val createListViewModel: CreateListViewModel by viewModels()
private val renameListViewModel: RenameListViewModel by viewModels()
private val deleteListViewModel: DeleteListViewModel by viewModels()
override val defaultIcon: Int = CustomIcons.LIST
override fun onCreate(savedInstanceState: Bundle?) {
gtasksList = intent.getParcelableExtra(EXTRA_STORE_DATA)

@ -20,7 +20,8 @@ import org.tasks.extensions.formatNumber
import org.tasks.filters.PlaceFilter
import org.tasks.location.MapFragment
import org.tasks.preferences.Preferences
import java.util.*
import org.tasks.themes.CustomIcons
import java.util.Locale
import javax.inject.Inject
import kotlin.math.roundToInt
@ -46,6 +47,7 @@ class PlaceSettingsActivity : BaseListSettingsActivity(), MapFragment.MapFragmen
@Inject lateinit var localBroadcastManager: LocalBroadcastManager
private lateinit var place: Place
override val defaultIcon: Int = CustomIcons.PLACE
override fun onCreate(savedInstanceState: Bundle?) {
if (intent?.hasExtra(EXTRA_PLACE) != true) {
@ -65,7 +67,7 @@ class PlaceSettingsActivity : BaseListSettingsActivity(), MapFragment.MapFragmen
if (savedInstanceState == null) {
name.setText(place.displayName)
selectedColor = place.color
selectedIcon = place.getIcon()
selectedIcon = place.icon
}
val dark = preferences.mapTheme == 2
@ -102,7 +104,7 @@ class PlaceSettingsActivity : BaseListSettingsActivity(), MapFragment.MapFragmen
override fun hasChanges() = name.text.toString() != place.displayName
|| selectedColor != place.color
|| selectedIcon != place.getIcon()
|| selectedIcon != place.icon
override suspend fun save() {
val newName: String = name.text.toString()
@ -112,10 +114,12 @@ class PlaceSettingsActivity : BaseListSettingsActivity(), MapFragment.MapFragmen
return
}
place.name = newName
place.color = selectedColor
place.setIcon(selectedIcon)
place.radius = slider.value.toInt()
place = place.copy(
name = newName,
color = selectedColor,
icon = selectedIcon,
radius = slider.value.toInt(),
)
locationDao.update(place)
setResult(
Activity.RESULT_OK,

@ -24,6 +24,7 @@ import org.tasks.data.TagDao
import org.tasks.data.TagData
import org.tasks.data.TagDataDao
import org.tasks.databinding.ActivityTagSettingsBinding
import org.tasks.themes.CustomIcons
import javax.inject.Inject
@AndroidEntryPoint
@ -36,6 +37,7 @@ class TagSettingsActivity : BaseListSettingsActivity() {
private var isNewTag = false
private lateinit var tagData: TagData
override val defaultIcon: Int = CustomIcons.LABEL
override fun onCreate(savedInstanceState: Bundle?) {
tagData = intent.getParcelableExtra(EXTRA_TAG_DATA)

@ -28,7 +28,7 @@ import org.tasks.data.CaldavTask
import org.tasks.data.FilterDao
import org.tasks.data.Geofence
import org.tasks.data.LocationDao
import org.tasks.data.Place.Companion.newPlace
import org.tasks.data.Place
import org.tasks.data.Tag
import org.tasks.data.TagDao
import org.tasks.data.TagData
@ -214,13 +214,14 @@ class TasksJsonImporter @Inject constructor(
)
}
for (location in backup.locations) {
val place = newPlace()
place.longitude = location.longitude
place.latitude = location.latitude
place.name = location.name
place.address = location.address
place.url = location.url
place.phone = location.phone
val place = Place(
longitude = location.longitude,
latitude = location.latitude,
name = location.name,
address = location.address,
url = location.url,
phone = location.phone,
)
locationDao.insert(place)
locationDao.insert(
Geofence(

@ -26,6 +26,7 @@ import org.tasks.data.CaldavAccount
import org.tasks.data.CaldavCalendar
import org.tasks.data.CaldavDao
import org.tasks.databinding.ActivityCaldavCalendarSettingsBinding
import org.tasks.themes.CustomIcons
import org.tasks.ui.DisplayableException
import java.net.ConnectException
import javax.inject.Inject
@ -42,6 +43,7 @@ abstract class BaseCaldavCalendarSettingsActivity : BaseListSettingsActivity() {
protected var caldavCalendar: CaldavCalendar? = null
protected lateinit var caldavAccount: CaldavAccount
override val defaultIcon: Int = CustomIcons.LIST
override fun bind() = ActivityCaldavCalendarSettingsBinding.inflate(layoutInflater).let {
root = it.rootLayout

@ -77,10 +77,15 @@ class iCalendar @Inject constructor(
}
var place: Place? = locationDao.findPlace(
geo.latitude.toLikeString(),
geo.longitude.toLikeString())
geo.longitude.toLikeString()
)
if (place == null) {
place = Place.newPlace(geo)
place.id = locationDao.insert(place)
place = Place(
latitude = geo.latitude.toDouble(),
longitude = geo.longitude.toDouble(),
).let {
it.copy(id = locationDao.insert(it))
}
workManager.reverseGeocode(place)
}
val existing = locationDao.getGeofences(taskId)

@ -110,14 +110,14 @@ fun SampleLocation() {
LocationRow(
location = Location(
Geofence(),
Place().apply {
name = "Googleplex"
Place(
name = "Googleplex",
address = "1600 Amphitheatre Pkwy, Mountain View, CA 94043"
},
),
),
hasPermissions = true,
onClick = {},
openGeofenceOptions = {},
)
}
}
}

@ -13,99 +13,83 @@ import androidx.room.PrimaryKey
import com.todoroo.andlib.data.Table
import com.todoroo.astrid.api.FilterListItem.NO_ORDER
import com.todoroo.astrid.helper.UUIDHelper
import net.fortuna.ical4j.model.property.Geo
import org.tasks.Strings
import org.tasks.extensions.Context.openUri
import org.tasks.location.MapPosition
import org.tasks.themes.CustomIcons.PLACE
import java.io.Serializable
import java.util.regex.Pattern
import kotlin.math.abs
@Entity(tableName = Place.TABLE_NAME, indices = [Index(name = "place_uid", value = ["uid"], unique = true)])
class Place : Serializable, Parcelable {
@Entity(
tableName = Place.TABLE_NAME,
indices = [
Index(name = "place_uid", value = ["uid"], unique = true)
],
)
data class Place(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "place_id")
@Transient
var id: Long = 0
val id: Long = 0,
@ColumnInfo(name = "uid")
var uid: String? = null
val uid: String? = UUIDHelper.newUUID(),
@ColumnInfo(name = "name")
var name: String? = null
val name: String? = null,
@ColumnInfo(name = "address")
var address: String? = null
val address: String? = null,
@ColumnInfo(name = "phone")
var phone: String? = null
val phone: String? = null,
@ColumnInfo(name = "url")
var url: String? = null
val url: String? = null,
@ColumnInfo(name = "latitude")
var latitude = 0.0
val latitude: Double = 0.0,
@ColumnInfo(name = "longitude")
var longitude = 0.0
val longitude: Double = 0.0,
@ColumnInfo(name = "place_color")
var color = 0
val color: Int = 0,
@ColumnInfo(name = "place_icon")
private var icon = -1
val icon: Int = -1,
@ColumnInfo(name = "place_order")
var order = NO_ORDER
val order: Int = NO_ORDER,
@ColumnInfo(name = "radius", defaultValue = "250")
var radius = 250
constructor()
val radius: Int = 250,
) : Serializable, Parcelable {
@Ignore
constructor(o: Place) {
id = o.id
uid = o.uid
name = o.name
address = o.address
phone = o.phone
url = o.url
latitude = o.latitude
longitude = o.longitude
color = o.color
icon = o.icon
order = o.order
radius = o.radius
}
constructor(o: Place): this(
id = o.id,
uid = o.uid,
name = o.name,
address = o.address,
phone = o.phone,
url = o.url,
latitude = o.latitude,
longitude = o.longitude,
color = o.color,
icon = o.icon,
order = o.order,
radius = o.radius,
)
@Ignore
constructor(parcel: Parcel) {
id = parcel.readLong()
uid = parcel.readString()
name = parcel.readString()
address = parcel.readString()
phone = parcel.readString()
url = parcel.readString()
latitude = parcel.readDouble()
longitude = parcel.readDouble()
color = parcel.readInt()
icon = parcel.readInt()
order = parcel.readInt()
radius = parcel.readInt()
}
fun getIcon(): Int = if (icon == -1) PLACE else icon
fun setIcon(icon: Int) {
this.icon = icon
}
constructor(parcel: Parcel): this(
id = parcel.readLong(),
uid = parcel.readString(),
name = parcel.readString(),
address = parcel.readString(),
phone = parcel.readString(),
url = parcel.readString(),
latitude = parcel.readDouble(),
longitude = parcel.readDouble(),
color = parcel.readInt(),
icon = parcel.readInt(),
order = parcel.readInt(),
radius = parcel.readInt(),
)
val displayName: String
get() {
if (!Strings.isNullOrEmpty(name) && !COORDS.matcher(name!!).matches()) {
return name!!
return name
}
return if (!Strings.isNullOrEmpty(address)) {
address!!
@ -142,48 +126,6 @@ class Place : Serializable, Parcelable {
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Place
if (id != other.id) return false
if (uid != other.uid) return false
if (name != other.name) return false
if (address != other.address) return false
if (phone != other.phone) return false
if (url != other.url) return false
if (latitude != other.latitude) return false
if (longitude != other.longitude) return false
if (color != other.color) return false
if (icon != other.icon) return false
if (order != other.order) return false
if (radius != other.radius) return false
return true
}
override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + (uid?.hashCode() ?: 0)
result = 31 * result + (name?.hashCode() ?: 0)
result = 31 * result + (address?.hashCode() ?: 0)
result = 31 * result + (phone?.hashCode() ?: 0)
result = 31 * result + (url?.hashCode() ?: 0)
result = 31 * result + latitude.hashCode()
result = 31 * result + longitude.hashCode()
result = 31 * result + color
result = 31 * result + icon
result = 31 * result + order
result = 31 * result + radius
return result
}
override fun toString(): String {
return "Place(id=$id, uid=$uid, name=$name, address=$address, phone=$phone, url=$url, latitude=$latitude, longitude=$longitude, color=$color, icon=$icon, order=$order, radius=$radius)"
}
companion object {
const val KEY = "place"
const val TABLE_NAME = "places"
@ -213,16 +155,5 @@ class Place : Serializable, Parcelable {
}
}
@JvmStatic fun newPlace(geo: Geo): Place = newPlace().apply {
latitude = geo.latitude.toDouble()
longitude = geo.longitude.toDouble()
}
fun newPlace(mapPosition: MapPosition) = newPlace().apply {
latitude = mapPosition.latitude
longitude = mapPosition.longitude
}
@JvmStatic fun newPlace(): Place = Place().apply { uid = UUIDHelper.newUUID() }
}
}

@ -10,7 +10,7 @@ class PlaceUsage {
get() = place.color
val icon: Int
get() = place.getIcon()
get() = place.icon
override fun equals(other: Any?): Boolean {
if (this === other) return true

@ -3,7 +3,9 @@ package org.tasks.filters;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import com.todoroo.andlib.data.Table;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Field;
@ -12,12 +14,15 @@ import com.todoroo.andlib.sql.QueryTemplate;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterListItem;
import com.todoroo.astrid.data.Task;
import java.util.HashMap;
import java.util.Map;
import org.tasks.R;
import org.tasks.data.Geofence;
import org.tasks.data.Place;
import org.tasks.data.TaskDao;
import org.tasks.themes.CustomIcons;
import java.util.HashMap;
import java.util.Map;
public class PlaceFilter extends Filter {
@ -61,6 +66,9 @@ public class PlaceFilter extends Filter {
id = place.getId();
tint = place.getColor();
icon = place.getIcon();
if (icon == -1) {
icon = CustomIcons.PLACE;
}
order = place.getOrder();
}

@ -41,11 +41,14 @@ class ReverseGeocodeWork @AssistedInject constructor(
Timber.d("Ignoring $result - ${distanceBetween}m away")
return Result.failure()
}
place.name = result.name
place.address = result.address
place.phone = result.phone
place.url = result.url
locationDao.update(place)
locationDao.update(
place.copy(
name = result.name,
address = result.address,
phone = result.phone,
url = result.url,
)
)
localBroadcastManager.broadcastRefresh()
Timber.d("found $result")
Result.success()

@ -12,7 +12,6 @@ import okhttp3.Request
import org.tasks.DebugNetworkInterceptor
import org.tasks.R
import org.tasks.data.Place
import org.tasks.data.Place.Companion.newPlace
import org.tasks.extensions.JsonObject.getStringOrNull
import org.tasks.preferences.Preferences
import java.io.IOException
@ -48,24 +47,23 @@ class GeocoderMapbox @Inject constructor(
.takeIf { it.size() > 0 }?.get(0)?.asJsonObject
?.let { toPlace(it) }
internal fun toPlace(feature: JsonObject): Place =
newPlace().apply {
val types = feature.get("place_type").asStringList
val text = feature.get("text").asString
name = if (types.contains("address")) {
feature
.getStringOrNull("address")
?.let { "$it $text" }
?: text
} else {
text
}
address = feature.get("place_name").asString
feature.get("center").asCoordinates.let {
longitude = it.first
latitude = it.second
}
}
internal fun toPlace(feature: JsonObject): Place {
val text = feature.get("text").asString
val coords = feature.get("center").asCoordinates
return Place(
name = if (feature.get("place_type").asStringList.contains("address")) {
feature
.getStringOrNull("address")
?.let { "$it $text" }
?: text
} else {
text
},
address = feature.get("place_name").asString,
longitude = coords.first,
latitude = coords.second,
)
}
private val JsonElement.asStringList: List<String>
get() = asJsonArray.map { it.asString }

@ -10,7 +10,6 @@ import okhttp3.Request
import org.tasks.BuildConfig
import org.tasks.R
import org.tasks.data.Place
import org.tasks.data.Place.Companion.newPlace
import org.tasks.extensions.JsonObject.getOrNull
import org.tasks.extensions.JsonObject.getStringOrNull
import org.tasks.http.HttpClientFactory
@ -50,16 +49,15 @@ class GeocoderNominatim @Inject constructor(
.get("properties").asJsonObject
.get("geocoding").asJsonObject
val geometry = feature.get("geometry").asJsonObject
newPlace().apply {
val coords = geometry.get("coordinates").asCoordinates
return Place(
name = geocoding.getStringOrNull("name")
?: geocoding.getStringOrNull("housenumber")
?.let { "$it ${geocoding.get("street").asString}" }
address = geocoding.getOrNull("label")?.asString
geometry.get("coordinates").asCoordinates.let {
longitude = it.first
latitude = it.second
}
}
?.let { "$it ${geocoding.get("street").asString}" },
address = geocoding.getOrNull("label")?.asString,
longitude = coords.first,
latitude = coords.second,
)
}
private val JsonElement.asCoordinates: Pair<Double, Double>

@ -35,7 +35,6 @@ import org.tasks.billing.Inventory
import org.tasks.caldav.GeoUtils.toLikeString
import org.tasks.data.LocationDao
import org.tasks.data.Place
import org.tasks.data.Place.Companion.newPlace
import org.tasks.data.PlaceUsage
import org.tasks.databinding.ActivityLocationPickerBinding
import org.tasks.dialogs.DialogBuilder
@ -231,7 +230,13 @@ class LocationPickerActivity : InjectingAppCompatActivity(), Toolbar.OnMenuItemC
loadingIndicator.visibility = View.VISIBLE
lifecycleScope.launch {
try {
returnPlace(geocoder.reverseGeocode(mapPosition) ?: newPlace(mapPosition))
returnPlace(
geocoder.reverseGeocode(mapPosition)
?: Place(
latitude = mapPosition.latitude,
longitude = mapPosition.longitude,
)
)
} catch (e: Exception) {
loadingIndicator.visibility = View.GONE
firebase.reportException(e)
@ -268,14 +273,12 @@ class LocationPickerActivity : InjectingAppCompatActivity(), Toolbar.OnMenuItemC
lifecycleScope.launch {
var place = place
if (place.id <= 0) {
val existing = locationDao.findPlace(
place = locationDao
.findPlace(
place.latitude.toLikeString(),
place.longitude.toLikeString())
if (existing == null) {
place.id = locationDao.insert(place)
} else {
place = existing
}
place.longitude.toLikeString()
)
?: place.copy(id = locationDao.insert(place))
}
setResult(Activity.RESULT_OK, Intent().putExtra(EXTRA_PLACE, place as Parcelable?))
finish()

@ -12,11 +12,10 @@ import org.tasks.R
import org.tasks.data.CaldavAccount.Companion.TYPE_TASKS
import org.tasks.data.CaldavDao
import org.tasks.data.Place
import org.tasks.data.Place.Companion.newPlace
import org.tasks.http.HttpClientFactory
import org.tasks.http.HttpException
import timber.log.Timber
import java.util.*
import java.util.UUID
import javax.inject.Inject
class PlaceSearchGoogle @Inject constructor(
@ -126,14 +125,14 @@ class PlaceSearchGoogle @Inject constructor(
internal fun toPlace(json: JsonObject): Place {
val result = json.get("result").asJsonObject
val location = result.get("geometry").asJsonObject.get("location").asJsonObject
return newPlace().apply {
name = result.get("name").asString
address = result.getString("formatted_address")
phone = result.getString("international_phone_number")
url = result.getString("website")
latitude = location.get("lat").asDouble
longitude = location.get("lng").asDouble
}
return Place(
name = result.get("name").asString,
address = result.getString("formatted_address"),
phone = result.getString("international_phone_number"),
url = result.getString("website"),
latitude = location.get("lat").asDouble,
longitude = location.get("lng").asDouble,
)
}
private fun JsonObject.getString(field: String): String? = if (has(field)) {

Loading…
Cancel
Save