parent
25d2fdac15
commit
c94b42abe8
@ -0,0 +1,58 @@ |
||||
package net.pokeranalytics.android.ui.activity |
||||
|
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import android.os.Bundle |
||||
import androidx.fragment.app.Fragment |
||||
import kotlinx.android.synthetic.main.activity_filters_list.* |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity |
||||
import net.pokeranalytics.android.ui.fragment.FiltersListFragment |
||||
import net.pokeranalytics.android.ui.interfaces.FilterActivityRequestCode |
||||
|
||||
class FiltersListActivity : PokerAnalyticsActivity() { |
||||
|
||||
enum class IntentKey(val keyName: String) { |
||||
DATA_TYPE("DATA_TYPE"), |
||||
LIVE_DATA_TYPE("LIVE_DATA_TYPE"), |
||||
ITEM_DELETED("ITEM_DELETED"), |
||||
SHOW_ADD_BUTTON("SHOW_ADD_BUTTON"), |
||||
} |
||||
|
||||
companion object { |
||||
fun newInstance(context: Context, dataType: Int) { |
||||
context.startActivity(getIntent(context, dataType)) |
||||
} |
||||
|
||||
fun newSelectInstance(fragment: Fragment, dataType: Int, showAddButton: Boolean = true) { |
||||
val context = fragment.requireContext() |
||||
fragment.startActivityForResult(getIntent(context, dataType, showAddButton), FilterActivityRequestCode.SELECT_FILTER.ordinal) |
||||
} |
||||
|
||||
private fun getIntent(context: Context, dataType: Int, showAddButton: Boolean = true): Intent { |
||||
val intent = Intent(context, FiltersListActivity::class.java) |
||||
intent.putExtra(IntentKey.DATA_TYPE.keyName, dataType) |
||||
intent.putExtra(IntentKey.SHOW_ADD_BUTTON.keyName, showAddButton) |
||||
return intent |
||||
} |
||||
} |
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
setContentView(R.layout.activity_filters_list) |
||||
|
||||
initUI() |
||||
} |
||||
|
||||
/** |
||||
* Init UI |
||||
*/ |
||||
private fun initUI() { |
||||
val dataType = intent.getIntExtra(IntentKey.DATA_TYPE.keyName, 0) |
||||
val showAddButton = intent.getBooleanExtra(IntentKey.SHOW_ADD_BUTTON.keyName, true) |
||||
val fragment = filtersListFragment as FiltersListFragment |
||||
fragment.setData(dataType) |
||||
fragment.updateUI(showAddButton) |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,122 @@ |
||||
package net.pokeranalytics.android.ui.fragment |
||||
|
||||
import android.app.Activity |
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import io.realm.RealmResults |
||||
import net.pokeranalytics.android.model.LiveData |
||||
import net.pokeranalytics.android.model.interfaces.Deletable |
||||
import net.pokeranalytics.android.model.interfaces.Identifiable |
||||
import net.pokeranalytics.android.model.realm.Filter |
||||
import net.pokeranalytics.android.ui.activity.EditableDataActivity |
||||
import net.pokeranalytics.android.ui.activity.FiltersActivity |
||||
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetFragment |
||||
import net.pokeranalytics.android.ui.interfaces.FilterHandler.Companion.INTENT_FILTER_UPDATE_FILTER_UI |
||||
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.util.Preferences |
||||
import timber.log.Timber |
||||
|
||||
|
||||
open class FiltersListFragment : DataListFragment() { |
||||
|
||||
private var identifiableClass: Class<out Deletable> = Filter::class.java |
||||
private var dataType: LiveData = LiveData.FILTER |
||||
private lateinit var items: RealmResults<Filter> |
||||
|
||||
/** |
||||
* Set fragment data |
||||
*/ |
||||
override fun setData(dataType: Int) { |
||||
super.setData(dataType) |
||||
|
||||
this.dataType = LiveData.FILTER |
||||
this.identifiableClass = Filter::class.java |
||||
setToolbarTitle(this.dataType.pluralLocalizedTitle(requireContext())) |
||||
this.items = this.retrieveItems(getRealm()) as RealmResults<Filter> |
||||
} |
||||
|
||||
override fun rowRepresentableForPosition(position: Int): RowRepresentable? { |
||||
Timber.d("rowRepresentableForPosition: ${this.items[position] as RowRepresentable}") |
||||
return this.items[position] as RowRepresentable |
||||
} |
||||
|
||||
override fun numberOfRows(): Int { |
||||
return this.items.size |
||||
} |
||||
|
||||
override fun adapterRows(): List<RowRepresentable>? { |
||||
return items |
||||
} |
||||
|
||||
override fun viewTypeForPosition(position: Int): Int { |
||||
val viewType = (this.items[position] as RowRepresentable).viewType |
||||
return if (viewType != -1) viewType else RowViewType.DATA.ordinal |
||||
} |
||||
|
||||
override fun editDescriptors(row: RowRepresentable): ArrayList<RowRepresentableEditDescriptor>? { |
||||
return when (row) { |
||||
is Filter -> row.editingDescriptors(mapOf("defaultValue" to row.name)) |
||||
else -> super.editDescriptors(row) |
||||
} |
||||
} |
||||
|
||||
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
||||
when (row) { |
||||
is Filter -> { |
||||
row.updateValue(value, row) |
||||
dataListAdapter.refreshRow(row) |
||||
updateFilterUIIfNecessary(requireContext(), row.id) |
||||
} |
||||
else -> super.onRowValueChanged(value, row) |
||||
} |
||||
} |
||||
|
||||
override fun onRowDeleted(row: RowRepresentable) { |
||||
when (row) { |
||||
is Filter -> { |
||||
val filterId = row.id |
||||
deleteItem(dataListAdapter, items, filterId) |
||||
if (filterId == Preferences.getActiveFilterId(requireContext())) { |
||||
Preferences.setActiveFilterId("", requireContext()) |
||||
updateFilterUIIfNecessary(requireContext(), "") |
||||
} |
||||
} |
||||
else -> super.onRowDeleted(row) |
||||
} |
||||
} |
||||
|
||||
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
||||
when (row) { |
||||
is Filter -> { |
||||
if (fromAction) { |
||||
val data = row.editingDescriptors(mapOf("defaultValue" to row.name)) |
||||
BottomSheetFragment.create(fragmentManager, row, this, data, false, isDeletable = true, valueHasPlaceholder = false) |
||||
} else { |
||||
val intent = Intent() |
||||
intent.putExtra(FiltersActivity.IntentKey.FILTER_ID.keyName, row.id) |
||||
activity?.setResult(Activity.RESULT_OK, intent) |
||||
activity?.finish() |
||||
} |
||||
} |
||||
else -> { |
||||
val identifier = (row as Identifiable).id |
||||
EditableDataActivity.newInstanceForResult(this, this.dataType, identifier, REQUEST_CODE_DETAILS) |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Update filter UI |
||||
*/ |
||||
fun updateFilterUIIfNecessary(context: Context, filterId: String) { |
||||
if (filterId == Preferences.getActiveFilterId(context)) { |
||||
// Send broadcast |
||||
val intent = Intent() |
||||
intent.action = INTENT_FILTER_UPDATE_FILTER_UI |
||||
context.sendBroadcast(intent) |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,15 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical"> |
||||
|
||||
<fragment |
||||
android:id="@+id/filtersListFragment" |
||||
android:name="net.pokeranalytics.android.ui.fragment.FiltersListFragment" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:layout="@layout/fragment_data_list" /> |
||||
|
||||
</LinearLayout> |
||||
Loading…
Reference in new issue