parent
08167f721a
commit
aa2e144fb9
@ -0,0 +1,25 @@ |
||||
package net.pokeranalytics.android.ui.modules.settings |
||||
|
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import android.os.Bundle |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.ui.activity.components.BaseActivity |
||||
|
||||
class TransactionFilterActivity : BaseActivity() { |
||||
|
||||
companion object { |
||||
|
||||
fun newInstance(context: Context) { |
||||
val intent = Intent(context, TransactionFilterActivity::class.java) |
||||
context.startActivity(intent) |
||||
} |
||||
|
||||
} |
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
setContentView(R.layout.activity_transaction_filter) |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,132 @@ |
||||
package net.pokeranalytics.android.ui.modules.settings |
||||
|
||||
import android.content.Context |
||||
import android.os.Bundle |
||||
import android.view.* |
||||
import androidx.lifecycle.ViewModelProvider |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.realm.kotlin.where |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.databinding.FragmentTransactionFilterBinding |
||||
import net.pokeranalytics.android.model.realm.TransactionType |
||||
import net.pokeranalytics.android.model.realm.UserConfig |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate |
||||
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource |
||||
import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
import net.pokeranalytics.android.ui.view.RowViewType |
||||
|
||||
class TransactionFilterFragment : RealmFragment(), StaticRowRepresentableDataSource, |
||||
RowRepresentableDelegate { |
||||
|
||||
private var _binding: FragmentTransactionFilterBinding? = null |
||||
private val binding get() = _binding!! |
||||
|
||||
private lateinit var model: TransactionFilterViewModel |
||||
|
||||
private lateinit var rowRepresentableAdapter: RowRepresentableAdapter |
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) { |
||||
super.onCreate(savedInstanceState) |
||||
|
||||
this.model = activity?.run { |
||||
ViewModelProvider(this).get(TransactionFilterViewModel::class.java) |
||||
} ?: throw Exception("Invalid Activity") |
||||
|
||||
} |
||||
|
||||
override fun onCreateView( |
||||
inflater: LayoutInflater, |
||||
container: ViewGroup?, |
||||
savedInstanceState: Bundle? |
||||
): View { |
||||
super.onCreateView(inflater, container, savedInstanceState) |
||||
_binding = FragmentTransactionFilterBinding.inflate(inflater, container, false) |
||||
return binding.root |
||||
} |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initUI() |
||||
initData() |
||||
} |
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { |
||||
menu.clear() |
||||
inflater.inflate(R.menu.toolbar_save, menu) |
||||
super.onCreateOptionsMenu(menu, inflater) |
||||
} |
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean { |
||||
when (item.itemId) { |
||||
R.id.save -> save() |
||||
} |
||||
return true |
||||
} |
||||
|
||||
private fun initUI() { |
||||
setDisplayHomeAsUpEnabled(true) |
||||
|
||||
val viewManager = LinearLayoutManager(requireContext()) |
||||
this.binding.recyclerView.apply { |
||||
setHasFixedSize(true) |
||||
layoutManager = viewManager |
||||
} |
||||
|
||||
this.rowRepresentableAdapter = RowRepresentableAdapter(this, this) |
||||
this.binding.recyclerView.adapter = rowRepresentableAdapter |
||||
|
||||
} |
||||
|
||||
private fun initData() { |
||||
val transactionTypes = getRealm().where<TransactionType>() |
||||
.notEqualTo("kind", TransactionType.Value.DEPOSIT.uniqueIdentifier) |
||||
.notEqualTo("kind", TransactionType.Value.WITHDRAWAL.uniqueIdentifier) |
||||
.notEqualTo("kind", TransactionType.Value.TRANSFER.uniqueIdentifier) |
||||
.notEqualTo("kind", TransactionType.Value.STACKING_INCOMING.uniqueIdentifier) |
||||
.notEqualTo("kind", TransactionType.Value.STACKING_OUTGOING.uniqueIdentifier) |
||||
.sort("name") |
||||
.findAll() |
||||
this.model.transactionTypes = transactionTypes |
||||
|
||||
val userConfig = UserConfig.getConfiguration(this.getRealm()) |
||||
this.model.selectedTransactionTypes = userConfig.transactionTypes(getRealm()).toMutableSet() |
||||
|
||||
} |
||||
|
||||
private fun save() { |
||||
getRealm().executeTransaction { realm -> |
||||
val userConfig = UserConfig.getConfiguration(realm) |
||||
userConfig.setTransactionTypeIds(this.model.selectedTransactionTypes) |
||||
realm.copyToRealmOrUpdate(userConfig) |
||||
} |
||||
this.activity?.finish() |
||||
} |
||||
|
||||
override fun adapterRows(): List<RowRepresentable> { |
||||
return this.model.transactionTypes.map { TransactionTypeSwitchRow(it) } |
||||
} |
||||
|
||||
override fun boolForRow(row: RowRepresentable): Boolean { |
||||
val transactionTypeRow = row as TransactionTypeSwitchRow |
||||
return this.model.selectedTransactionTypes.contains(transactionTypeRow.transactionType) |
||||
} |
||||
|
||||
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
||||
val isChecked = value as Boolean |
||||
val transactionTypeRow = row as TransactionTypeSwitchRow |
||||
this.model.selectTransactionType(transactionTypeRow.transactionType, isChecked) |
||||
} |
||||
|
||||
} |
||||
|
||||
class TransactionTypeSwitchRow(val transactionType: TransactionType) : RowRepresentable { |
||||
|
||||
override fun getDisplayName(context: Context): String { |
||||
return transactionType.name |
||||
} |
||||
|
||||
override val viewType: Int = RowViewType.TITLE_SWITCH.identifier |
||||
|
||||
} |
||||
@ -0,0 +1,19 @@ |
||||
package net.pokeranalytics.android.ui.modules.settings |
||||
|
||||
import androidx.lifecycle.ViewModel |
||||
import net.pokeranalytics.android.model.realm.TransactionType |
||||
|
||||
class TransactionFilterViewModel : ViewModel() { |
||||
|
||||
lateinit var transactionTypes: List<TransactionType> |
||||
|
||||
var selectedTransactionTypes: MutableSet<TransactionType> = mutableSetOf() |
||||
|
||||
fun selectTransactionType(transactionType: TransactionType, selected: Boolean) { |
||||
when(selected) { |
||||
true -> this.selectedTransactionTypes.add(transactionType) |
||||
false -> this.selectedTransactionTypes.remove(transactionType) |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,10 @@ |
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:width="24dp" |
||||
android:height="24dp" |
||||
android:viewportWidth="24" |
||||
android:viewportHeight="24" |
||||
android:tint="?attr/colorControlNormal"> |
||||
<path |
||||
android:fillColor="@android:color/white" |
||||
android:pathData="M20,4L4,4c-1.11,0 -1.99,0.89 -1.99,2L2,18c0,1.11 0.89,2 2,2h16c1.11,0 2,-0.89 2,-2L22,6c0,-1.11 -0.89,-2 -2,-2zM20,18L4,18v-6h16v6zM20,8L4,8L4,6h16v2z"/> |
||||
</vector> |
||||
@ -0,0 +1,15 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:orientation="vertical"> |
||||
|
||||
<androidx.fragment.app.FragmentContainerView |
||||
android:id="@+id/fragment" |
||||
android:name="net.pokeranalytics.android.ui.modules.settings.TransactionFilterFragment" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:layout="@layout/fragment_transaction_filter" /> |
||||
|
||||
</LinearLayout> |
||||
@ -0,0 +1,59 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<com.google.android.material.appbar.AppBarLayout |
||||
android:id="@+id/appBar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="128dp" |
||||
android:theme="@style/PokerAnalyticsTheme.Toolbar.Session" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent"> |
||||
|
||||
<com.google.android.material.appbar.CollapsingToolbarLayout |
||||
android:id="@+id/collapsingToolbar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
app:collapsedTitleTextAppearance="@style/PokerAnalyticsTheme.Toolbar.CollapsedTitleAppearance" |
||||
app:contentScrim="?attr/colorPrimary" |
||||
app:expandedTitleGravity="bottom" |
||||
app:expandedTitleMarginStart="72dp" |
||||
app:expandedTitleTextAppearance="@style/PokerAnalyticsTheme.Toolbar.ExpandedTitleAppearance" |
||||
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"> |
||||
|
||||
<androidx.appcompat.widget.Toolbar |
||||
android:id="@+id/toolbar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="?attr/actionBarSize" |
||||
app:layout_collapseMode="pin" |
||||
app:title="@string/transaction_filter" |
||||
app:titleTextColor="@color/white" /> |
||||
|
||||
</com.google.android.material.appbar.CollapsingToolbarLayout> |
||||
|
||||
</com.google.android.material.appbar.AppBarLayout> |
||||
|
||||
<androidx.core.widget.NestedScrollView |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:fillViewport="true" |
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior"> |
||||
|
||||
<FrameLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/recyclerView" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:clipToPadding="false" /> |
||||
|
||||
</FrameLayout> |
||||
|
||||
</androidx.core.widget.NestedScrollView> |
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
||||
@ -1,4 +1,5 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<item name="menu_item_filter" type="id"/> |
||||
<item name="menu_item_transaction_filter" type="id"/> |
||||
</resources> |
||||
Loading…
Reference in new issue