|
|
|
|
@ -12,7 +12,9 @@ import kotlinx.android.synthetic.main.fragment_editable_data.recyclerView |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_filters.* |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_filters.view.toolbar |
|
|
|
|
import net.pokeranalytics.android.R |
|
|
|
|
import net.pokeranalytics.android.model.LiveData |
|
|
|
|
import net.pokeranalytics.android.model.realm.Filter |
|
|
|
|
import net.pokeranalytics.android.ui.activity.DataListActivity |
|
|
|
|
import net.pokeranalytics.android.ui.activity.FilterDetailsActivity |
|
|
|
|
import net.pokeranalytics.android.ui.activity.FiltersActivity |
|
|
|
|
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter |
|
|
|
|
@ -20,6 +22,7 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate |
|
|
|
|
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource |
|
|
|
|
import net.pokeranalytics.android.ui.extensions.px |
|
|
|
|
import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
|
|
|
|
import net.pokeranalytics.android.ui.interfaces.FilterActivityRequestCode |
|
|
|
|
import net.pokeranalytics.android.ui.interfaces.FilterableType |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow |
|
|
|
|
@ -32,11 +35,13 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
const val REQUEST_CODE_FILTER_DETAILS = 100 |
|
|
|
|
|
|
|
|
|
var currentFilter: Filter? = null |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private lateinit var rowRepresentableAdapter: RowRepresentableAdapter |
|
|
|
|
|
|
|
|
|
private var currentFilter: Filter? = null |
|
|
|
|
private var filterCopy: Filter? = null |
|
|
|
|
|
|
|
|
|
private var rows: ArrayList<RowRepresentable> = ArrayList() |
|
|
|
|
@ -46,6 +51,7 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
|
|
|
|
|
private var selectedRow: RowRepresentable? = null |
|
|
|
|
private var isUpdating = false |
|
|
|
|
private var isSaved = false |
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState) |
|
|
|
|
@ -56,24 +62,37 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
super.onViewCreated(view, savedInstanceState) |
|
|
|
|
initUI() |
|
|
|
|
initData() |
|
|
|
|
updateMostUsedFilters() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
|
|
|
|
super.onActivityResult(requestCode, resultCode, data) |
|
|
|
|
|
|
|
|
|
Timber.d("requestCode: $requestCode") |
|
|
|
|
Timber.d("resultCode: $resultCode") |
|
|
|
|
Timber.d("data: $data") |
|
|
|
|
|
|
|
|
|
if (requestCode == REQUEST_CODE_FILTER_DETAILS && resultCode == RESULT_OK) { |
|
|
|
|
|
|
|
|
|
// Update object |
|
|
|
|
/* |
|
|
|
|
Timber.d("onActivityResult: $requestCode") |
|
|
|
|
if (data != null && data.hasExtra(FilterDetailsActivity.IntentKey.FILTER_ID.keyName)) { |
|
|
|
|
val filterId = data.getStringExtra(FilterDetailsActivity.IntentKey.FILTER_ID.keyName) |
|
|
|
|
Timber.d("Updated queryWith: ${filterId}") |
|
|
|
|
currentFilter?.id?.let { currentFilterId -> |
|
|
|
|
Filter.getFilterBydId(getRealm(), currentFilterId)?.let { filter -> |
|
|
|
|
currentFilter = filter |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
selectedRow?.let { |
|
|
|
|
rowRepresentableAdapter.refreshRow(it) |
|
|
|
|
} |
|
|
|
|
} else if (requestCode == FilterActivityRequestCode.SELECT_FILTER.ordinal && resultCode == RESULT_OK) { |
|
|
|
|
if (data != null && data.hasExtra(FiltersActivity.IntentKey.FILTER_ID.keyName)) { |
|
|
|
|
val filterId = data.getStringExtra(FiltersActivity.IntentKey.FILTER_ID.keyName) |
|
|
|
|
finishActivityWithResult(filterId) |
|
|
|
|
} else { |
|
|
|
|
updateMostUsedFilters() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -96,7 +115,6 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem?): Boolean { |
|
|
|
|
when (item!!.itemId) { |
|
|
|
|
R.id.save -> validateUpdates() |
|
|
|
|
R.id.delete -> deleteFilter() |
|
|
|
|
} |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
@ -142,48 +160,9 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
layoutManager = viewManager |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val filters = getRealm().sorted(Filter::class.java) |
|
|
|
|
val currentFilterId = Preferences.getActiveFilterId(requireContext()) |
|
|
|
|
|
|
|
|
|
mostUsedFilters.removeAllViews() |
|
|
|
|
filters.forEachIndexed { index, filter -> |
|
|
|
|
if (index < 5) { |
|
|
|
|
val chip = Chip(requireContext()) |
|
|
|
|
chip.id = View.generateViewId() |
|
|
|
|
chip.tag = filter.id |
|
|
|
|
chip.text = filter.getDisplayName(requireContext()) |
|
|
|
|
chip.chipStartPadding = 8f.px |
|
|
|
|
chip.chipEndPadding = 8f.px |
|
|
|
|
|
|
|
|
|
chip.isCloseIconVisible = true |
|
|
|
|
chip.isChecked = filter.id == currentFilterId |
|
|
|
|
chip.setOnCloseIconClickListener { |
|
|
|
|
Preferences.removeActiveFilterId(requireContext()) |
|
|
|
|
chip.isChecked = false |
|
|
|
|
} |
|
|
|
|
chip.setOnClickListener { |
|
|
|
|
deleteFilter(false) |
|
|
|
|
finishActivityWithResult(filter.id) |
|
|
|
|
} |
|
|
|
|
mostUsedFilters.addView(chip) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
Limit.values().forEach { |
|
|
|
|
val chip = Chip(requireContext()) |
|
|
|
|
chip.id = it.ordinal |
|
|
|
|
chip.text = it.shortName |
|
|
|
|
chip.chipStartPadding = 8f.px |
|
|
|
|
chip.chipEndPadding = 8f.px |
|
|
|
|
chip.isChecked = false |
|
|
|
|
mostUsedFilters.addView(chip) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mostUsedFilters.setOnCheckedChangeListener { _, i -> |
|
|
|
|
moreFilters.setOnClickListener { |
|
|
|
|
DataListActivity.newSelectInstance(this, LiveData.FILTER.ordinal) |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -198,18 +177,20 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
Timber.d("Filters: ${filters.size}") |
|
|
|
|
|
|
|
|
|
primaryKey?.let { |
|
|
|
|
currentFilter = Filter.getFilterBydId(realm, it) |
|
|
|
|
currentFilter = realm.copyFromRealm(Filter.getFilterBydId(realm, it)) |
|
|
|
|
isUpdating = true |
|
|
|
|
} ?: run { |
|
|
|
|
realm.beginTransaction() |
|
|
|
|
currentFilter = Filter.newInstance(realm, this.filterableType.ordinal) |
|
|
|
|
currentFilter = Filter.newInstance(realm, this.filterableType.ordinal) //realm.copyFromRealm(Filter.newInstance(realm, this.filterableType.ordinal)) |
|
|
|
|
realm.commitTransaction() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a copy if the user cancels the updates |
|
|
|
|
currentFilter?.let { |
|
|
|
|
if (it.isValid && it.isManaged) { |
|
|
|
|
filterCopy = getRealm().copyFromRealm(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rows.clear() |
|
|
|
|
rows.addAll(FilterCategoryRow.values(this.filterableType)) |
|
|
|
|
@ -219,12 +200,55 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update the most used filters chips |
|
|
|
|
*/ |
|
|
|
|
private fun updateMostUsedFilters() { |
|
|
|
|
|
|
|
|
|
if (isUpdating) { |
|
|
|
|
mostUsedFiltersLayout.visibility = View.GONE |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val filters = getRealm().sorted(Filter::class.java) |
|
|
|
|
val currentFilterId = Preferences.getActiveFilterId(requireContext()) |
|
|
|
|
|
|
|
|
|
mostUsedFilters.removeAllViews() |
|
|
|
|
filters.forEachIndexed { index, filter -> |
|
|
|
|
if (index < 3) { |
|
|
|
|
val chip = Chip(requireContext()) |
|
|
|
|
chip.id = View.generateViewId() |
|
|
|
|
chip.tag = filter.id |
|
|
|
|
chip.text = filter.getDisplayName(requireContext()) |
|
|
|
|
chip.chipStartPadding = 8f.px |
|
|
|
|
chip.chipEndPadding = 8f.px |
|
|
|
|
|
|
|
|
|
chip.isChecked = filter.id == currentFilterId |
|
|
|
|
chip.setOnCloseIconClickListener { |
|
|
|
|
|
|
|
|
|
chip.isChecked = false |
|
|
|
|
} |
|
|
|
|
chip.setOnClickListener { |
|
|
|
|
if (chip.isChecked) { |
|
|
|
|
deleteFilter(false) |
|
|
|
|
finishActivityWithResult(filter.id) |
|
|
|
|
} else { |
|
|
|
|
Preferences.removeActiveFilterId(requireContext()) |
|
|
|
|
deleteFilter(true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
mostUsedFilters.addView(chip) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update menu UI |
|
|
|
|
*/ |
|
|
|
|
private fun updateMenuUI() { |
|
|
|
|
filterMenu?.findItem(R.id.delete)?.isVisible = isUpdating |
|
|
|
|
filterMenu?.findItem(R.id.save)?.isVisible = true |
|
|
|
|
filterMenu?.findItem(R.id.delete)?.isVisible = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -232,6 +256,16 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
*/ |
|
|
|
|
private fun validateUpdates() { |
|
|
|
|
Timber.d("Validate queryWith updates") |
|
|
|
|
|
|
|
|
|
val realm = getRealm() |
|
|
|
|
realm.beginTransaction() |
|
|
|
|
currentFilter?.let { |
|
|
|
|
it.name = it.query.getName(requireContext()) |
|
|
|
|
Timber.d("name: ${it.name}") |
|
|
|
|
realm.copyToRealmOrUpdate(it) |
|
|
|
|
} |
|
|
|
|
realm.commitTransaction() |
|
|
|
|
|
|
|
|
|
val filterId = currentFilter?.id ?: "" |
|
|
|
|
finishActivityWithResult(filterId) |
|
|
|
|
} |
|
|
|
|
@ -258,10 +292,12 @@ open class FiltersFragment : RealmFragment(), StaticRowRepresentableDataSource, |
|
|
|
|
*/ |
|
|
|
|
private fun deleteFilter(closeActivity: Boolean = true) { |
|
|
|
|
Timber.d("Delete queryWith") |
|
|
|
|
if (currentFilter?.isValid == true && currentFilter?.isManaged == true) { |
|
|
|
|
val realm = getRealm() |
|
|
|
|
realm.beginTransaction() |
|
|
|
|
currentFilter?.deleteFromRealm() |
|
|
|
|
realm.commitTransaction() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (closeActivity) { |
|
|
|
|
finishActivityWithResult("") |
|
|
|
|
|