Füge Verwaltung für Techniker hinzu
parent
fe5c7ca98c
commit
c8523a9e9a
@ -0,0 +1,144 @@
|
||||
package com.cyb3rko.techniklogger.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.afollestad.materialdialogs.LayoutMode
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
|
||||
import com.afollestad.materialdialogs.callbacks.onDismiss
|
||||
import com.afollestad.materialdialogs.customview.customView
|
||||
import com.cyb3rko.techniklogger.R
|
||||
import com.cyb3rko.techniklogger.databinding.FragmentManageTechnikerBinding
|
||||
import com.google.android.material.switchmaterial.SwitchMaterial
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import com.parse.ParseObject
|
||||
import com.parse.ParseQuery
|
||||
import es.dmoral.toasty.Toasty
|
||||
import kotlinx.android.synthetic.main.techniker_dialog.view.*
|
||||
|
||||
class ManageTechnikerFragment : Fragment() {
|
||||
private var _binding: FragmentManageTechnikerBinding? = null
|
||||
private lateinit var myContext: Context
|
||||
|
||||
private lateinit var techniker: MutableList<ParseObject>
|
||||
private val technikerNames = mutableListOf<String>()
|
||||
private val query = ParseQuery.getQuery<ParseObject>("Techniker")
|
||||
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
_binding = FragmentManageTechnikerBinding.inflate(inflater, container, false)
|
||||
val root = binding.root
|
||||
myContext = requireContext()
|
||||
|
||||
query.whereEqualTo("entlassen", false)
|
||||
query.limit = 100000
|
||||
|
||||
fetchTechniker()
|
||||
|
||||
binding.fab.setOnClickListener {
|
||||
MaterialDialog(myContext, BottomSheet(LayoutMode.WRAP_CONTENT)).show {
|
||||
title(text = "Techniker hinzufügen")
|
||||
customView(R.layout.techniker_dialog, horizontalPadding = true)
|
||||
positiveButton(text = "Hinzufügen") {
|
||||
val view = it.view
|
||||
val newName = view.name.text.toString()
|
||||
val newAdmin = view.admin.isChecked
|
||||
ParseObject.create("Techniker").apply {
|
||||
put("name", newName)
|
||||
put("admin", newAdmin)
|
||||
saveInBackground {
|
||||
if (it == null) {
|
||||
Toast.makeText(myContext, "Hinzugefügt", Toast.LENGTH_SHORT).show()
|
||||
fetchTechniker()
|
||||
} else {
|
||||
Toasty.error(myContext, "Fehler bei Speicherung").show()
|
||||
Log.e("TechnikLogger.Techniker", it.message.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
private fun fetchTechniker() {
|
||||
query.findInBackground { objects, e ->
|
||||
if (e == null) {
|
||||
techniker = objects.sortedBy {
|
||||
it.getString("name")!!
|
||||
}.toMutableList()
|
||||
|
||||
technikerNames.clear()
|
||||
objects.forEach {
|
||||
technikerNames.add(it.getString("name")!!)
|
||||
}
|
||||
technikerNames.sort()
|
||||
|
||||
val adapter = ArrayAdapter(myContext, android.R.layout.simple_list_item_1, technikerNames)
|
||||
binding.list.adapter = adapter
|
||||
binding.list.setOnItemClickListener { _, _, index, _ ->
|
||||
val dialogView = layoutInflater.inflate(R.layout.techniker_dialog, null)
|
||||
dialogView.findViewById<TextInputEditText>(R.id.name).setText(technikerNames[index])
|
||||
dialogView.findViewById<SwitchMaterial>(R.id.admin).isChecked = techniker[index].getBoolean("admin")
|
||||
MaterialDialog(myContext, BottomSheet(LayoutMode.WRAP_CONTENT)).show {
|
||||
title(text = "Techniker-Info")
|
||||
customView(0, dialogView, horizontalPadding = true)
|
||||
onDismiss {
|
||||
val view = it.view
|
||||
val newName = view.name.text.toString()
|
||||
val newAdmin = view.admin.isChecked
|
||||
val parseObject = techniker[index]
|
||||
if (newName != parseObject.getString("name") || newAdmin != parseObject.getBoolean("admin")) {
|
||||
parseObject.apply {
|
||||
put("name", newName)
|
||||
put("admin", newAdmin)
|
||||
saveInBackground {
|
||||
if (it == null) {
|
||||
Toast.makeText(myContext, "Gespeichert", Toast.LENGTH_SHORT).show()
|
||||
fetchTechniker()
|
||||
} else {
|
||||
Toasty.error(myContext, "Fehler bei Speicherung").show()
|
||||
Log.e("TechnikLogger.Techniker", it.message.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
negativeButton(text = "Entlassen") {
|
||||
val name = technikerNames[index]
|
||||
MaterialDialog(myContext).show {
|
||||
title(text = "Entlassung")
|
||||
message(text = "Soll '$name' wirklich entlassen werden?")
|
||||
positiveButton(text = "Ja") {
|
||||
techniker[index].apply {
|
||||
put("entlassen", true)
|
||||
saveInBackground {
|
||||
if (it == null) {
|
||||
Toast.makeText(myContext, "Gespeichert", Toast.LENGTH_SHORT).show()
|
||||
fetchTechniker()
|
||||
} else {
|
||||
Toasty.error(myContext, "Fehler bei Speicherung").show()
|
||||
Log.e("TechnikLogger.Techniker", it.message.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
negativeButton(text = "Nein")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ListView
|
||||
android:id="@+id/list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:src="@drawable/_icon_add"
|
||||
android:tint="@color/drawableTint" />
|
||||
|
||||
</RelativeLayout>
|
||||
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingBottom="35dp">
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:helperTextEnabled="true">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:maxLines="1"
|
||||
android:lines="1"
|
||||
android:singleLine="true"
|
||||
android:inputType="textPersonName"
|
||||
android:hint="Name"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="20dp"
|
||||
android:text="Admin"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<com.google.android.material.switchmaterial.SwitchMaterial
|
||||
android:id="@+id/admin"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp" />
|
||||
|
||||
</LinearLayout>
|
||||
Loading…
Reference in New Issue