You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.7 KiB
Kotlin
158 lines
5.7 KiB
Kotlin
package com.cyb3rko.techniklogger.fragments
|
|
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import androidx.fragment.app.Fragment
|
|
import androidx.navigation.fragment.findNavController
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import com.cyb3rko.techniklogger.*
|
|
import com.cyb3rko.techniklogger.CURRENT_YEAR
|
|
import com.cyb3rko.techniklogger.CURRENT_YEAR_NAME
|
|
import com.cyb3rko.techniklogger.data.ParseController
|
|
import com.cyb3rko.techniklogger.data.objects.Year
|
|
import com.cyb3rko.techniklogger.databinding.FragmentYearsBinding
|
|
import com.cyb3rko.techniklogger.recycler.YearAdapter
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
import com.parse.ParseObject
|
|
import es.dmoral.toasty.Toasty
|
|
|
|
class YearsFragment : Fragment() {
|
|
private var _binding: FragmentYearsBinding? = null
|
|
private lateinit var myContext: Context
|
|
|
|
private var adminMode: Boolean? = null
|
|
private lateinit var yearAdapter: YearAdapter
|
|
private var data: List<Year> = mutableListOf()
|
|
|
|
// This property is only valid between onCreateView and onDestroyView.
|
|
private val binding get() = _binding!!
|
|
|
|
override fun onCreateView(
|
|
inflater: LayoutInflater,
|
|
container: ViewGroup?,
|
|
savedInstanceState: Bundle?
|
|
): View {
|
|
_binding = FragmentYearsBinding.inflate(inflater, container, false)
|
|
val root = binding.root
|
|
myContext = requireContext()
|
|
return root
|
|
}
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
binding.loadingAnimation.playAnimation()
|
|
|
|
yearAdapter = YearAdapter { year ->
|
|
Safe.storeKey(myContext, CURRENT_YEAR, year.objectId)
|
|
Safe.storeKey(myContext, CURRENT_YEAR_NAME, year.name)
|
|
findNavController().navigate(R.id.navigation_listing)
|
|
}
|
|
binding.recyclerView.apply {
|
|
layoutManager = LinearLayoutManager(myContext)
|
|
adapter = yearAdapter
|
|
}
|
|
|
|
loadEntries()
|
|
|
|
binding.swipeRefreshLayout.apply {
|
|
setProgressBackgroundColorSchemeResource(R.color.refreshLayoutBackground)
|
|
setColorSchemeResources(R.color.refreshLayoutArrow)
|
|
setOnRefreshListener {
|
|
loadEntries()
|
|
}
|
|
}
|
|
|
|
binding.fab.setOnClickListener {
|
|
val current = yearAdapter.currentList[0]
|
|
val currentYear = current.name.split("/")[1]
|
|
val new = "$currentYear/${currentYear.toInt() + 1}"
|
|
MaterialAlertDialogBuilder(myContext)
|
|
.setTitle("Neues Schuljahr")
|
|
.setMessage("Möchtest du das Schuljahr $new hinzufügen?")
|
|
.setPositiveButton(android.R.string.ok) { _, _ ->
|
|
Year().apply {
|
|
put("name", new)
|
|
save()
|
|
loadEntries()
|
|
}
|
|
}
|
|
.show()
|
|
}
|
|
|
|
if (adminMode == null && Safe.getKey(myContext, NAME).isNotEmpty()) {
|
|
updateAdminStatus()
|
|
} else if (adminMode != null && adminMode!!) {
|
|
binding.fab.show()
|
|
}
|
|
}
|
|
|
|
private fun loadEntries() {
|
|
ParseController.fetchYears { years, e ->
|
|
if (e == null) {
|
|
data = years
|
|
showAnimation(false)
|
|
binding.swipeRefreshLayout.isRefreshing = false
|
|
yearAdapter.submitList(years)
|
|
(requireActivity() as MainActivity).setActionBarSubtitle(years.size.toString())
|
|
} else {
|
|
if (e.message != null) {
|
|
if (e.message != "results not cached") {
|
|
binding.swipeRefreshLayout.isRefreshing = false
|
|
if (data.isEmpty()) {
|
|
showAnimation(true, false)
|
|
}
|
|
showErrorToast("Abruf fehlgeschlagen")
|
|
logE("Abruf der Schuljahre fehlgeschlagen")
|
|
e.printStackTrace()
|
|
} else {
|
|
logD("Leerer Cache, rufe Daten direkt ab.")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun updateAdminStatus() {
|
|
ParseController.fetchAdminStatus(Safe.getKey(myContext, NAME)) { objectId, admin, e ->
|
|
if (e == null) {
|
|
if (admin!!) {
|
|
adminMode = true
|
|
Safe.storeBoolean(myContext, ADMIN, adminMode!!)
|
|
binding.fab.show()
|
|
} else {
|
|
adminMode = false
|
|
Safe.storeBoolean(myContext, ADMIN, adminMode!!)
|
|
binding.fab.hide()
|
|
}
|
|
Safe.storeKey(myContext, TECHNIKER_ID, objectId)
|
|
} else {
|
|
binding.fab.hide()
|
|
adminMode = false
|
|
Safe.storeKey(myContext, TECHNIKER_ID, "")
|
|
Safe.storeBoolean(myContext, ADMIN, adminMode!!)
|
|
showErrorToast("Adminstatus unbekannt", Toasty.LENGTH_SHORT)
|
|
logE("Adminstatus unbekannt")
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun showAnimation(show: Boolean, connected: Boolean = true) {
|
|
val viewVisibility = if (show) View.VISIBLE else View.INVISIBLE
|
|
val newSpeed = if (!connected) 1.2f else 1.5f
|
|
val animation = if (connected) "loading.json" else "no-connection.json"
|
|
binding.apply {
|
|
loadingAnimation.apply {
|
|
setAnimation(animation)
|
|
speed = newSpeed
|
|
visibility = viewVisibility
|
|
playAnimation()
|
|
}
|
|
}
|
|
}
|
|
}
|