diff --git a/app/src/main/java/com/cyb3rko/techniklogger/fragments/MissionFragment.kt b/app/src/main/java/com/cyb3rko/techniklogger/fragments/MissionFragment.kt index ace4492..cc84310 100644 --- a/app/src/main/java/com/cyb3rko/techniklogger/fragments/MissionFragment.kt +++ b/app/src/main/java/com/cyb3rko/techniklogger/fragments/MissionFragment.kt @@ -61,7 +61,118 @@ class MissionFragment : Fragment() { objectId = args.objectId - participationAdapter = ParticipationAdapter { + loadData() + + binding.addButton.setOnClickListener { + val name = sharedPref.getString(NAME, "invalid")!! + if (!adminMode) selfAdd(name) else bothAdd(name) + } + + emptyCheck() + + if (adminMode || sharedPref.getString(TECHNIKER_ID, "")!!.isNotBlank()) { + binding.addButton.visibility = View.VISIBLE + } + + if (adminMode) { + binding.resetButton.visibility = View.VISIBLE + binding.resetButton.setOnClickListener { + MaterialAlertDialogBuilder(myContext) + .setTitle("Techniker entfernen") + .setMessage("Möchtest du alle eingetragenen Techniker entfernen?") + .setPositiveButton("Ja") { _, _ -> + ParseController.fetchParticipations(objectId, false) { entries, e -> + if (e == null) { + ParseObject.deleteAllInBackground(entries) + participationAdapter.submitList(listOf()) + showDivider(false) + updateTechnikerCount(0) + } else { + Toasty.error(myContext, "Fehler bei Techniker-Abfrage", Toasty.LENGTH_SHORT).show() + Log.e("TechnikLogger.TechSuche", e.message.toString()) + } + } + } + .setNegativeButton("Abbrechen", null) + .show() + } + } + + binding.swipeRefreshLayout.apply { + setProgressBackgroundColorSchemeResource(R.color.refreshLayoutBackground) + setColorSchemeResources(R.color.refreshLayoutArrow) + setOnRefreshListener { + loadData() + } + } + + return root + } + + private fun loadData() { + ParseController.fetchMission(objectId) { mission, e -> + if (e == null) { + val date = SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).format(mission!!.date()) + time = mission.time() + duration = mission.duration + val prettyDuration = duration.toPrettyString() + + time = if (time != "") ", $time ($prettyDuration h)" else ", $prettyDuration h" + val dateTime = date + time + + binding.apply { + if (titleView.text != mission.name) { + titleView.text = mission.name + } + if (dateView.text != dateTime) { + dateView.text = dateTime + } + if (locationView.text != mission.location) { + locationView.text = mission.location + } + } + + loadParticipations() + } else { + if (e.message != null) { + if (e.message != "results not cached") { + Toasty.error(myContext, "Fehler bei Einsatz-Suche").show() + Log.e("TechnikLogger.EinsSuche", e.message.toString()) + } else { + Log.d("TechnikLogger.EinsSuche", "Empty cache, fetching data immediately.") + } + } + } + binding.swipeRefreshLayout.isRefreshing = false + } + } + + private fun loadParticipations() { + ParseController.fetchParticipations(objectId, true) { participations, e2 -> + if (e2 == null) { + if (participations.isNotEmpty()) { + participations.forEach { + if (it.duration == 0f) it.setDuration(duration) + } + + initializeRecyclerView() + + participationAdapter.submitList(participations) + updateTechnikerCount(participations.size) + showDivider() + } + } else { + Toasty.error(myContext, "Fehler bei Teilnehmer-Abfrage").show() + Log.e("TechnikLogger.TechSuche", e2.message.toString()) + } + } + } + + private fun initializeRecyclerView() { + if (this::participationAdapter.isInitialized) { + return + } + participationAdapter = ParticipationAdapter(duration) { if (adminMode || it.name == sharedPref.getString(NAME, "")) { val uhrzeit = if (it.time == "0") time else it.time val message = "Arbeitszeit:
$uhrzeit Uhr

" + @@ -165,113 +276,8 @@ class MissionFragment : Fragment() { .show() } } - - loadData() - binding.recyclerView.layoutManager = LinearLayoutManager(myContext) binding.recyclerView.adapter = participationAdapter - - binding.addButton.setOnClickListener { - val name = sharedPref.getString(NAME, "invalid")!! - if (!adminMode) selfAdd(name) else bothAdd(name) - } - - emptyCheck() - - if (adminMode || sharedPref.getString(TECHNIKER_ID, "")!!.isNotBlank()) { - binding.addButton.visibility = View.VISIBLE - } - - if (adminMode) { - binding.resetButton.visibility = View.VISIBLE - binding.resetButton.setOnClickListener { - MaterialAlertDialogBuilder(myContext) - .setTitle("Techniker entfernen") - .setMessage("Möchtest du alle eingetragenen Techniker entfernen?") - .setPositiveButton("Ja") { _, _ -> - ParseController.fetchParticipations(objectId, false) { entries, e -> - if (e == null) { - ParseObject.deleteAllInBackground(entries) - participationAdapter.submitList(listOf()) - showDivider(false) - updateTechnikerCount(0) - } else { - Toasty.error(myContext, "Fehler bei Techniker-Abfrage", Toasty.LENGTH_SHORT).show() - Log.e("TechnikLogger.TechSuche", e.message.toString()) - } - } - } - .setNegativeButton("Abbrechen", null) - .show() - } - } - - binding.swipeRefreshLayout.apply { - setProgressBackgroundColorSchemeResource(R.color.refreshLayoutBackground) - setColorSchemeResources(R.color.refreshLayoutArrow) - setOnRefreshListener { - loadData() - } - } - - return root - } - - private fun loadData() { - ParseController.fetchMission(objectId) { mission, e -> - if (e == null) { - val date = SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).format(mission!!.date()) - time = mission.time() - duration = mission.duration - val prettyDuration = duration.toPrettyString() - - time = if (time != "") ", $time ($prettyDuration h)" else ", $prettyDuration h" - val dateTime = date + time - - binding.apply { - if (titleView.text != mission.name) { - titleView.text = mission.name - } - if (dateView.text != dateTime) { - dateView.text = dateTime - } - if (locationView.text != mission.location) { - locationView.text = mission.location - } - } - - loadParticipations() - } else { - if (e.message != null) { - if (e.message != "results not cached") { - Toasty.error(myContext, "Fehler bei Einsatz-Suche").show() - Log.e("TechnikLogger.EinsSuche", e.message.toString()) - } else { - Log.d("TechnikLogger.EinsSuche", "Empty cache, fetching data immediately.") - } - } - } - binding.swipeRefreshLayout.isRefreshing = false - } - } - - private fun loadParticipations() { - ParseController.fetchParticipations(objectId, true) { participations, e2 -> - if (e2 == null) { - if (participations.isNotEmpty()) { - participations.forEach { - if (it.duration == 0f) it.setDuration(duration) - } - - participationAdapter.submitList(participations) - updateTechnikerCount(participations.size) - showDivider() - } - } else { - Toasty.error(myContext, "Fehler bei Teilnehmer-Abfrage").show() - Log.e("TechnikLogger.TechSuche", e2.message.toString()) - } - } } private fun memberExists(name: String): Boolean { @@ -400,7 +406,9 @@ class MissionFragment : Fragment() { } private fun emptyCheck() { - if (participationAdapter.currentList.isEmpty()) { + if (this::participationAdapter.isInitialized && + participationAdapter.currentList.isEmpty()) + { binding.divider.visibility = View.GONE } } diff --git a/app/src/main/java/com/cyb3rko/techniklogger/recycler/ParticipationAdapter.kt b/app/src/main/java/com/cyb3rko/techniklogger/recycler/ParticipationAdapter.kt index cdceef2..c8ec6ab 100644 --- a/app/src/main/java/com/cyb3rko/techniklogger/recycler/ParticipationAdapter.kt +++ b/app/src/main/java/com/cyb3rko/techniklogger/recycler/ParticipationAdapter.kt @@ -12,6 +12,7 @@ import com.cyb3rko.techniklogger.data.objects.Participation import com.cyb3rko.techniklogger.toPrettyString internal class ParticipationAdapter( + private val missionDuration: Float, val action: (participation: Participation) -> Unit ) : ListAdapter(ParticipationDiffCallback) { @@ -22,7 +23,11 @@ internal class ParticipationAdapter( override fun onBindViewHolder(holder: ViewHolder, position: Int) { val entry = getItem(position) - val title = "${entry.name}, ${entry.duration.toPrettyString()} h" + val title = if (missionDuration == entry.duration) { + entry.name + } else { + "${entry.name}, ${entry.duration.toPrettyString()} h" + } holder.titleView.text = title holder.titleView.setOnClickListener { action(entry)