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

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

@ -1,26 +1,79 @@
package net.pokeranalytics.android.ui.fragment package net.pokeranalytics.android.ui.fragment
import net.pokeranalytics.android.exceptions.TypeException
import net.pokeranalytics.android.model.realm.Location 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.helpers.PlacePickerManager
import net.pokeranalytics.android.ui.view.RowRepresentable 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.LocationRow
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
/** /**
* Custom EditableDataFragment to manage the LOCATE_ME case * 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) { override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
when(row) { when (row) {
LocationRow.LOCATE_ME -> { LocationRow.LOCATE_ME -> {
if (item is Location) { isLookingForPlaces = true
(item as Location).isLookingForPlaces = true
PlacePickerManager.create(parentActivity, row, this) PlacePickerManager.create(parentActivity, row, this)
rowRepresentableAdapter.refreshRow(row) rowRepresentableAdapter.refreshRow(row)
} else {
throw TypeException("Need to manage LocationRow.LOCATE_ME for ${item::class.java}")
}
} }
else -> super.onRowSelected(position, row, fromAction) else -> super.onRowSelected(position, row, fromAction)
} }
@ -30,6 +83,7 @@ class LocationDataFragment: EditableDataFragment() {
super.onRowValueChanged(value, row) super.onRowValueChanged(value, row)
when (row) { when (row) {
LocationRow.LOCATE_ME -> { LocationRow.LOCATE_ME -> {
isLookingForPlaces = false
rowRepresentableAdapter.notifyDataSetChanged() rowRepresentableAdapter.notifyDataSetChanged()
} }
} }

Loading…
Cancel
Save