Refactor location management (and EditableDataFragment)

feature/top10
Aurelien Hubert 7 years ago
parent 83e50182bb
commit e2e036546d
  1. 53
      app/src/main/java/net/pokeranalytics/android/model/realm/Location.kt
  2. 22
      app/src/main/java/net/pokeranalytics/android/ui/fragment/EditableDataFragment.kt
  3. 76
      app/src/main/java/net/pokeranalytics/android/ui/fragment/LocationDataFragment.kt

@ -2,20 +2,16 @@ package net.pokeranalytics.android.model.realm
import com.google.android.libraries.places.api.model.Place
import io.realm.RealmObject
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.Manageable
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.rowrepresentable.LocationRow
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
import java.util.*
import kotlin.collections.ArrayList
open class Location : RealmObject(), Manageable, StaticRowRepresentableDataSource, RowRepresentable {
open class Location : RealmObject(), Manageable, RowRepresentable {
@PrimaryKey
var id = UUID.randomUUID().toString()
@ -32,9 +28,6 @@ open class Location : RealmObject(), Manageable, StaticRowRepresentableDataSourc
// the latitude of the location
var latitude: Double? = null
@Ignore
var isLookingForPlaces = false
override fun getDisplayName(): String {
return this.name
}
@ -43,54 +36,12 @@ open class Location : RealmObject(), Manageable, StaticRowRepresentableDataSourc
return this.id
}
override fun adapterRows(): List<RowRepresentable>? {
val rows = ArrayList<RowRepresentable>()
rows.add(SimpleRow.NAME)
rows.addAll(LocationRow.values())
return rows
}
override fun stringForRow(row: RowRepresentable): String {
return when (row) {
SimpleRow.NAME -> this.name
LocationRow.ADDRESS -> this.address
else -> return super.stringForRow(row)
}
}
override fun boolForRow(row: RowRepresentable): Boolean {
return when(row) {
LocationRow.LOCATE_ME -> return isLookingForPlaces
else -> super.boolForRow(row)
}
}
override fun editDescriptors(row: RowRepresentable): ArrayList<RowRepresentableEditDescriptor> {
val data = java.util.ArrayList<RowRepresentableEditDescriptor>()
when (row) {
SimpleRow.NAME -> data.add(
RowRepresentableEditDescriptor(
this.name,
SimpleRow.NAME.resId
)
)
LocationRow.ADDRESS -> data.add(
RowRepresentableEditDescriptor(
this.address,
LocationRow.ADDRESS.resId
)
)
}
return data
}
override fun updateValue(value: Any?, row: RowRepresentable) {
when (row) {
SimpleRow.NAME -> this.name = value as String? ?: ""
LocationRow.ADDRESS -> this.address = value as String? ?: ""
LocationRow.LOCATE_ME -> {
isLookingForPlaces = false
if (value is Place) {
setPlace(value)
}
@ -109,7 +60,7 @@ open class Location : RealmObject(), Manageable, StaticRowRepresentableDataSourc
/**
* Fill the location attributes with a place object
*/
fun setPlace(place: Place) {
private fun setPlace(place: Place) {
this.name = place.name ?: ""
this.address = place.address ?: ""
this.latitude = place.latLng?.latitude

@ -4,7 +4,6 @@ import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.RealmObject
@ -64,7 +63,7 @@ open class EditableDataFragment : PokerAnalyticsFragment(), RowRepresentableDele
}
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
BottomSheetFragment.create(fragmentManager, row, this, (this.item as RowRepresentableDataSource).editDescriptors(row))
BottomSheetFragment.create(fragmentManager, row, this, getDataSource().editDescriptors(row))
}
override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
@ -91,6 +90,12 @@ open class EditableDataFragment : PokerAnalyticsFragment(), RowRepresentableDele
}
}
/**
* Return the data source
*/
open fun getDataSource(): RowRepresentableDataSource {
return this.item as RowRepresentableDataSource
}
/**
* Init data
@ -99,21 +104,22 @@ open class EditableDataFragment : PokerAnalyticsFragment(), RowRepresentableDele
if (this.dataType != null) {
val proxyItem: RealmObject? = this.liveDataType.getData(this.getRealm(), primaryKey)
proxyItem?.let {
//TODO: Localize
this.appBar.toolbar.title = "Update ${this.liveDataType.localizedTitle(this.parentActivity).toLowerCase().capitalize()}"
isUpdating = true
} ?: run {
//TODO: Localize
this.appBar.toolbar.title = "New ${this.liveDataType.localizedTitle(this.parentActivity).toLowerCase().capitalize()}"
}
this.item = this.liveDataType.updateOrCreate(this.getRealm(), primaryKey)
this.rowRepresentableAdapter = RowRepresentableAdapter(
(this.item as RowRepresentableDataSource),
this
)
val dataSource = getDataSource()
this.rowRepresentableAdapter = RowRepresentableAdapter(getDataSource(), this)
this.recyclerView.adapter = rowRepresentableAdapter
// When creating an object, open automatically the keyboard for the first row
if (!isUpdating && this.item is RowRepresentableDataSource) {
val row = (this.item as RowRepresentableDataSource).adapterRows()?.firstOrNull()
if (!isUpdating) {
val row = dataSource.adapterRows()?.firstOrNull()
row?.let {
onRowSelected(0, it)
}

@ -1,28 +1,81 @@
package net.pokeranalytics.android.ui.fragment
import net.pokeranalytics.android.exceptions.TypeException
import net.pokeranalytics.android.model.realm.Location
import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.helpers.PlacePickerManager
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor
import net.pokeranalytics.android.ui.view.rowrepresentable.LocationRow
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
/**
* Custom EditableDataFragment to manage the LOCATE_ME case
*/
class LocationDataFragment: EditableDataFragment() {
class LocationDataFragment : EditableDataFragment(), StaticRowRepresentableDataSource {
// Return the item as a Location object
private val location: Location
get() {
return this.item as Location
}
// Loader boolean
private var isLookingForPlaces: Boolean = false
override fun getDataSource(): RowRepresentableDataSource {
return this
}
override fun adapterRows(): List<RowRepresentable>? {
val rows = ArrayList<RowRepresentable>()
rows.add(SimpleRow.NAME)
rows.addAll(LocationRow.values())
return rows
}
override fun stringForRow(row: RowRepresentable): String {
return when (row) {
SimpleRow.NAME -> location.name
LocationRow.ADDRESS -> location.address
else -> return super.stringForRow(row)
}
}
override fun boolForRow(row: RowRepresentable): Boolean {
return when (row) {
LocationRow.LOCATE_ME -> return isLookingForPlaces
else -> super.boolForRow(row)
}
}
override fun editDescriptors(row: RowRepresentable): ArrayList<RowRepresentableEditDescriptor> {
val data = java.util.ArrayList<RowRepresentableEditDescriptor>()
when (row) {
SimpleRow.NAME -> data.add(
RowRepresentableEditDescriptor(
location.name,
SimpleRow.NAME.resId
)
)
LocationRow.ADDRESS -> data.add(
RowRepresentableEditDescriptor(
location.address,
LocationRow.ADDRESS.resId
)
)
}
return data
}
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
when(row) {
when (row) {
LocationRow.LOCATE_ME -> {
if (item is Location) {
(item as Location).isLookingForPlaces = true
PlacePickerManager.create(parentActivity, row, this)
rowRepresentableAdapter.refreshRow(row)
} else {
throw TypeException("Need to manage LocationRow.LOCATE_ME for ${item::class.java}")
}
isLookingForPlaces = true
PlacePickerManager.create(parentActivity, row, this)
rowRepresentableAdapter.refreshRow(row)
}
else -> super.onRowSelected(position, row, fromAction)
else -> super.onRowSelected(position, row, fromAction)
}
}
@ -30,6 +83,7 @@ class LocationDataFragment: EditableDataFragment() {
super.onRowValueChanged(value, row)
when (row) {
LocationRow.LOCATE_ME -> {
isLookingForPlaces = false
rowRepresentableAdapter.notifyDataSetChanged()
}
}

Loading…
Cancel
Save