|
|
|
|
@ -6,10 +6,16 @@ import android.os.Bundle |
|
|
|
|
import android.view.LayoutInflater |
|
|
|
|
import android.view.View |
|
|
|
|
import android.view.ViewGroup |
|
|
|
|
import androidx.appcompat.app.AlertDialog |
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager |
|
|
|
|
import com.google.android.material.snackbar.Snackbar |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import io.realm.RealmObject |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_bankroll.* |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_bankroll.addButton |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_bankroll.toolbar |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_data_list.* |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_stats.recyclerView |
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
import kotlinx.coroutines.GlobalScope |
|
|
|
|
@ -24,6 +30,7 @@ import net.pokeranalytics.android.calculus.bankroll.BankrollReport |
|
|
|
|
import net.pokeranalytics.android.calculus.bankroll.BankrollReportSetup |
|
|
|
|
import net.pokeranalytics.android.model.Criteria |
|
|
|
|
import net.pokeranalytics.android.model.LiveData |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.Deletable |
|
|
|
|
import net.pokeranalytics.android.model.realm.Bankroll |
|
|
|
|
import net.pokeranalytics.android.ui.activity.DataListActivity |
|
|
|
|
import net.pokeranalytics.android.ui.activity.EditableDataActivity |
|
|
|
|
@ -60,9 +67,12 @@ class BankrollFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSou |
|
|
|
|
|
|
|
|
|
private lateinit var parentActivity: PokerAnalyticsActivity |
|
|
|
|
private lateinit var bankrollAdapter: RowRepresentableAdapter |
|
|
|
|
|
|
|
|
|
private var rows: ArrayList<RowRepresentable> = ArrayList() |
|
|
|
|
private var bankrollReportForRow: HashMap<RowRepresentable, BankrollReport> = HashMap() |
|
|
|
|
|
|
|
|
|
private var deletedItem: RealmObject? = null |
|
|
|
|
private var lastDeletedItemPosition: Int = 0 |
|
|
|
|
private var lastItemClickedPosition: Int = 0 |
|
|
|
|
|
|
|
|
|
// Life Cycle |
|
|
|
|
|
|
|
|
|
@ -84,8 +94,7 @@ class BankrollFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSou |
|
|
|
|
if (needToDeleteItem) { |
|
|
|
|
GlobalScope.launch(Dispatchers.Main) { |
|
|
|
|
delay(300) |
|
|
|
|
//TODO: Delete bankroll |
|
|
|
|
//deleteItem(lastItemClickedPosition) |
|
|
|
|
deleteItem(lastItemClickedPosition) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -182,10 +191,10 @@ class BankrollFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSou |
|
|
|
|
parentActivity.supportActionBar?.setDisplayHomeAsUpEnabled(true) |
|
|
|
|
setHasOptionsMenu(true) |
|
|
|
|
|
|
|
|
|
val viewManager = LinearLayoutManager(requireContext()) |
|
|
|
|
|
|
|
|
|
bankrollAdapter = RowRepresentableAdapter(this, this) |
|
|
|
|
|
|
|
|
|
val viewManager = LinearLayoutManager(requireContext()) |
|
|
|
|
|
|
|
|
|
recyclerView.apply { |
|
|
|
|
setHasFixedSize(true) |
|
|
|
|
layoutManager = viewManager |
|
|
|
|
@ -203,4 +212,55 @@ class BankrollFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSou |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Delete item |
|
|
|
|
*/ |
|
|
|
|
private fun deleteItem(position: Int) { |
|
|
|
|
|
|
|
|
|
if (isDetached || activity == null) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Save the delete position & create a copy of the object |
|
|
|
|
val mRecentlyDeletedItem = rowRepresentableForPosition(position) |
|
|
|
|
lastDeletedItemPosition = position |
|
|
|
|
|
|
|
|
|
if (mRecentlyDeletedItem is RealmObject) { |
|
|
|
|
|
|
|
|
|
// Check if the object is valid for the deletion |
|
|
|
|
if ((mRecentlyDeletedItem as Deletable).isValidForDelete(this.getRealm())) { |
|
|
|
|
deletedItem = getRealm().copyFromRealm(mRecentlyDeletedItem) |
|
|
|
|
getRealm().executeTransaction { |
|
|
|
|
mRecentlyDeletedItem.deleteFromRealm() |
|
|
|
|
} |
|
|
|
|
bankrollAdapter.notifyItemRemoved(position) |
|
|
|
|
showUndoSnackBar() |
|
|
|
|
} else { |
|
|
|
|
bankrollAdapter.notifyItemChanged(position) |
|
|
|
|
val builder = AlertDialog.Builder(requireContext()) |
|
|
|
|
.setMessage((mRecentlyDeletedItem as Deletable).getFailedDeleteMessage()) |
|
|
|
|
.setNegativeButton(R.string.ok, null) |
|
|
|
|
builder.show() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Show undo snack bar |
|
|
|
|
*/ |
|
|
|
|
private fun showUndoSnackBar() { |
|
|
|
|
val message = String.format(getString(R.string.data_deleted), getString(R.string.bankroll)) |
|
|
|
|
val snackBar = Snackbar.make(constraintLayout, message, Snackbar.LENGTH_INDEFINITE) |
|
|
|
|
snackBar.setAction(R.string.cancel) { |
|
|
|
|
getRealm().executeTransaction { realm -> |
|
|
|
|
deletedItem?.let { |
|
|
|
|
realm.copyToRealmOrUpdate(it) |
|
|
|
|
bankrollAdapter.notifyItemInserted(lastDeletedItemPosition) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
snackBar.show() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |