|
|
|
|
@ -5,7 +5,6 @@ import android.content.Context |
|
|
|
|
import android.content.Intent |
|
|
|
|
import android.os.Bundle |
|
|
|
|
import android.view.LayoutInflater |
|
|
|
|
import android.view.Menu |
|
|
|
|
import android.view.View |
|
|
|
|
import android.view.ViewGroup |
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager |
|
|
|
|
@ -22,33 +21,26 @@ import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
|
|
|
|
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetFragment |
|
|
|
|
import net.pokeranalytics.android.ui.helpers.DateTimePickerManager |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterSectionRow |
|
|
|
|
import net.pokeranalytics.android.util.NULL_TEXT |
|
|
|
|
import net.pokeranalytics.android.util.extensions.shortDate |
|
|
|
|
import net.pokeranalytics.android.util.extensions.shortTime |
|
|
|
|
import net.pokeranalytics.android.util.extensions.toMinutes |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import java.util.* |
|
|
|
|
import kotlin.collections.ArrayList |
|
|
|
|
|
|
|
|
|
open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate { |
|
|
|
|
open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate { |
|
|
|
|
|
|
|
|
|
lateinit var rowRepresentableAdapter: RowRepresentableAdapter |
|
|
|
|
private lateinit var primaryKey: String |
|
|
|
|
private lateinit var filterCategoryRow: FilterCategoryRow |
|
|
|
|
private lateinit var primaryKey: String |
|
|
|
|
private lateinit var filterCategoryRow: FilterCategoryRow |
|
|
|
|
|
|
|
|
|
private var currentFilter: Filter? = null |
|
|
|
|
private var rows: ArrayList<RowRepresentable> = ArrayList() |
|
|
|
|
private var rowsForFilterSubcategoryRow: HashMap<FilterSectionRow, ArrayList<RowRepresentable>> = HashMap() |
|
|
|
|
|
|
|
|
|
private var filterMenu: Menu? = null |
|
|
|
|
private val selectedRows = ArrayList<QueryCondition>() |
|
|
|
|
private var isUpdating = false |
|
|
|
|
private var shouldOpenKeyboard = true |
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState) |
|
|
|
|
@ -63,49 +55,54 @@ open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataS |
|
|
|
|
|
|
|
|
|
override fun onBackPressed() { |
|
|
|
|
super.onBackPressed() |
|
|
|
|
println("<<<<< back pressed") |
|
|
|
|
println("<<<<< back pressed") |
|
|
|
|
saveData() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
|
|
|
|
super.onRowSelected(position, row, fromAction) |
|
|
|
|
|
|
|
|
|
Timber.d("Row: $row") |
|
|
|
|
|
|
|
|
|
if (row.viewType == RowViewType.TITLE_CHECK.ordinal) { |
|
|
|
|
updateRowsSelection(row) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (row.viewType == RowViewType.TITLE_CHECK.ordinal) { |
|
|
|
|
updateRowsSelection(row) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
when (row) { |
|
|
|
|
is QueryCondition.DateQuery -> DateTimePickerManager.create(requireContext(), row, this, row.singleValue, onlyDate = !row.showTime, onlyTime = row.showTime) |
|
|
|
|
is QueryCondition.DateQuery -> DateTimePickerManager.create( |
|
|
|
|
requireContext(), |
|
|
|
|
row, |
|
|
|
|
this, |
|
|
|
|
row.singleValue, |
|
|
|
|
onlyDate = !row.showTime, |
|
|
|
|
onlyTime = row.showTime |
|
|
|
|
) |
|
|
|
|
is QueryCondition.Duration -> { |
|
|
|
|
var hours: String? = null |
|
|
|
|
var minutes: String? = null |
|
|
|
|
row.minutes?.let { |
|
|
|
|
hours = if (it / 60 > 0) (it / 60).toString() else null |
|
|
|
|
minutes = if (it % 60 > 0) (it % 60).toString() else null |
|
|
|
|
} |
|
|
|
|
val data = row.editingDescriptors(mapOf("hours" to hours, "minutes" to minutes)) |
|
|
|
|
var hours: String? = null |
|
|
|
|
var minutes: String? = null |
|
|
|
|
row.minutes?.let { |
|
|
|
|
hours = if (it / 60 > 0) (it / 60).toString() else null |
|
|
|
|
minutes = if (it % 60 > 0) (it % 60).toString() else null |
|
|
|
|
} |
|
|
|
|
val data = row.editingDescriptors(mapOf("hours" to hours, "minutes" to minutes)) |
|
|
|
|
BottomSheetFragment.create(fragmentManager, row, this, data, true) |
|
|
|
|
} |
|
|
|
|
is QueryCondition.ListOfValues<*> -> { |
|
|
|
|
var valueAsString: String? = null |
|
|
|
|
row.listOfValues.firstOrNull()?.let { |
|
|
|
|
valueAsString = row.listOfValues.firstOrNull()?.toString() |
|
|
|
|
} |
|
|
|
|
val data = row.editingDescriptors(mapOf("valueAsString" to valueAsString)) |
|
|
|
|
BottomSheetFragment.create(fragmentManager, row, this, data, true) |
|
|
|
|
} |
|
|
|
|
is QueryCondition.ListOfValues<*> -> { |
|
|
|
|
var valueAsString: String? = null |
|
|
|
|
row.listOfValues.firstOrNull()?.let { |
|
|
|
|
valueAsString = row.listOfValues.firstOrNull()?.toString() |
|
|
|
|
} |
|
|
|
|
val data = row.editingDescriptors(mapOf("valueAsString" to valueAsString)) |
|
|
|
|
BottomSheetFragment.create(fragmentManager, row, this, data, true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun stringForRow(row: RowRepresentable, context: Context): String { |
|
|
|
|
return when (row) { |
|
|
|
|
is QueryCondition.ListOfValues<*> -> row.firstValue(context) |
|
|
|
|
else -> super.stringForRow(row) |
|
|
|
|
} ?: NULL_TEXT |
|
|
|
|
} |
|
|
|
|
return when (row) { |
|
|
|
|
is QueryCondition.ListOfValues<*> -> row.firstValue(context) |
|
|
|
|
else -> super.stringForRow(row) |
|
|
|
|
} ?: NULL_TEXT |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun isSelected(row: RowRepresentable): Boolean { |
|
|
|
|
return selectedRows.contains(row) |
|
|
|
|
@ -125,32 +122,32 @@ open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataS |
|
|
|
|
null |
|
|
|
|
} |
|
|
|
|
val minutes = try { |
|
|
|
|
(value[1] as String?)?.toInt() |
|
|
|
|
(value[1] as String?)?.toInt() |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
null |
|
|
|
|
} |
|
|
|
|
if (hours != null && minutes != null) { |
|
|
|
|
row.minutes = hours * 60 + minutes |
|
|
|
|
} else if (hours != null) { |
|
|
|
|
row.minutes = hours * 60 |
|
|
|
|
} else if (minutes != null) { |
|
|
|
|
row.minutes = minutes |
|
|
|
|
} |
|
|
|
|
if (hours != null && minutes != null) { |
|
|
|
|
row.minutes = hours * 60 + minutes |
|
|
|
|
} else if (hours != null) { |
|
|
|
|
row.minutes = hours * 60 |
|
|
|
|
} else if (minutes != null) { |
|
|
|
|
row.minutes = minutes |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
row.minutes = null |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
is QueryCondition.SingleInt -> row.singleValue = if (value != null && value is String) value.toInt() else null |
|
|
|
|
is QueryCondition.ListOfDouble-> row.listOfValues = arrayListOf<Double>().apply { |
|
|
|
|
if (value != null && value is String) this.add(value.toDouble()) |
|
|
|
|
} |
|
|
|
|
is QueryCondition.ListOfInt-> row.listOfValues = arrayListOf<Int>().apply { |
|
|
|
|
if (value != null && value is String) this.add(value.toInt()) |
|
|
|
|
} |
|
|
|
|
is QueryCondition.ListOfString-> row.listOfValues = arrayListOf<String>().apply { |
|
|
|
|
if (value != null && value is String) this.add(value) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
is QueryCondition.SingleInt -> row.singleValue = if (value != null && value is String) value.toInt() else null |
|
|
|
|
is QueryCondition.ListOfDouble -> row.listOfValues = arrayListOf<Double>().apply { |
|
|
|
|
if (value != null && value is String) this.add(value.toDouble()) |
|
|
|
|
} |
|
|
|
|
is QueryCondition.ListOfInt -> row.listOfValues = arrayListOf<Int>().apply { |
|
|
|
|
if (value != null && value is String) this.add(value.toInt()) |
|
|
|
|
} |
|
|
|
|
is QueryCondition.ListOfString -> row.listOfValues = arrayListOf<String>().apply { |
|
|
|
|
if (value != null && value is String) this.add(value) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Remove the row before updating the selected rows list |
|
|
|
|
selectedRows.remove(row as FilterElementRow) |
|
|
|
|
@ -189,24 +186,24 @@ open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataS |
|
|
|
|
*/ |
|
|
|
|
private fun initData() { |
|
|
|
|
|
|
|
|
|
currentFilter = Filter.getFilterBydId(getRealm(), primaryKey) |
|
|
|
|
//currentFilter = Filter.getFilterBydId(getRealm(), primaryKey) |
|
|
|
|
currentFilter = FiltersFragment.currentFilter |
|
|
|
|
|
|
|
|
|
this.appBar.toolbar.title = filterCategoryRow.localizedTitle(requireContext()) |
|
|
|
|
|
|
|
|
|
this.appBar.toolbar.title = filterCategoryRow.localizedTitle(requireContext()) |
|
|
|
|
this.rows.clear() |
|
|
|
|
this.rowsForFilterSubcategoryRow.clear() |
|
|
|
|
this.rows.addAll(filterCategoryRow.filterElements) |
|
|
|
|
|
|
|
|
|
this.rows.clear() |
|
|
|
|
this.rowsForFilterSubcategoryRow.clear() |
|
|
|
|
this.rows.addAll(filterCategoryRow.filterElements) |
|
|
|
|
|
|
|
|
|
this.rows.forEach { element -> |
|
|
|
|
if (element is QueryCondition && currentFilter?.contains(element) == true) { |
|
|
|
|
currentFilter?.loadValueForElement(element) |
|
|
|
|
this.selectedRows.add(element) |
|
|
|
|
} |
|
|
|
|
this.rows.forEach { element -> |
|
|
|
|
if (element is QueryCondition && currentFilter?.contains(element) == true) { |
|
|
|
|
currentFilter?.loadValueForElement(element) |
|
|
|
|
this.selectedRows.add(element) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.rowRepresentableAdapter = RowRepresentableAdapter(this, this) |
|
|
|
|
this.recyclerView.adapter = rowRepresentableAdapter |
|
|
|
|
this.rowRepresentableAdapter = RowRepresentableAdapter(this, this) |
|
|
|
|
this.recyclerView.adapter = rowRepresentableAdapter |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -231,7 +228,7 @@ open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataS |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
println("list of selected rows : $selectedRows") |
|
|
|
|
println("list of selected rows : $selectedRows") |
|
|
|
|
|
|
|
|
|
// Update UI |
|
|
|
|
rowRepresentableAdapter.refreshRow(row) |
|
|
|
|
@ -249,7 +246,7 @@ open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataS |
|
|
|
|
|
|
|
|
|
val realm = getRealm() |
|
|
|
|
realm.beginTransaction() |
|
|
|
|
currentFilter?.remove(filterCategoryRow) |
|
|
|
|
currentFilter?.remove(filterCategoryRow) |
|
|
|
|
currentFilter?.createOrUpdateFilterConditions(selectedRows) |
|
|
|
|
realm.commitTransaction() |
|
|
|
|
|
|
|
|
|
@ -257,7 +254,6 @@ open class FilterDetailsFragment : RealmFragment(), StaticRowRepresentableDataS |
|
|
|
|
Timber.d("Condition: $it") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
finishActivityWithResult(currentFilter?.id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|