|
|
|
|
@ -1,5 +1,7 @@ |
|
|
|
|
package net.pokeranalytics.android.ui.fragment |
|
|
|
|
|
|
|
|
|
import android.app.Activity |
|
|
|
|
import android.content.Intent |
|
|
|
|
import android.os.Bundle |
|
|
|
|
import android.view.LayoutInflater |
|
|
|
|
import android.view.View |
|
|
|
|
@ -11,9 +13,14 @@ import com.google.android.material.snackbar.Snackbar |
|
|
|
|
import io.realm.RealmObject |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_data_list.* |
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
import kotlinx.coroutines.GlobalScope |
|
|
|
|
import kotlinx.coroutines.delay |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import net.pokeranalytics.android.R |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.Deletable |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.Identifiable |
|
|
|
|
import net.pokeranalytics.android.ui.activity.DataListActivity |
|
|
|
|
import net.pokeranalytics.android.ui.activity.EditableDataActivity |
|
|
|
|
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity |
|
|
|
|
import net.pokeranalytics.android.ui.adapter.LiveRowRepresentableDataSource |
|
|
|
|
@ -28,12 +35,17 @@ import net.pokeranalytics.android.ui.view.rowrepresentable.SettingRow |
|
|
|
|
|
|
|
|
|
class DataListFragment : PokerAnalyticsFragment(), LiveRowRepresentableDataSource, RowRepresentableDelegate { |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
const val REQUEST_CODE_DETAILS = 1000 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private lateinit var dataType: SettingRow |
|
|
|
|
private lateinit var items: RealmResults<*> |
|
|
|
|
private lateinit var dataListAdapter: RowRepresentableAdapter |
|
|
|
|
|
|
|
|
|
private var deletedItem: RealmObject? = null |
|
|
|
|
private var lastDeletedItemPosition: Int = 0 |
|
|
|
|
private var lastItemClickedPosition: Int = 0 |
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
return inflater.inflate(R.layout.fragment_data_list, container, false) |
|
|
|
|
@ -44,6 +56,19 @@ class DataListFragment : PokerAnalyticsFragment(), LiveRowRepresentableDataSourc |
|
|
|
|
initUI() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
|
|
|
|
super.onActivityResult(requestCode, resultCode, data) |
|
|
|
|
if (requestCode == REQUEST_CODE_DETAILS && resultCode == Activity.RESULT_OK) { |
|
|
|
|
val needToDeleteItem = data?.getBooleanExtra(DataListActivity.IntentKey.ITEM_DELETED.keyName, false) ?: false |
|
|
|
|
if (needToDeleteItem) { |
|
|
|
|
GlobalScope.launch(Dispatchers.Main) { |
|
|
|
|
delay(300) |
|
|
|
|
deleteItem(lastItemClickedPosition) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onResume() { |
|
|
|
|
super.onResume() |
|
|
|
|
this.recyclerView?.adapter?.notifyDataSetChanged() |
|
|
|
|
@ -68,10 +93,12 @@ class DataListFragment : PokerAnalyticsFragment(), LiveRowRepresentableDataSourc |
|
|
|
|
|
|
|
|
|
override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) { |
|
|
|
|
this.dataType.relatedResultsRepresentable?.let { |
|
|
|
|
EditableDataActivity.newInstance( |
|
|
|
|
requireContext(), |
|
|
|
|
lastItemClickedPosition = position |
|
|
|
|
EditableDataActivity.newInstanceForResult( |
|
|
|
|
this, |
|
|
|
|
it.ordinal, |
|
|
|
|
(row as Identifiable).id |
|
|
|
|
(row as Identifiable).id, |
|
|
|
|
REQUEST_CODE_DETAILS |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -94,6 +121,37 @@ class DataListFragment : PokerAnalyticsFragment(), LiveRowRepresentableDataSourc |
|
|
|
|
dataListAdapter = RowRepresentableAdapter(this, this) |
|
|
|
|
|
|
|
|
|
val swipeToDelete = SwipeToDeleteCallback(dataListAdapter) { position -> |
|
|
|
|
deleteItem(position) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val itemTouchHelper = ItemTouchHelper(swipeToDelete) |
|
|
|
|
|
|
|
|
|
recyclerView.apply { |
|
|
|
|
setHasFixedSize(true) |
|
|
|
|
layoutManager = viewManager |
|
|
|
|
adapter = dataListAdapter |
|
|
|
|
itemTouchHelper.attachToRecyclerView(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.addButton.setOnClickListener { |
|
|
|
|
this.dataType.relatedResultsRepresentable?.let { |
|
|
|
|
EditableDataActivity.newInstance( |
|
|
|
|
requireContext(), |
|
|
|
|
dataType = it.ordinal, |
|
|
|
|
primaryKey = null |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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) |
|
|
|
|
@ -119,26 +177,6 @@ class DataListFragment : PokerAnalyticsFragment(), LiveRowRepresentableDataSourc |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val itemTouchHelper = ItemTouchHelper(swipeToDelete) |
|
|
|
|
|
|
|
|
|
recyclerView.apply { |
|
|
|
|
setHasFixedSize(true) |
|
|
|
|
layoutManager = viewManager |
|
|
|
|
adapter = dataListAdapter |
|
|
|
|
itemTouchHelper.attachToRecyclerView(this) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.addButton.setOnClickListener { |
|
|
|
|
this.dataType.relatedResultsRepresentable?.let { |
|
|
|
|
EditableDataActivity.newInstance( |
|
|
|
|
requireContext(), |
|
|
|
|
dataType = it.ordinal, |
|
|
|
|
primaryKey = null |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Show undo snack bar |
|
|
|
|
*/ |
|
|
|
|
|