|
|
|
|
@ -8,22 +8,26 @@ import android.view.MenuInflater |
|
|
|
|
import android.view.MenuItem |
|
|
|
|
import android.view.View |
|
|
|
|
import androidx.appcompat.app.AlertDialog |
|
|
|
|
import androidx.lifecycle.ViewModelProviders |
|
|
|
|
import net.pokeranalytics.android.R |
|
|
|
|
import net.pokeranalytics.android.exceptions.ConfigurationException |
|
|
|
|
import net.pokeranalytics.android.model.LiveData |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.Deletable |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.Savable |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.SaveValidityStatus |
|
|
|
|
import net.pokeranalytics.android.ui.activity.DataListActivity |
|
|
|
|
import net.pokeranalytics.android.ui.activity.EditableDataActivity |
|
|
|
|
import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
|
|
|
|
import net.pokeranalytics.android.ui.viewmodel.DataManagerViewModel |
|
|
|
|
|
|
|
|
|
open class DataManagerFragment : RealmFragment() { |
|
|
|
|
|
|
|
|
|
lateinit var item: Deletable |
|
|
|
|
protected val viewModel: DataManagerViewModel by lazy { |
|
|
|
|
ViewModelProviders.of(this).get(DataManagerViewModel::class.java) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected lateinit var liveDataType: LiveData |
|
|
|
|
protected var primaryKey: String? = null |
|
|
|
|
// lateinit var item: Deletable |
|
|
|
|
// protected lateinit var liveDataType: LiveData |
|
|
|
|
// protected var primaryKey: String? = null |
|
|
|
|
// protected var dataType: Int? = null |
|
|
|
|
|
|
|
|
|
var deleteButtonShouldAppear = false |
|
|
|
|
set(value) { |
|
|
|
|
@ -37,7 +41,6 @@ open class DataManagerFragment : RealmFragment() { |
|
|
|
|
this.updateMenuUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected var dataType: Int? = null |
|
|
|
|
|
|
|
|
|
private var editableMenu: Menu? = null |
|
|
|
|
|
|
|
|
|
@ -75,8 +78,8 @@ open class DataManagerFragment : RealmFragment() { |
|
|
|
|
*/ |
|
|
|
|
private fun loadItem() { |
|
|
|
|
|
|
|
|
|
this.item = this.liveDataType.updateOrCreate(this.getRealm(), primaryKey) |
|
|
|
|
this.deleteButtonShouldAppear = this.primaryKey != null |
|
|
|
|
// this.item = this.liveDataType.updateOrCreate(this.getRealm(), primaryKey) |
|
|
|
|
this.deleteButtonShouldAppear = this.viewModel.primaryKey != null |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -85,7 +88,7 @@ open class DataManagerFragment : RealmFragment() { |
|
|
|
|
*/ |
|
|
|
|
protected open fun saveData() { |
|
|
|
|
|
|
|
|
|
val savable = this.item |
|
|
|
|
val savable = this.viewModel.item |
|
|
|
|
this.willSaveData() |
|
|
|
|
|
|
|
|
|
when (savable) { |
|
|
|
|
@ -94,7 +97,7 @@ open class DataManagerFragment : RealmFragment() { |
|
|
|
|
when (status) { |
|
|
|
|
SaveValidityStatus.VALID -> { |
|
|
|
|
this.getRealm().executeTransaction { |
|
|
|
|
val managedItem = it.copyToRealmOrUpdate(this.item) |
|
|
|
|
val managedItem = it.copyToRealmOrUpdate(this.viewModel.item) |
|
|
|
|
if (managedItem is Savable) { |
|
|
|
|
val uniqueIdentifier = managedItem.id |
|
|
|
|
finishActivityWithResult(uniqueIdentifier) |
|
|
|
|
@ -132,14 +135,14 @@ open class DataManagerFragment : RealmFragment() { |
|
|
|
|
|
|
|
|
|
val realm = this.getRealm() |
|
|
|
|
|
|
|
|
|
if (this.item.isValidForDelete(realm)) { |
|
|
|
|
if (this.viewModel.item.isValidForDelete(realm)) { |
|
|
|
|
val intent = Intent() |
|
|
|
|
intent.putExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName, this.item.id) |
|
|
|
|
intent.putExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName, this.viewModel.item.id) |
|
|
|
|
activity?.setResult(Activity.RESULT_OK, intent) |
|
|
|
|
activity?.finish() |
|
|
|
|
} else { |
|
|
|
|
val status = this.item.getDeleteStatus(requireContext(), realm) |
|
|
|
|
val message = this.item.getFailedDeleteMessage(status) |
|
|
|
|
val status = this.viewModel.item.getDeleteStatus(requireContext(), realm) |
|
|
|
|
val message = this.viewModel.item.getFailedDeleteMessage(status) |
|
|
|
|
val builder = AlertDialog.Builder(requireContext()) |
|
|
|
|
.setMessage(message) |
|
|
|
|
.setNegativeButton(R.string.ok, null) |
|
|
|
|
@ -154,7 +157,7 @@ open class DataManagerFragment : RealmFragment() { |
|
|
|
|
*/ |
|
|
|
|
private fun finishActivityWithResult(uniqueIdentifier: String) { |
|
|
|
|
val intent = Intent() |
|
|
|
|
intent.putExtra(EditableDataActivity.IntentKey.DATA_TYPE.keyName, dataType) |
|
|
|
|
intent.putExtra(EditableDataActivity.IntentKey.DATA_TYPE.keyName, viewModel.dataType) |
|
|
|
|
intent.putExtra(EditableDataActivity.IntentKey.PRIMARY_KEY.keyName, uniqueIdentifier) |
|
|
|
|
activity?.setResult(Activity.RESULT_OK, intent) |
|
|
|
|
activity?.finish() |
|
|
|
|
|