# Conflicts: # app/src/main/java/net/pokeranalytics/android/model/migrations/PokerAnalyticsMigration.kt # app/src/main/java/net/pokeranalytics/android/ui/activity/EditableDataActivity.kt # app/src/main/java/net/pokeranalytics/android/ui/fragment/MoreFragment.kt # app/src/main/java/net/pokeranalytics/android/ui/view/RowViewType.kt # app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/MoreTabRow.ktfeature/players
commit
2751850a92
@ -0,0 +1,34 @@ |
||||
package net.pokeranalytics.android.calculus |
||||
|
||||
import net.pokeranalytics.android.util.TextFormat |
||||
import java.util.* |
||||
|
||||
/** |
||||
* ComputedStat contains a [stat] and their associated [value] |
||||
*/ |
||||
class ComputedStat(var stat: Stat, var value: Double, var secondValue: Double? = null, var currency: Currency? = null) { |
||||
|
||||
constructor(stat: Stat, value: Double, previousValue: Double?) : this(stat, value) { |
||||
if (previousValue != null) { |
||||
this.variation = (value - previousValue) / previousValue |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* The value used to get evolution dataset |
||||
*/ |
||||
var progressValue: Double? = null |
||||
|
||||
/** |
||||
* The variation of the stat |
||||
*/ |
||||
var variation: Double? = null |
||||
|
||||
/** |
||||
* Formats the value of the stat to be suitable for display |
||||
*/ |
||||
fun format(): TextFormat { |
||||
return this.stat.format(this.value, this.secondValue, this.currency) |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,118 @@ |
||||
package net.pokeranalytics.android.calculus.bankroll |
||||
|
||||
import io.realm.Realm |
||||
import io.realm.RealmResults |
||||
import kotlinx.coroutines.Dispatchers |
||||
import kotlinx.coroutines.GlobalScope |
||||
import kotlinx.coroutines.async |
||||
import kotlinx.coroutines.launch |
||||
import net.pokeranalytics.android.model.realm.Bankroll |
||||
import net.pokeranalytics.android.model.realm.ComputableResult |
||||
import net.pokeranalytics.android.model.realm.Transaction |
||||
import timber.log.Timber |
||||
import java.util.* |
||||
import kotlin.coroutines.CoroutineContext |
||||
|
||||
object BankrollReportManager { |
||||
|
||||
val coroutineContext: CoroutineContext |
||||
get() = Dispatchers.Main |
||||
|
||||
private var reports: MutableMap<String?, BankrollReport> = mutableMapOf() |
||||
|
||||
private var computableResults: RealmResults<ComputableResult> |
||||
private var bankrolls: RealmResults<Bankroll> |
||||
private var transactions: RealmResults<Transaction> |
||||
|
||||
init { |
||||
|
||||
val realm = Realm.getDefaultInstance() |
||||
computableResults = realm.where(ComputableResult::class.java).findAll() |
||||
bankrolls = realm.where(Bankroll::class.java).findAll() |
||||
transactions = realm.where(Transaction::class.java).findAll() |
||||
|
||||
initializeListeners() |
||||
realm.close() |
||||
} |
||||
|
||||
/** |
||||
* Listens to all objects that might have an impact on any bankroll report |
||||
*/ |
||||
private fun initializeListeners() { |
||||
|
||||
this.computableResults.addChangeListener { t, changeSet -> |
||||
val indexes = changeSet.changes.plus(changeSet.insertions).toList() |
||||
val bankrolls = indexes.mapNotNull { t[it]?.session?.bankroll }.toSet() |
||||
this.updateBankrolls(bankrolls) |
||||
} |
||||
this.bankrolls.addChangeListener { t, changeSet -> |
||||
val indexes = changeSet.changes.plus(changeSet.insertions).toList() |
||||
val bankrolls = indexes.mapNotNull { t[it] }.toSet() |
||||
this.updateBankrolls(bankrolls) |
||||
} |
||||
this.transactions.addChangeListener { t, changeSet -> |
||||
val indexes = changeSet.changes.plus(changeSet.insertions).toList() |
||||
val bankrolls = indexes.mapNotNull { t[it]?.bankroll }.toSet() |
||||
this.updateBankrolls(bankrolls) |
||||
} |
||||
} |
||||
|
||||
fun reportForBankroll(bankrollId: String?, handler: (BankrollReport) -> Unit) { |
||||
|
||||
Timber.d("Request bankroll report for bankrollId = $bankrollId") |
||||
// if the report exists, return it |
||||
val existingReport: BankrollReport? = this.reports[bankrollId] |
||||
if (existingReport != null) { |
||||
handler(existingReport) |
||||
return |
||||
} |
||||
|
||||
// otherwise compute it |
||||
GlobalScope.launch(coroutineContext) { |
||||
|
||||
var report: BankrollReport? = null |
||||
val coroutine = GlobalScope.async { |
||||
val s = Date() |
||||
Timber.d(">>>>> start computing bankroll...") |
||||
|
||||
val realm = Realm.getDefaultInstance() |
||||
|
||||
val setup = BankrollReportSetup(bankrollId) |
||||
report = BankrollCalculator.computeReport(realm, setup) |
||||
|
||||
realm.close() |
||||
|
||||
val e = Date() |
||||
val duration = (e.time - s.time) / 1000.0 |
||||
Timber.d(">>>>> ended in $duration seconds") |
||||
|
||||
} |
||||
coroutine.await() |
||||
|
||||
report?.let { |
||||
handler(it) |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Notifies the manager of cases not managed by RealmResults listener, such as deletions |
||||
*/ |
||||
fun notifyBankrollReportImpact(bankrollId: String) { |
||||
this.reports.remove(bankrollId) |
||||
this.reports.remove(null) |
||||
} |
||||
|
||||
private fun updateBankrolls(bankrolls: Set<Bankroll>) { |
||||
this.invalidateReport(bankrolls) |
||||
} |
||||
|
||||
private fun invalidateReport(bankrolls: Set<Bankroll>) { |
||||
this.reports.remove(null) |
||||
bankrolls.forEach { br -> |
||||
this.reports.remove(br.id) |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,39 @@ |
||||
package net.pokeranalytics.android.model.utils |
||||
|
||||
import io.realm.Realm |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import net.pokeranalytics.android.model.realm.Transaction |
||||
import net.pokeranalytics.android.model.realm.TransactionType |
||||
import java.util.* |
||||
|
||||
class DataUtils { |
||||
|
||||
companion object { |
||||
|
||||
/** |
||||
* Returns true if the provided parameters doesn't correspond to an existing session |
||||
*/ |
||||
fun sessionCount(realm: Realm, startDate: Date, endDate: Date, net: Double): Int { |
||||
val sessions = realm.where(Session::class.java) |
||||
.equalTo("startDate", startDate) |
||||
.equalTo("endDate", endDate) |
||||
.equalTo("result.net", net) |
||||
.findAll() |
||||
return sessions.size |
||||
} |
||||
|
||||
/** |
||||
* Returns true if the provided parameters doesn't correspond to an existing transaction |
||||
*/ |
||||
fun transactionUnicityCheck(realm: Realm, date: Date, amount: Double, type: TransactionType): Boolean { |
||||
val transactions = realm.where(Transaction::class.java) |
||||
.equalTo("date", date) |
||||
.equalTo("amount", amount) |
||||
.equalTo("type.id", type.id) |
||||
.findAll() |
||||
return transactions.isEmpty() |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -1,21 +0,0 @@ |
||||
package net.pokeranalytics.android.model.utils |
||||
|
||||
import io.realm.Realm |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import java.util.* |
||||
|
||||
class SessionUtils { |
||||
|
||||
companion object { |
||||
|
||||
/** |
||||
* Returns true if the provided parameters doesn't correspond to an existing session |
||||
*/ |
||||
fun unicityCheck(realm: Realm, startDate: Date, endDate: Date, net: Double) : Boolean { |
||||
val sessions = realm.where(Session::class.java).equalTo("startDate", startDate).equalTo("endDate", endDate).equalTo("result.net", net).findAll() |
||||
return sessions.isEmpty() |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -1,115 +0,0 @@ |
||||
package net.pokeranalytics.android.ui.fragment |
||||
|
||||
import android.os.Bundle |
||||
import android.view.* |
||||
import kotlinx.android.synthetic.main.fragment_comparison_chart.* |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.ui.activity.BankrollActivity |
||||
import net.pokeranalytics.android.ui.activity.SettingsActivity |
||||
import net.pokeranalytics.android.ui.adapter.ComparisonChartPagerAdapter |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate |
||||
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource |
||||
import net.pokeranalytics.android.ui.extensions.toast |
||||
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment |
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
import net.pokeranalytics.android.ui.view.rowrepresentable.MoreTabRow |
||||
|
||||
class ComparisonChartFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate { |
||||
|
||||
companion object { |
||||
|
||||
/** |
||||
* Create new instance |
||||
*/ |
||||
fun newInstance(): ComparisonChartFragment { |
||||
val fragment = ComparisonChartFragment() |
||||
val bundle = Bundle() |
||||
fragment.arguments = bundle |
||||
return fragment |
||||
} |
||||
|
||||
val rowRepresentation: List<RowRepresentable> by lazy { |
||||
val rows = ArrayList<RowRepresentable>() |
||||
rows.addAll(MoreTabRow.values()) |
||||
rows |
||||
} |
||||
|
||||
} |
||||
|
||||
private lateinit var viewPagerAdapter: ComparisonChartPagerAdapter |
||||
private var comparisonChartMenu: Menu? = null |
||||
|
||||
|
||||
// Life Cycle |
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
||||
return inflater.inflate(R.layout.fragment_comparison_chart, container, false) |
||||
} |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initData() |
||||
initUI() |
||||
} |
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) { |
||||
menu?.clear() |
||||
inflater?.inflate(R.menu.toolbar_comparison_chart, menu) |
||||
this.comparisonChartMenu = menu |
||||
super.onCreateOptionsMenu(menu, inflater) |
||||
} |
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem?): Boolean { |
||||
when (item!!.itemId) { |
||||
R.id.settings -> openChangeStatistics() |
||||
} |
||||
return true |
||||
} |
||||
|
||||
// Rows |
||||
override fun adapterRows(): List<RowRepresentable>? { |
||||
return rowRepresentation |
||||
} |
||||
|
||||
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
||||
super.onRowSelected(position, row, fromAction) |
||||
when(row) { |
||||
MoreTabRow.BANKROLL -> BankrollActivity.newInstance(requireContext()) |
||||
MoreTabRow.SETTINGS -> SettingsActivity.newInstance(requireContext()) |
||||
} |
||||
} |
||||
|
||||
// Business |
||||
|
||||
/** |
||||
* Init data |
||||
*/ |
||||
private fun initData() { |
||||
} |
||||
|
||||
/** |
||||
* Init UI |
||||
*/ |
||||
private fun initUI() { |
||||
|
||||
setDisplayHomeAsUpEnabled(true) |
||||
setToolbarTitle(getString(R.string.comparison_chart)) |
||||
|
||||
parentActivity?.let { |
||||
viewPagerAdapter = ComparisonChartPagerAdapter(requireContext(), it.supportFragmentManager) |
||||
viewPager.adapter = viewPagerAdapter |
||||
viewPager.offscreenPageLimit = 2 |
||||
tabs.setupWithViewPager(viewPager) |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Open change statistics |
||||
*/ |
||||
private fun openChangeStatistics() { |
||||
//TODO |
||||
toast("Open change statistics") |
||||
} |
||||
|
||||
} |
||||
@ -1,95 +1,196 @@ |
||||
package net.pokeranalytics.android.ui.fragment |
||||
|
||||
import android.app.Activity |
||||
import android.content.ActivityNotFoundException |
||||
import android.content.Intent |
||||
import android.net.Uri |
||||
import android.os.Bundle |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.realm.Realm |
||||
import kotlinx.android.synthetic.main.fragment_more.* |
||||
import net.pokeranalytics.android.BuildConfig |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.model.LiveData |
||||
import net.pokeranalytics.android.ui.activity.BankrollActivity |
||||
import net.pokeranalytics.android.ui.activity.DataListActivity |
||||
import net.pokeranalytics.android.ui.activity.SettingsActivity |
||||
import net.pokeranalytics.android.model.realm.Currency |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import net.pokeranalytics.android.ui.activity.* |
||||
import net.pokeranalytics.android.ui.activity.components.RequestCode |
||||
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.extensions.openContactMail |
||||
import net.pokeranalytics.android.ui.extensions.openPlayStorePage |
||||
import net.pokeranalytics.android.ui.extensions.openUrl |
||||
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment |
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
import net.pokeranalytics.android.ui.view.rowrepresentable.MoreTabRow |
||||
import net.pokeranalytics.android.ui.view.rowrepresentable.SettingRow |
||||
import net.pokeranalytics.android.util.Preferences |
||||
import net.pokeranalytics.android.util.URL |
||||
import net.pokeranalytics.android.util.UserDefaults |
||||
import net.pokeranalytics.android.util.billing.AppGuard |
||||
import net.pokeranalytics.android.util.billing.IAPProducts |
||||
import timber.log.Timber |
||||
import java.util.* |
||||
|
||||
class MoreFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate { |
||||
|
||||
companion object { |
||||
class MoreFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, StaticRowRepresentableDataSource { |
||||
|
||||
/** |
||||
* Create new instance |
||||
*/ |
||||
fun newInstance(): MoreFragment { |
||||
val fragment = MoreFragment() |
||||
val bundle = Bundle() |
||||
fragment.arguments = bundle |
||||
return fragment |
||||
} |
||||
|
||||
val rowRepresentation: List<RowRepresentable> by lazy { |
||||
val rows = ArrayList<RowRepresentable>() |
||||
rows.addAll(MoreTabRow.values()) |
||||
rows |
||||
} |
||||
companion object { |
||||
|
||||
} |
||||
/** |
||||
* Create new instance |
||||
*/ |
||||
fun newInstance(): MoreFragment { |
||||
val fragment = MoreFragment() |
||||
val bundle = Bundle() |
||||
fragment.arguments = bundle |
||||
return fragment |
||||
} |
||||
|
||||
val rowRepresentation: List<RowRepresentable> by lazy { |
||||
val rows = ArrayList<RowRepresentable>() |
||||
rows.addAll(SettingRow.getRows()) |
||||
rows |
||||
} |
||||
|
||||
private lateinit var moreAdapter: RowRepresentableAdapter |
||||
} |
||||
|
||||
private lateinit var settingsAdapterRow: RowRepresentableAdapter |
||||
|
||||
// Life Cycle |
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
||||
return inflater.inflate(R.layout.fragment_more, container, false) |
||||
} |
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
||||
return inflater.inflate(R.layout.fragment_more, container, false) |
||||
} |
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initData() |
||||
initUI() |
||||
} |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initData() |
||||
initUI() |
||||
} |
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
||||
|
||||
when (requestCode) { |
||||
RequestCode.CURRENCY.value -> { |
||||
if (resultCode == Activity.RESULT_OK) { |
||||
data?.let { |
||||
Preferences.setCurrencyCode(data.getStringExtra(CurrenciesFragment.INTENT_CURRENCY_CODE), requireContext()) |
||||
val realm = Realm.getDefaultInstance() |
||||
realm.executeTransaction { |
||||
realm.where(Currency::class.java).isNull("code").or().equalTo("code", UserDefaults.currency.currencyCode).findAll() |
||||
.forEach { currency -> |
||||
currency.rate = Currency.DEFAULT_RATE |
||||
} |
||||
|
||||
realm.where(Session::class.java).isNull("bankroll.currency.code").findAll().forEach { session -> |
||||
session.bankrollHasBeenUpdated() |
||||
} |
||||
} |
||||
realm.close() |
||||
settingsAdapterRow.refreshRow(SettingRow.CURRENCY) |
||||
} |
||||
} |
||||
} |
||||
RequestCode.SUBSCRIPTION.value -> { |
||||
settingsAdapterRow.refreshRow(SettingRow.SUBSCRIPTION) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Rows |
||||
override fun adapterRows(): List<RowRepresentable>? { |
||||
return rowRepresentation |
||||
} |
||||
|
||||
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
||||
super.onRowSelected(position, row, fromAction) |
||||
when(row) { |
||||
MoreTabRow.BANKROLL -> BankrollActivity.newInstance(requireContext()) |
||||
MoreTabRow.PLAYERS -> DataListActivity.newInstance(requireContext(), LiveData.PLAYER.ordinal) |
||||
MoreTabRow.SETTINGS -> SettingsActivity.newInstance(requireContext()) |
||||
override fun stringForRow(row: RowRepresentable): String { |
||||
return when (row) { |
||||
SettingRow.SUBSCRIPTION -> AppGuard.subscriptionStatus(requireContext()) |
||||
SettingRow.VERSION -> BuildConfig.VERSION_NAME + if (BuildConfig.DEBUG) " (${BuildConfig.VERSION_CODE}) DEBUG" else "" |
||||
SettingRow.CURRENCY -> UserDefaults.currency.symbol |
||||
else -> "" |
||||
} |
||||
} |
||||
|
||||
// Business |
||||
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
||||
when (row) { |
||||
SettingRow.BANKROLL_REPORT -> BankrollActivity.newInstance(requireContext()) |
||||
SettingRow.TOP_10 -> Top10Activity.newInstance(requireContext()) |
||||
SettingRow.SUBSCRIPTION -> { |
||||
if (!AppGuard.isProUser) { |
||||
BillingActivity.newInstanceForResult(this, false) |
||||
} else { |
||||
this.openPlaystoreAccount() |
||||
} |
||||
} |
||||
SettingRow.RATE_APP -> parentActivity?.openPlayStorePage() |
||||
SettingRow.CONTACT_US -> parentActivity?.openContactMail(R.string.contact) |
||||
SettingRow.BUG_REPORT -> parentActivity?.openContactMail(R.string.bug_report_subject, Realm.getDefaultInstance().path) |
||||
SettingRow.CURRENCY -> CurrenciesActivity.newInstanceForResult(this@MoreFragment, RequestCode.CURRENCY.value) |
||||
SettingRow.FOLLOW_US -> { |
||||
when (position) { |
||||
0 -> parentActivity?.openUrl(URL.BLOG.value) |
||||
1 -> parentActivity?.openUrl(URL.INSTAGRAM.value) |
||||
2 -> parentActivity?.openUrl(URL.TWITTER.value) |
||||
3 -> parentActivity?.openUrl(URL.FACEBOOK.value) |
||||
} |
||||
} |
||||
SettingRow.PRIVACY_POLICY -> parentActivity?.openUrl(URL.PRIVACY_POLICY.value) |
||||
SettingRow.TERMS_OF_USE -> parentActivity?.openUrl(URL.TERMS.value) |
||||
SettingRow.GDPR -> openGDPRActivity() |
||||
} |
||||
|
||||
/** |
||||
* Init data |
||||
*/ |
||||
private fun initData() { |
||||
} |
||||
row.relatedResultsRepresentable?.let { |
||||
DataListActivity.newInstance(requireContext(), it.ordinal) |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Init UI |
||||
*/ |
||||
private fun initUI() { |
||||
|
||||
moreAdapter = RowRepresentableAdapter(this, this) |
||||
setToolbarTitle(getString(R.string.more)) |
||||
|
||||
val viewManager = LinearLayoutManager(requireContext()) |
||||
settingsAdapterRow = RowRepresentableAdapter( |
||||
this, this |
||||
) |
||||
|
||||
recyclerView.apply { |
||||
setHasFixedSize(true) |
||||
layoutManager = viewManager |
||||
adapter = moreAdapter |
||||
adapter = settingsAdapterRow |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Init data |
||||
*/ |
||||
private fun initData() { |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Open GDPR Activity |
||||
*/ |
||||
private fun openGDPRActivity() { |
||||
val intent = Intent(requireContext(), GDPRActivity::class.java) |
||||
startActivity(intent) |
||||
} |
||||
|
||||
/** |
||||
* Open Google Play account |
||||
*/ |
||||
private fun openPlaystoreAccount() { |
||||
|
||||
val packageName = "net.pokeranalytics.android" |
||||
val sku = IAPProducts.PRO.identifier |
||||
|
||||
try { |
||||
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/account/subscriptions?sku=$sku&package=$packageName"))) |
||||
} catch (e: ActivityNotFoundException) { |
||||
Timber.d(e) |
||||
} |
||||
} |
||||
|
||||
|
||||
@ -1,184 +0,0 @@ |
||||
package net.pokeranalytics.android.ui.fragment |
||||
|
||||
import android.app.Activity |
||||
import android.content.ActivityNotFoundException |
||||
import android.content.Intent |
||||
import android.net.Uri |
||||
import android.os.Bundle |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import io.realm.Realm |
||||
import kotlinx.android.synthetic.main.fragment_settings.* |
||||
import net.pokeranalytics.android.BuildConfig |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import net.pokeranalytics.android.ui.activity.BillingActivity |
||||
import net.pokeranalytics.android.ui.activity.CurrenciesActivity |
||||
import net.pokeranalytics.android.ui.activity.DataListActivity |
||||
import net.pokeranalytics.android.ui.activity.GDPRActivity |
||||
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.extensions.openContactMail |
||||
import net.pokeranalytics.android.ui.extensions.openPlayStorePage |
||||
import net.pokeranalytics.android.ui.extensions.openUrl |
||||
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment |
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
import net.pokeranalytics.android.ui.view.rowrepresentable.SettingRow |
||||
import net.pokeranalytics.android.util.Preferences |
||||
import net.pokeranalytics.android.util.URL |
||||
import net.pokeranalytics.android.util.UserDefaults |
||||
import net.pokeranalytics.android.util.billing.AppGuard |
||||
import net.pokeranalytics.android.util.billing.IAPProducts |
||||
import timber.log.Timber |
||||
import java.util.* |
||||
|
||||
|
||||
class SettingsFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, StaticRowRepresentableDataSource { |
||||
|
||||
|
||||
companion object { |
||||
|
||||
/** |
||||
* Create new instance |
||||
*/ |
||||
fun newInstance(): SettingsFragment { |
||||
val fragment = SettingsFragment() |
||||
val bundle = Bundle() |
||||
fragment.arguments = bundle |
||||
return fragment |
||||
} |
||||
|
||||
val rowRepresentation: List<RowRepresentable> by lazy { |
||||
val rows = ArrayList<RowRepresentable>() |
||||
rows.addAll(SettingRow.getRows()) |
||||
rows |
||||
} |
||||
|
||||
const val REQUEST_CODE_CURRENCY: Int = 100 |
||||
} |
||||
|
||||
private lateinit var settingsAdapterRow: RowRepresentableAdapter |
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
||||
return inflater.inflate(R.layout.fragment_settings, container, false) |
||||
} |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initData() |
||||
initUI() |
||||
} |
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
||||
if (requestCode == REQUEST_CODE_CURRENCY && resultCode == Activity.RESULT_OK) { |
||||
data?.let { |
||||
Preferences.setCurrencyCode(data.getStringExtra(CurrenciesFragment.INTENT_CURRENCY_CODE), requireContext()) |
||||
val realm = Realm.getDefaultInstance() |
||||
realm.executeTransaction { |
||||
it.where(Session::class.java).isNull("bankroll.currency.code").findAll().forEach { |
||||
it.bankrollHasBeenUpdated() |
||||
} |
||||
} |
||||
realm.close() |
||||
settingsAdapterRow.refreshRow(SettingRow.CURRENCY) |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun adapterRows(): List<RowRepresentable>? { |
||||
return rowRepresentation |
||||
} |
||||
|
||||
override fun stringForRow(row: RowRepresentable): String { |
||||
return when (row) { |
||||
SettingRow.SUBSCRIPTION -> AppGuard.subscriptionStatus(requireContext()) |
||||
SettingRow.VERSION -> BuildConfig.VERSION_NAME + if (BuildConfig.DEBUG) " (${BuildConfig.VERSION_CODE}) DEBUG" else "" |
||||
SettingRow.CURRENCY -> UserDefaults.currency.symbol |
||||
else -> "" |
||||
} |
||||
} |
||||
|
||||
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
||||
when (row) { |
||||
SettingRow.SUBSCRIPTION -> { |
||||
if (!AppGuard.isProUser) { |
||||
BillingActivity.newInstance(requireContext()) |
||||
} else { |
||||
this.openPlaystoreAccount() |
||||
} |
||||
} |
||||
SettingRow.RATE_APP -> parentActivity?.openPlayStorePage() |
||||
SettingRow.CONTACT_US -> parentActivity?.openContactMail(R.string.contact) |
||||
SettingRow.BUG_REPORT -> parentActivity?.openContactMail(R.string.bug_report_subject, Realm.getDefaultInstance().path) |
||||
SettingRow.CURRENCY -> CurrenciesActivity.newInstanceForResult(this@SettingsFragment, REQUEST_CODE_CURRENCY) |
||||
SettingRow.FOLLOW_US -> { |
||||
when (position) { |
||||
0 -> parentActivity?.openUrl(URL.BLOG.value) |
||||
1 -> parentActivity?.openUrl(URL.INSTAGRAM.value) |
||||
2 -> parentActivity?.openUrl(URL.TWITTER.value) |
||||
3 -> parentActivity?.openUrl(URL.FACEBOOK.value) |
||||
} |
||||
} |
||||
SettingRow.PRIVACY_POLICY -> parentActivity?.openUrl(URL.PRIVACY_POLICY.value) |
||||
SettingRow.TERMS_OF_USE -> parentActivity?.openUrl(URL.TERMS.value) |
||||
SettingRow.GDPR -> openGDPRActivity() |
||||
} |
||||
|
||||
row.relatedResultsRepresentable?.let { |
||||
DataListActivity.newInstance(requireContext(), it.ordinal) |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Init UI |
||||
*/ |
||||
private fun initUI() { |
||||
|
||||
setDisplayHomeAsUpEnabled(true) |
||||
|
||||
val viewManager = LinearLayoutManager(requireContext()) |
||||
settingsAdapterRow = RowRepresentableAdapter( |
||||
this, this |
||||
) |
||||
|
||||
recyclerView.apply { |
||||
setHasFixedSize(true) |
||||
layoutManager = viewManager |
||||
adapter = settingsAdapterRow |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Init data |
||||
*/ |
||||
private fun initData() { |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Open GDPR Activity |
||||
*/ |
||||
private fun openGDPRActivity() { |
||||
val intent = Intent(requireContext(), GDPRActivity::class.java) |
||||
startActivity(intent) |
||||
} |
||||
|
||||
/** |
||||
* Open Google Play account |
||||
*/ |
||||
private fun openPlaystoreAccount() { |
||||
|
||||
val packageName = "net.pokeranalytics.android" |
||||
val sku = IAPProducts.PRO.identifier |
||||
|
||||
try { |
||||
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/account/subscriptions?sku=$sku&package=$packageName"))) |
||||
} catch (e: ActivityNotFoundException) { |
||||
Timber.d(e) |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,175 @@ |
||||
package net.pokeranalytics.android.ui.fragment |
||||
|
||||
import android.os.Bundle |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import com.google.android.material.tabs.TabLayout |
||||
import io.realm.RealmResults |
||||
import io.realm.kotlin.where |
||||
import kotlinx.android.synthetic.main.fragment_top_10.* |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.model.realm.Session |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate |
||||
import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
import net.pokeranalytics.android.ui.view.RowViewType |
||||
import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager |
||||
|
||||
|
||||
class Top10Fragment : RealmFragment(), RowRepresentableDataSource, RowRepresentableDelegate { |
||||
|
||||
private enum class Tab { |
||||
CASH_GAMES, |
||||
TOURNAMENTS |
||||
} |
||||
|
||||
companion object { |
||||
fun newInstance(): Top10Fragment { |
||||
val fragment = Top10Fragment() |
||||
val bundle = Bundle() |
||||
fragment.arguments = bundle |
||||
return fragment |
||||
} |
||||
} |
||||
|
||||
private lateinit var positiveSessions: RealmResults<Session> |
||||
private lateinit var dataListAdapter: RowRepresentableAdapter |
||||
|
||||
private var realmCashGames: List<Session> = mutableListOf() |
||||
private var realmTournaments: List<Session> = mutableListOf() |
||||
|
||||
private var currentTab: Tab = Tab.CASH_GAMES |
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
||||
super.onCreateView(inflater, container, savedInstanceState) |
||||
return inflater.inflate(R.layout.fragment_top_10, container, false) |
||||
} |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initUI() |
||||
initData() |
||||
} |
||||
|
||||
/** |
||||
* Init UI |
||||
*/ |
||||
private fun initUI() { |
||||
|
||||
dataListAdapter = RowRepresentableAdapter(this, this) |
||||
recyclerView.adapter = dataListAdapter |
||||
|
||||
setDisplayHomeAsUpEnabled(true) |
||||
|
||||
tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { |
||||
override fun onTabSelected(tab: TabLayout.Tab) { |
||||
when (tab.position) { |
||||
0 -> { |
||||
currentTab = Tab.CASH_GAMES |
||||
dataListAdapter.notifyDataSetChanged() |
||||
} |
||||
1 -> { |
||||
currentTab = Tab.TOURNAMENTS |
||||
} |
||||
} |
||||
dataListAdapter.notifyDataSetChanged() |
||||
} |
||||
|
||||
override fun onTabUnselected(tab: TabLayout.Tab) { |
||||
} |
||||
|
||||
override fun onTabReselected(tab: TabLayout.Tab) { |
||||
} |
||||
}) |
||||
|
||||
|
||||
val viewManager = SmoothScrollLinearLayoutManager(requireContext()) |
||||
recyclerView.apply { |
||||
setHasFixedSize(true) |
||||
layoutManager = viewManager |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Init data |
||||
*/ |
||||
private fun initData() { |
||||
|
||||
this.positiveSessions = getRealm().where<Session>() |
||||
.isNotNull("endDate") |
||||
.greaterThan("result.net", 0.0) |
||||
.findAll() |
||||
|
||||
updateTop10() |
||||
|
||||
} |
||||
|
||||
private fun updateTop10() { |
||||
|
||||
val cashGames = mutableListOf<Session>() |
||||
val tournaments = mutableListOf<Session>() |
||||
|
||||
// filter by type: cash game or tournament |
||||
this.positiveSessions.forEach { |
||||
when (it.type) { |
||||
Session.Type.CASH_GAME.ordinal -> { |
||||
cashGames.add(it) |
||||
} |
||||
else -> { |
||||
tournaments.add(it) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Sort by rated net |
||||
val sortedCashGames = cashGames.sortedByDescending { |
||||
it.computableResult?.ratedNet |
||||
}.toMutableList() |
||||
val sortedTournaments = tournaments.sortedByDescending { |
||||
it.computableResult?.ratedNet |
||||
}.toMutableList() |
||||
|
||||
// Keep 10 items |
||||
if (sortedCashGames.size > 10) { |
||||
sortedCashGames.subList(10, sortedCashGames.size).clear() |
||||
} |
||||
if (sortedTournaments.size > 10) { |
||||
sortedTournaments.subList(10, sortedTournaments.size).clear() |
||||
} |
||||
|
||||
this.realmCashGames = sortedCashGames |
||||
this.realmTournaments = sortedTournaments |
||||
|
||||
dataListAdapter.notifyDataSetChanged() |
||||
} |
||||
|
||||
override fun adapterRows(): List<RowRepresentable>? { |
||||
return when (currentTab) { |
||||
Tab.CASH_GAMES -> realmCashGames |
||||
Tab.TOURNAMENTS -> realmTournaments |
||||
} |
||||
} |
||||
|
||||
override fun rowRepresentableForPosition(position: Int): RowRepresentable? { |
||||
return when (currentTab) { |
||||
Tab.CASH_GAMES -> realmCashGames[position] |
||||
Tab.TOURNAMENTS -> realmTournaments[position] |
||||
} |
||||
} |
||||
|
||||
override fun numberOfRows(): Int { |
||||
return when (currentTab) { |
||||
Tab.CASH_GAMES -> realmCashGames.size |
||||
Tab.TOURNAMENTS -> realmTournaments.size |
||||
} |
||||
} |
||||
|
||||
override fun viewTypeForPosition(position: Int): Int { |
||||
return RowViewType.ROW_TOP_10.ordinal |
||||
} |
||||
|
||||
} |
||||
@ -1,48 +0,0 @@ |
||||
package net.pokeranalytics.android.ui.fragment.components.bottomsheet |
||||
|
||||
enum class BottomSheetType { |
||||
NONE, |
||||
LIST, |
||||
LIST_STATIC, |
||||
LIST_GAME, |
||||
DOUBLE_LIST, |
||||
MULTI_SELECTION, |
||||
GRID, |
||||
EDIT_TEXT, |
||||
EDIT_TEXT_MULTI_LINES, |
||||
DOUBLE_EDIT_TEXT, |
||||
NUMERIC_TEXT, |
||||
SUM; |
||||
|
||||
fun newInstance(): BottomSheetFragment { |
||||
return when (this) { |
||||
NONE -> BottomSheetFragment() |
||||
LIST -> BottomSheetListFragment() |
||||
LIST_STATIC -> BottomSheetStaticListFragment() |
||||
LIST_GAME -> BottomSheetListGameFragment() |
||||
DOUBLE_LIST -> BottomSheetListGameFragment() |
||||
MULTI_SELECTION -> BottomSheetMultiSelectionFragment() |
||||
GRID -> BottomSheetTableSizeGridFragment() |
||||
EDIT_TEXT -> BottomSheetEditTextFragment() |
||||
EDIT_TEXT_MULTI_LINES -> BottomSheetEditTextMultiLinesFragment() |
||||
DOUBLE_EDIT_TEXT -> BottomSheetDoubleEditTextFragment() |
||||
NUMERIC_TEXT -> BottomSheetNumericTextFragment() |
||||
SUM -> BottomSheetSumFragment() |
||||
} |
||||
} |
||||
|
||||
val validationRequired: Boolean |
||||
get() = when (this) { |
||||
LIST, LIST_GAME, LIST_STATIC, GRID, DOUBLE_LIST -> false |
||||
else -> true |
||||
} |
||||
|
||||
val clearRequired: Boolean |
||||
get() = true |
||||
|
||||
val addRequired: Boolean |
||||
get() = when (this) { |
||||
EDIT_TEXT, NUMERIC_TEXT, DOUBLE_EDIT_TEXT, EDIT_TEXT_MULTI_LINES, GRID, LIST_STATIC, SUM -> false |
||||
else -> true |
||||
} |
||||
} |
||||
@ -0,0 +1,50 @@ |
||||
package net.pokeranalytics.android.ui.fragment.components.input |
||||
|
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
|
||||
enum class InputFragmentType { |
||||
NONE, |
||||
LIST, |
||||
LIST_STATIC, |
||||
LIST_GAME, |
||||
DOUBLE_LIST, |
||||
MULTI_SELECTION, |
||||
GRID, |
||||
EDIT_TEXT, |
||||
EDIT_TEXT_MULTI_LINES, |
||||
DOUBLE_EDIT_TEXT, |
||||
NUMERIC_TEXT, |
||||
SUM; |
||||
|
||||
fun newInstance(row: RowRepresentable): InputFragment { |
||||
return when (this) { |
||||
NONE -> InputFragment(row) |
||||
LIST -> InputListFragment(row) |
||||
LIST_STATIC -> InputStaticListFragment(row) |
||||
LIST_GAME -> InputListGameFragment(row) |
||||
DOUBLE_LIST -> InputListGameFragment(row) |
||||
MULTI_SELECTION -> InputMultiSelectionFragment(row) |
||||
GRID -> InputTableSizeGridFragment(row) |
||||
EDIT_TEXT -> InputEditTextFragment(row) |
||||
EDIT_TEXT_MULTI_LINES -> InputEditTextMultiLinesFragment(row) |
||||
DOUBLE_EDIT_TEXT -> InputDoubleEditTextFragment(row) |
||||
NUMERIC_TEXT -> InputNumericTextFragment(row) |
||||
SUM -> InputSumFragment(row) |
||||
} |
||||
} |
||||
|
||||
val validationRequired: Boolean |
||||
get() = when (this) { |
||||
LIST, LIST_GAME, LIST_STATIC, GRID, DOUBLE_LIST -> false |
||||
else -> true |
||||
} |
||||
|
||||
val clearRequired: Boolean |
||||
get() = true |
||||
|
||||
val addRequired: Boolean |
||||
get() = when (this) { |
||||
EDIT_TEXT, NUMERIC_TEXT, DOUBLE_EDIT_TEXT, EDIT_TEXT_MULTI_LINES, GRID, LIST_STATIC, SUM -> false |
||||
else -> true |
||||
} |
||||
} |
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue