|
|
|
|
@ -7,6 +7,7 @@ import android.view.animation.OvershootInterpolator |
|
|
|
|
import android.widget.Toast |
|
|
|
|
import androidx.appcompat.app.AlertDialog |
|
|
|
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator |
|
|
|
|
import androidx.lifecycle.ViewModelProviders |
|
|
|
|
import androidx.recyclerview.widget.DiffUtil |
|
|
|
|
import com.crashlytics.android.Crashlytics |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_session.* |
|
|
|
|
@ -30,6 +31,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback |
|
|
|
|
import net.pokeranalytics.android.ui.view.SmoothScrollLinearLayoutManager |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow |
|
|
|
|
import net.pokeranalytics.android.ui.viewmodel.SessionViewModel |
|
|
|
|
import net.pokeranalytics.android.util.extensions.findById |
|
|
|
|
import net.pokeranalytics.android.util.extensions.getNextMinuteInMilliseconds |
|
|
|
|
import java.util.* |
|
|
|
|
@ -37,9 +39,10 @@ import java.util.* |
|
|
|
|
|
|
|
|
|
class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
|
|
|
|
|
private lateinit var viewModel: SessionViewModel |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
const val TIMER_DELAY = 60000L |
|
|
|
|
|
|
|
|
|
const val REQUEST_CODE_NEW_CUSTOM_FIELD = 1000 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -48,7 +51,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
|
|
|
|
|
private var sessionMenu: Menu? = null |
|
|
|
|
private val oldRows: ArrayList<RowRepresentable> = ArrayList() |
|
|
|
|
private var sessionHasBeenCustomized = false |
|
|
|
|
private var sessionHasBeenUserCustomized = false |
|
|
|
|
private val handler: Handler = Handler() |
|
|
|
|
|
|
|
|
|
private val refreshTimer: Runnable = object : Runnable { |
|
|
|
|
@ -58,51 +61,43 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onResume() { |
|
|
|
|
super.onResume() |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set fragment data |
|
|
|
|
*/ |
|
|
|
|
fun setData(isTournament: Boolean, sessionId: String? = null, duplicate: Boolean) { |
|
|
|
|
|
|
|
|
|
val realm = getRealm() |
|
|
|
|
if (sessionId != null) { |
|
|
|
|
|
|
|
|
|
val sessionRealm = realm.findById<Session>(sessionId) |
|
|
|
|
if (sessionRealm != null) { |
|
|
|
|
Crashlytics.log("session valid=${currentSession.isValid}, managed=${currentSession.isManaged}, loaded=${currentSession.isLoaded} ") |
|
|
|
|
|
|
|
|
|
if (duplicate) { // duplicate session |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
val session = sessionRealm.duplicate() |
|
|
|
|
currentSession = session |
|
|
|
|
} |
|
|
|
|
sessionHasBeenCustomized = false |
|
|
|
|
} else { // show existing session |
|
|
|
|
currentSession = sessionRealm |
|
|
|
|
sessionHasBeenCustomized = true |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
throw PAIllegalStateException("Session cannot be null here, session id = $sessionId") |
|
|
|
|
} |
|
|
|
|
} else { // create new session |
|
|
|
|
realm.executeTransaction { executeRealm -> |
|
|
|
|
currentSession = Session.newInstance(executeRealm, isTournament) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, null, requireContext()) |
|
|
|
|
this.refreshTimer() |
|
|
|
|
} |
|
|
|
|
// Find the nearest location around the user |
|
|
|
|
parentActivity?.findNearestLocation { |
|
|
|
|
it?.let { location -> |
|
|
|
|
realm.executeTransaction { executeRealm -> |
|
|
|
|
val realmLocation = executeRealm.findById<Location>(location.id) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, realmLocation, requireContext()) |
|
|
|
|
|
|
|
|
|
currentSession.location = realmLocation |
|
|
|
|
private fun refreshTimer() { |
|
|
|
|
this.updateSessionUI(false) |
|
|
|
|
} |
|
|
|
|
updateSessionUI(true) |
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
|
|
|
super.onCreate(savedInstanceState) |
|
|
|
|
|
|
|
|
|
this.viewModel = activity?.run { |
|
|
|
|
ViewModelProviders.of(this).get(SessionViewModel::class.java) |
|
|
|
|
} ?: throw Exception("Invalid Activity") |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState) |
|
|
|
|
return inflater.inflate(R.layout.fragment_session, container, false) |
|
|
|
|
} |
|
|
|
|
sessionHasBeenCustomized = false |
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
|
|
|
|
super.onViewCreated(view, savedInstanceState) |
|
|
|
|
loadOrCreateSession() |
|
|
|
|
initUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Init UI |
|
|
|
|
*/ |
|
|
|
|
private fun initUI() { |
|
|
|
|
|
|
|
|
|
toolbar.title = if (currentSession.isTournament()) getString(R.string.tournament) else getString(R.string.cash_game) |
|
|
|
|
collapsingToolbar.title = toolbar.title |
|
|
|
|
|
|
|
|
|
@ -110,28 +105,26 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
recyclerView.adapter = sessionAdapter |
|
|
|
|
|
|
|
|
|
updateSessionUI(true) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onResume() { |
|
|
|
|
super.onResume() |
|
|
|
|
|
|
|
|
|
Crashlytics.log("session valid=${currentSession.isValid}, managed=${currentSession.isManaged}, loaded=${currentSession.isLoaded} ") |
|
|
|
|
setDisplayHomeAsUpEnabled(true) |
|
|
|
|
|
|
|
|
|
this.refreshTimer() |
|
|
|
|
val viewManager = SmoothScrollLinearLayoutManager(requireContext()) |
|
|
|
|
recyclerView.apply { |
|
|
|
|
setHasFixedSize(true) |
|
|
|
|
layoutManager = viewManager |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun refreshTimer() { |
|
|
|
|
this.updateSessionUI(false) |
|
|
|
|
floatingActionButton.setOnClickListener { |
|
|
|
|
if (this.currentSession.isValidForSave()) { |
|
|
|
|
sessionHasBeenUserCustomized = true |
|
|
|
|
manageSessionState() |
|
|
|
|
} else { |
|
|
|
|
val builder = AlertDialog.Builder(requireContext()) |
|
|
|
|
.setMessage(this.currentSession.getFailedSaveMessage(SaveValidityStatus.DATA_INVALID)) |
|
|
|
|
.setNegativeButton(R.string.ok, null) |
|
|
|
|
builder.show() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState) |
|
|
|
|
return inflater.inflate(R.layout.fragment_session, container, false) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
|
|
|
|
super.onViewCreated(view, savedInstanceState) |
|
|
|
|
initUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onPause() { |
|
|
|
|
@ -157,6 +150,50 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun loadOrCreateSession() { |
|
|
|
|
|
|
|
|
|
val realm = getRealm() |
|
|
|
|
|
|
|
|
|
val sessionId = this.viewModel.sessionId |
|
|
|
|
if (sessionId != null) { |
|
|
|
|
|
|
|
|
|
val sessionRealm = realm.findById<Session>(sessionId) |
|
|
|
|
if (sessionRealm != null) { |
|
|
|
|
|
|
|
|
|
if (this.viewModel.duplicate) { // duplicate session |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
val session = sessionRealm.duplicate() |
|
|
|
|
currentSession = session |
|
|
|
|
} |
|
|
|
|
sessionHasBeenUserCustomized = false |
|
|
|
|
} else { // show existing session |
|
|
|
|
currentSession = sessionRealm |
|
|
|
|
sessionHasBeenUserCustomized = true |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
throw PAIllegalStateException("Session cannot be null here, session id = $sessionId") |
|
|
|
|
} |
|
|
|
|
} else { // create new session |
|
|
|
|
realm.executeTransaction { executeRealm -> |
|
|
|
|
currentSession = Session.newInstance(executeRealm, this.viewModel.isTournament) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, null, requireContext()) |
|
|
|
|
} |
|
|
|
|
// Find the nearest location around the user |
|
|
|
|
parentActivity?.findNearestLocation { |
|
|
|
|
it?.let { location -> |
|
|
|
|
realm.executeTransaction { executeRealm -> |
|
|
|
|
val realmLocation = executeRealm.findById<Location>(location.id) |
|
|
|
|
FavoriteSessionFinder.copyParametersFromFavoriteSession(currentSession, realmLocation, requireContext()) |
|
|
|
|
|
|
|
|
|
currentSession.location = realmLocation |
|
|
|
|
} |
|
|
|
|
updateSessionUI(true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
sessionHasBeenUserCustomized = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
|
|
|
|
if (fromAction) { |
|
|
|
|
Toast.makeText(requireContext(), "Action for row: $row", Toast.LENGTH_SHORT).show() |
|
|
|
|
@ -189,7 +226,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onRowValueChanged(value: Any?, row: RowRepresentable) { |
|
|
|
|
sessionHasBeenCustomized = true |
|
|
|
|
sessionHasBeenUserCustomized = true |
|
|
|
|
try { |
|
|
|
|
currentSession.updateValue(value, row) |
|
|
|
|
} catch (e: PAIllegalStateException) { |
|
|
|
|
@ -203,32 +240,6 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Init UI |
|
|
|
|
*/ |
|
|
|
|
private fun initUI() { |
|
|
|
|
|
|
|
|
|
setDisplayHomeAsUpEnabled(true) |
|
|
|
|
|
|
|
|
|
val viewManager = SmoothScrollLinearLayoutManager(requireContext()) |
|
|
|
|
recyclerView.apply { |
|
|
|
|
setHasFixedSize(true) |
|
|
|
|
layoutManager = viewManager |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
floatingActionButton.setOnClickListener { |
|
|
|
|
if (this.currentSession.isValidForSave()) { |
|
|
|
|
sessionHasBeenCustomized = true |
|
|
|
|
manageSessionState() |
|
|
|
|
} else { |
|
|
|
|
val builder = AlertDialog.Builder(requireContext()) |
|
|
|
|
.setMessage(this.currentSession.getFailedSaveMessage(SaveValidityStatus.DATA_INVALID)) |
|
|
|
|
.setNegativeButton(R.string.ok, null) |
|
|
|
|
builder.show() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update the UI with the session data |
|
|
|
|
* Should be called after the initialization of the session |
|
|
|
|
@ -241,8 +252,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
|
|
|
|
|
val animationDuration = if (firstDisplay) 0L else 300L |
|
|
|
|
|
|
|
|
|
val state = currentSession.getState() |
|
|
|
|
when (state) { |
|
|
|
|
when (currentSession.getState()) { |
|
|
|
|
SessionState.PENDING, SessionState.PLANNED -> { |
|
|
|
|
sessionMenu?.findItem(R.id.restart)?.isVisible = false |
|
|
|
|
floatingActionButton.setImageResource(R.drawable.ic_outline_play) |
|
|
|
|
@ -376,7 +386,7 @@ class SessionFragment : RealmFragment(), RowRepresentableDelegate { |
|
|
|
|
*/ |
|
|
|
|
override fun onBackPressed() { |
|
|
|
|
super.onBackPressed() |
|
|
|
|
if (!sessionHasBeenCustomized) { |
|
|
|
|
if (!sessionHasBeenUserCustomized) { |
|
|
|
|
currentSession.delete() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|