|
|
|
@ -1,13 +1,20 @@ |
|
|
|
package net.pokeranalytics.android.ui.fragment |
|
|
|
package net.pokeranalytics.android.ui.fragment |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle |
|
|
|
|
|
|
|
import android.view.View |
|
|
|
|
|
|
|
import com.google.android.libraries.places.api.model.PlaceLikelihood |
|
|
|
|
|
|
|
import net.pokeranalytics.android.R |
|
|
|
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.RowRepresentableDataSource |
|
|
|
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource |
|
|
|
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.RowRepresentableEditDescriptor |
|
|
|
|
|
|
|
import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
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 net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable |
|
|
|
|
|
|
|
import timber.log.Timber |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Custom EditableDataFragment to manage the LOCATE_ME case |
|
|
|
* Custom EditableDataFragment to manage the LOCATE_ME case |
|
|
|
@ -22,29 +29,52 @@ class LocationDataFragment : EditableDataFragment(), StaticRowRepresentableDataS |
|
|
|
|
|
|
|
|
|
|
|
// Loader boolean |
|
|
|
// Loader boolean |
|
|
|
private var isLookingForPlaces: Boolean = false |
|
|
|
private var isLookingForPlaces: Boolean = false |
|
|
|
|
|
|
|
private var placesForRows: HashMap<CustomizableRowRepresentable, PlaceLikelihood> = HashMap() |
|
|
|
|
|
|
|
private var rowPlaces: ArrayList<CustomizableRowRepresentable> = ArrayList() |
|
|
|
|
|
|
|
private var locationActivated = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val rows = ArrayList<RowRepresentable>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
|
|
|
|
|
|
|
super.onViewCreated(view, savedInstanceState) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shouldOpenKeyboard = false |
|
|
|
|
|
|
|
locationActivated = parentActivity.hasLocationPermissionGranted() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isUpdating) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we update a location, we set the switch to the correct value |
|
|
|
|
|
|
|
locationActivated = location.latitude != null && location.longitude != null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (locationActivated) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we create a new location, we try to locate the user by default |
|
|
|
|
|
|
|
isLookingForPlaces = true |
|
|
|
|
|
|
|
getSuggestionsPlaces() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateAdapterUI() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun getDataSource(): RowRepresentableDataSource { |
|
|
|
override fun getDataSource(): RowRepresentableDataSource { |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun adapterRows(): List<RowRepresentable>? { |
|
|
|
override fun adapterRows(): List<RowRepresentable>? { |
|
|
|
val rows = ArrayList<RowRepresentable>() |
|
|
|
|
|
|
|
rows.add(SimpleRow.NAME) |
|
|
|
|
|
|
|
rows.addAll(LocationRow.values()) |
|
|
|
|
|
|
|
return rows |
|
|
|
return rows |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun stringForRow(row: RowRepresentable): String { |
|
|
|
override fun stringForRow(row: RowRepresentable): String { |
|
|
|
return when (row) { |
|
|
|
return when (row) { |
|
|
|
SimpleRow.NAME -> location.name |
|
|
|
SimpleRow.NAME -> location.name |
|
|
|
LocationRow.ADDRESS -> location.address |
|
|
|
|
|
|
|
else -> return super.stringForRow(row) |
|
|
|
else -> return super.stringForRow(row) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun boolForRow(row: RowRepresentable): Boolean { |
|
|
|
override fun boolForRow(row: RowRepresentable): Boolean { |
|
|
|
return when (row) { |
|
|
|
return when (row) { |
|
|
|
LocationRow.LOCATE_ME -> return isLookingForPlaces |
|
|
|
LocationRow.LOCATION_PERMISSION_SWITCH -> return locationActivated |
|
|
|
else -> super.boolForRow(row) |
|
|
|
else -> super.boolForRow(row) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -58,17 +88,19 @@ class LocationDataFragment : EditableDataFragment(), StaticRowRepresentableDataS |
|
|
|
SimpleRow.NAME.resId |
|
|
|
SimpleRow.NAME.resId |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
LocationRow.ADDRESS -> data.add( |
|
|
|
|
|
|
|
RowRepresentableEditDescriptor( |
|
|
|
|
|
|
|
location.address, |
|
|
|
|
|
|
|
LocationRow.ADDRESS.resId |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return data |
|
|
|
return data |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
|
|
|
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we click on a location row, save the location |
|
|
|
|
|
|
|
placesForRows[row]?.place?.let { place -> |
|
|
|
|
|
|
|
location.setPlace(place) |
|
|
|
|
|
|
|
saveData() |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
when (row) { |
|
|
|
when (row) { |
|
|
|
LocationRow.LOCATE_ME -> { |
|
|
|
LocationRow.LOCATE_ME -> { |
|
|
|
isLookingForPlaces = true |
|
|
|
isLookingForPlaces = true |
|
|
|
@ -80,11 +112,108 @@ class LocationDataFragment : EditableDataFragment(), StaticRowRepresentableDataS |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
|
|
|
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
|
|
|
super.onRowValueChanged(value, row) |
|
|
|
|
|
|
|
when (row) { |
|
|
|
when (row) { |
|
|
|
LocationRow.LOCATE_ME -> { |
|
|
|
LocationRow.LOCATION_PERMISSION_SWITCH -> { |
|
|
|
|
|
|
|
if (value is Boolean && value != locationActivated) { |
|
|
|
|
|
|
|
rowPlaces.clear() |
|
|
|
|
|
|
|
locationActivated = value |
|
|
|
|
|
|
|
isLookingForPlaces = value |
|
|
|
|
|
|
|
if (value) { |
|
|
|
|
|
|
|
updateAdapterUI() |
|
|
|
|
|
|
|
getSuggestionsPlaces() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
clearCurrentLocation() |
|
|
|
|
|
|
|
updateAdapterUI() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else -> super.onRowValueChanged(value, row) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Clear current location |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun clearCurrentLocation() { |
|
|
|
|
|
|
|
location.latitude = null |
|
|
|
|
|
|
|
location.longitude = null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Refresh rows |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun refreshRows() { |
|
|
|
|
|
|
|
rows.clear() |
|
|
|
|
|
|
|
rows.add(SimpleRow.NAME) |
|
|
|
|
|
|
|
rows.add(LocationRow.LOCATION_PERMISSION_SWITCH) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isLookingForPlaces) { |
|
|
|
|
|
|
|
rows.add(LocationRow.LOCATION_LOADER) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (locationActivated && rowPlaces.size > 0) { |
|
|
|
|
|
|
|
rows.add(CustomizableRowRepresentable(resId = R.string.suggestions)) |
|
|
|
|
|
|
|
for (row in rowPlaces) { |
|
|
|
|
|
|
|
rows.add(row) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Update UI adapter |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun updateAdapterUI() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val currentRowsSize = rows.size |
|
|
|
|
|
|
|
refreshRows() |
|
|
|
|
|
|
|
val newRowsSize = rows.size |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (currentRowsSize < newRowsSize) { |
|
|
|
|
|
|
|
rowRepresentableAdapter.notifyItemRangeInserted(currentRowsSize, newRowsSize - currentRowsSize) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
rowRepresentableAdapter.notifyItemRangeRemoved(newRowsSize, currentRowsSize - newRowsSize) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Return the places around the user |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun getSuggestionsPlaces() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val maxResults = 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parentActivity.askForPlacesRequest { success, places -> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (success) { |
|
|
|
|
|
|
|
// Try to get the location of the user |
|
|
|
|
|
|
|
parentActivity.findCurrentLocation {currentLocation -> |
|
|
|
|
|
|
|
currentLocation?.let { |
|
|
|
|
|
|
|
Timber.d("Current location: ${it.latitude}, ${it.longitude}") |
|
|
|
|
|
|
|
location.latitude = currentLocation.latitude |
|
|
|
|
|
|
|
location.longitude = currentLocation.longitude |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (success && places.size > 0) { |
|
|
|
|
|
|
|
locationActivated = true |
|
|
|
|
|
|
|
rowPlaces.clear() |
|
|
|
|
|
|
|
placesForRows.clear() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for ((index, place) in places.withIndex()) { |
|
|
|
|
|
|
|
if (index < maxResults) { |
|
|
|
|
|
|
|
val row = CustomizableRowRepresentable(customViewType = RowViewType.LOCATION_TITLE, title = place.place.name, isSelectable = true) |
|
|
|
|
|
|
|
rowPlaces.add(row) |
|
|
|
|
|
|
|
placesForRows[row] = place |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isLookingForPlaces = false |
|
|
|
|
|
|
|
updateAdapterUI() |
|
|
|
|
|
|
|
} else { |
|
|
|
isLookingForPlaces = false |
|
|
|
isLookingForPlaces = false |
|
|
|
rowRepresentableAdapter.notifyDataSetChanged() |
|
|
|
locationActivated = false |
|
|
|
|
|
|
|
updateAdapterUI() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|