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.
165 lines
6.3 KiB
Kotlin
165 lines
6.3 KiB
Kotlin
package com.cyb3rko.techniklogger.fragments
|
|
|
|
import android.content.Context
|
|
import android.content.SharedPreferences
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
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.SHARED_PREFERENCE
|
|
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()
|
|
private lateinit var sharedPref: SharedPreferences
|
|
private lateinit var sharedPrefEditor: SharedPreferences.Editor
|
|
|
|
// 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)
|
|
|
|
sharedPref = myContext.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE)
|
|
sharedPrefEditor = sharedPref.edit()
|
|
|
|
binding.loadingAnimation.playAnimation()
|
|
|
|
yearAdapter = YearAdapter { year ->
|
|
sharedPrefEditor.putString(CURRENT_YEAR, year.objectId)
|
|
sharedPrefEditor.putString(CURRENT_YEAR_NAME, year.name).commit()
|
|
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) { _, _ ->
|
|
ParseObject("Jahr").apply {
|
|
put("name", new)
|
|
save()
|
|
loadEntries()
|
|
}
|
|
}
|
|
.show()
|
|
}
|
|
|
|
if (adminMode == null && sharedPref.getString("name", "") != "") {
|
|
updateAdminStatus()
|
|
} else if (adminMode != null && adminMode!!) {
|
|
binding.fab.visibility = View.VISIBLE
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
Toasty.error(myContext, "Abruf fehlgeschlagen", Toasty.LENGTH_SHORT).show()
|
|
Log.e("TechnikLogger.JahrSuche", e.message.toString())
|
|
e.printStackTrace()
|
|
} else {
|
|
Log.d("TechnikLogger.EinsSuche", "Empty cache, fetching data immediately.")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun updateAdminStatus() {
|
|
ParseController.fetchAdminStatus(sharedPref.getString("name", "")) { objectId, admin, e ->
|
|
if (e == null) {
|
|
if (admin!!) {
|
|
adminMode = true
|
|
sharedPrefEditor.putBoolean("admin", adminMode!!)
|
|
binding.fab.visibility = View.VISIBLE
|
|
} else {
|
|
adminMode = false
|
|
sharedPrefEditor.putBoolean("admin", adminMode!!)
|
|
binding.fab.visibility = View.INVISIBLE
|
|
}
|
|
sharedPrefEditor.putString("technikerId", objectId).apply()
|
|
} else {
|
|
binding.fab.visibility = View.INVISIBLE
|
|
adminMode = false
|
|
sharedPrefEditor.putString("technikerId", "")
|
|
sharedPrefEditor.putBoolean("admin", adminMode!!).apply()
|
|
Toasty.error(myContext, "Adminstatus unbekannt", Toasty.LENGTH_SHORT).show()
|
|
Log.e("TechnikLogger.TechSuche", e.message.toString())
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
}
|
|
}
|