parent
5848f7df22
commit
570cacdffe
@ -1,70 +0,0 @@ |
|||||||
package net.pokeranalytics.android.ui.modules.filter |
|
||||||
|
|
||||||
import android.content.Context |
|
||||||
import android.content.Intent |
|
||||||
import android.os.Bundle |
|
||||||
import androidx.fragment.app.Fragment |
|
||||||
import net.pokeranalytics.android.R |
|
||||||
import net.pokeranalytics.android.ui.activity.components.BaseActivity |
|
||||||
|
|
||||||
class FilterDetailsActivity : BaseActivity() { |
|
||||||
|
|
||||||
enum class IntentKey(val keyName: String) { |
|
||||||
FILTER_ID("FILTER_ID"), |
|
||||||
FILTER_CATEGORY_ORDINAL("FILTER_CATEGORY_ORDINAL") |
|
||||||
} |
|
||||||
|
|
||||||
companion object { |
|
||||||
|
|
||||||
/** |
|
||||||
* Default constructor |
|
||||||
*/ |
|
||||||
fun newInstance(context: Context, filterId: String, filterCategoryOrdinal: Int) { |
|
||||||
val intent = Intent(context, FilterDetailsActivity::class.java) |
|
||||||
intent.putExtra(IntentKey.FILTER_ID.keyName, filterId) |
|
||||||
intent.putExtra(IntentKey.FILTER_CATEGORY_ORDINAL.keyName, filterCategoryOrdinal) |
|
||||||
context.startActivity(intent) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create a new instance for result |
|
||||||
*/ |
|
||||||
fun newInstanceForResult(fragment: Fragment, filterId: String, filterCategoryOrdinal: Int, requestCode: Int) { |
|
||||||
|
|
||||||
val intent = Intent(fragment.requireContext(), FilterDetailsActivity::class.java) |
|
||||||
intent.putExtra(IntentKey.FILTER_ID.keyName, filterId) |
|
||||||
intent.putExtra(IntentKey.FILTER_CATEGORY_ORDINAL.keyName, filterCategoryOrdinal) |
|
||||||
fragment.startActivityForResult(intent, requestCode) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
private lateinit var fragment: FilterDetailsFragment |
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) { |
|
||||||
super.onCreate(savedInstanceState) |
|
||||||
setContentView(R.layout.activity_filter_details) |
|
||||||
initUI() |
|
||||||
} |
|
||||||
|
|
||||||
override fun onBackPressed() { |
|
||||||
fragment.onBackPressed() |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Init UI |
|
||||||
*/ |
|
||||||
private fun initUI() { |
|
||||||
|
|
||||||
val fragmentManager = supportFragmentManager |
|
||||||
val fragmentTransaction = fragmentManager.beginTransaction() |
|
||||||
val filterId = intent.getStringExtra(IntentKey.FILTER_ID.keyName) |
|
||||||
val filterCategoryOrdinal = intent.getIntExtra(IntentKey.FILTER_CATEGORY_ORDINAL.keyName, 0) |
|
||||||
|
|
||||||
fragment = |
|
||||||
FilterDetailsFragment() |
|
||||||
fragmentTransaction.add(R.id.container, fragment) |
|
||||||
fragmentTransaction.commit() |
|
||||||
fragment.setData(filterId, filterCategoryOrdinal) |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,76 @@ |
|||||||
|
package net.pokeranalytics.android.ui.modules.filter |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import androidx.lifecycle.ViewModel |
||||||
|
import io.realm.Realm |
||||||
|
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
||||||
|
import net.pokeranalytics.android.model.realm.Filter |
||||||
|
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource |
||||||
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||||
|
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow |
||||||
|
import net.pokeranalytics.android.util.extensions.findById |
||||||
|
|
||||||
|
class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource { |
||||||
|
|
||||||
|
var currentFilter: Filter? = null |
||||||
|
|
||||||
|
// Main |
||||||
|
var filterableType: FilterableType? = null |
||||||
|
|
||||||
|
var filterCopy: Filter? = null |
||||||
|
private var categoryRows: ArrayList<RowRepresentable> = ArrayList() |
||||||
|
var primaryKey: String? = null |
||||||
|
var selectedCategoryRow: RowRepresentable? = null |
||||||
|
var isUpdating = false |
||||||
|
|
||||||
|
// Details |
||||||
|
val filterCategoryRow: FilterCategoryRow |
||||||
|
get() { |
||||||
|
return this.selectedCategoryRow as FilterCategoryRow |
||||||
|
} |
||||||
|
|
||||||
|
fun init(realm: Realm) { |
||||||
|
|
||||||
|
if (this.currentFilter != null) { // can be called twice and we don't want that |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
this.primaryKey?.let { |
||||||
|
|
||||||
|
val filter = realm.findById<Filter>(it) ?: throw PAIllegalStateException("Can't find filter with id=$it") |
||||||
|
this.currentFilter = realm.copyFromRealm(filter) |
||||||
|
this.isUpdating = true |
||||||
|
} ?: run { |
||||||
|
this.filterableType?.uniqueIdentifier?.let { |
||||||
|
this.currentFilter = Filter.newInstance(it) //realm.copyFromRealm(Filter.newInstanceForResult(realm, this.filterableType.ordinal)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Create a copy if the user cancels the updates |
||||||
|
this.currentFilter?.let { |
||||||
|
if (it.isValid && it.isManaged) { |
||||||
|
this.filterCopy = realm.copyFromRealm(it) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
this.categoryRows.clear() |
||||||
|
|
||||||
|
this.filterableType?.let { |
||||||
|
this.categoryRows.addAll(FilterCategoryRow.values(it)) |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
// Data source |
||||||
|
|
||||||
|
override fun adapterRows(): List<RowRepresentable>? { |
||||||
|
return this.categoryRows |
||||||
|
} |
||||||
|
|
||||||
|
override fun charSequenceForRow(row: RowRepresentable, context: Context, tag: Int): CharSequence { |
||||||
|
// Return the number of selected filters for this category |
||||||
|
val count = this.currentFilter?.countBy(row as FilterCategoryRow) ?: 0 |
||||||
|
return if (count > 0) count.toString() else "" |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue