Hinzufügen anderer Techniker zu einem Eintrag ermöglicht (als Admin)

master
Niko Diamadis 5 years ago
parent abdb64e79e
commit a5444209fd

@ -9,29 +9,29 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.input.getInputField import com.afollestad.materialdialogs.input.getInputField
import com.afollestad.materialdialogs.input.input import com.afollestad.materialdialogs.input.input
import com.afollestad.materialdialogs.list.isItemChecked
import com.afollestad.materialdialogs.list.listItemsMultiChoice
import com.afollestad.materialdialogs.list.uncheckItems
import com.cyb3rko.techniklogger.recycler.ProjectTechnikerViewHolder import com.cyb3rko.techniklogger.recycler.ProjectTechnikerViewHolder
import com.cyb3rko.techniklogger.recycler.ProjectTechnikerViewState import com.cyb3rko.techniklogger.recycler.ProjectTechnikerViewState
import com.google.firebase.database.* import com.google.firebase.database.*
import es.dmoral.toasty.Toasty import es.dmoral.toasty.Toasty
import java.lang.IndexOutOfBoundsException import java.lang.IndexOutOfBoundsException
import me.ibrahimyilmaz.kiel.adapter.RecyclerViewAdapter.Companion.adapterOf import me.ibrahimyilmaz.kiel.adapter.RecyclerViewAdapter.Companion.adapterOf
import kotlinx.android.synthetic.main.activity_project.* import kotlinx.android.synthetic.main.activity_project.*
import kotlinx.android.synthetic.main.activity_project.recycler_view import kotlinx.android.synthetic.main.activity_project.recycler_view
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
class ProjectActivity : AppCompatActivity() { class ProjectActivity : AppCompatActivity() {
private var addTechniker = mutableListOf<String>()
private var adminMode = false private var adminMode = false
private var childKey = "" private var childKey = ""
private lateinit var databaseReference: DatabaseReference private lateinit var databaseReference: DatabaseReference
@ -63,11 +63,11 @@ class ProjectActivity : AppCompatActivity() {
vh.itemView.setOnClickListener { vh.itemView.setOnClickListener {
MaterialDialog(this@ProjectActivity) MaterialDialog(this@ProjectActivity)
.show { .show {
message(0, "Möchtest du \'${text.name}\' entfernen?") message(text = "Möchtest du \'${text.name}\' entfernen?")
positiveButton(0, "Ja") { positiveButton(text = "Ja") {
databaseReference.child("techniker").child(text.key).removeValue() databaseReference.child("techniker").child(text.key).removeValue()
} }
negativeButton(0, "Abbrechen") negativeButton(text = "Abbrechen")
} }
} }
} }
@ -116,36 +116,118 @@ class ProjectActivity : AppCompatActivity() {
setDurationEventListener() setDurationEventListener()
add_button.setOnClickListener { add_button.setOnClickListener {
val name = getSharedPreferences("Safe", 0).getString("name", "invalid") val name = getSharedPreferences("Safe", 0).getString("name", "invalid")!!
var existing = false if (!adminMode) selfAdd(name) else bothAdd(name)
for (techniker in techniker) {
if (name == techniker.name) {
existing = true
}
}
if (!existing) {
MaterialDialog(this)
.show {
message(0, "Möchtest du dich als involvierter Techniker eintragen?")
positiveButton(0, "Ja") {
databaseReference.child("techniker").push().setValue(name)
}
negativeButton(0, "Abbrechen")
}
} else {
MaterialDialog(this)
.show {
message(0, "Du bist bereits eingetragen.")
positiveButton(0, "Ok")
}
}
} }
emptyCheck() emptyCheck()
} }
private fun technikerExists(name: String): Boolean {
for (techniker in techniker) {
if (name == techniker.name) {
return true
}
}
return false
}
private fun bothAdd(name: String) {
MaterialDialog(this)
.show {
message(text = "Möchtest du dich oder andere eintragen?")
positiveButton(text = "Mich") {
selfAdd(name, true)
}
negativeButton(text = "Andere") {
otherAdd()
}
}
}
private fun selfAdd(name: String, skip: Boolean = false) {
if (!technikerExists(name)) {
if (!skip) {
MaterialDialog(this).show {
message(text = "Möchtest du dich als involvierter Techniker eintragen?")
positiveButton(text = "Ja") {
databaseReference.child("techniker").push().setValue(name)
}
negativeButton(text = "Abbrechen")
}
} else {
databaseReference.child("techniker").push().setValue(name)
}
} else {
MaterialDialog(this)
.show {
message(text = "Du bist bereits eingetragen.")
positiveButton(text = "Ok")
}
}
}
private fun otherAdd() {
val technikerReference = FirebaseDatabase.getInstance().reference.child("techniker")
val childReadListener = getReadListener(technikerReference)
technikerReference.addChildEventListener(childReadListener)
technikerReference.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
technikerReference.removeEventListener(childReadListener)
technikerReference.removeEventListener(this)
addTechniker.sort()
val currentSelection = mutableListOf<Int>()
addTechniker.forEachIndexed { index, s ->
techniker.forEach {
if (s == it.name) currentSelection.add(index)
}
}
val intArray = currentSelection.toIntArray()
MaterialDialog(this@ProjectActivity).show {
listItemsMultiChoice(items = addTechniker, initialSelection = intArray, disabledIndices = intArray)
positiveButton(text = "Hinzufügen") {
it.uncheckItems(intArray)
GlobalScope.launch {
addTechniker.forEachIndexed { index, s ->
if (it.isItemChecked(index)) {
databaseReference.child("techniker").push().setValue(s)
}
}
addTechniker.clear()
}
}
negativeButton(text = "Abbrechen") {
addTechniker.clear()
}
}
}
}
override fun onCancelled(error: DatabaseError) {
technikerReference.removeEventListener(childReadListener)
technikerReference.removeEventListener(this)
}
})
}
private fun getReadListener(databaseRef: DatabaseReference): ChildEventListener {
return object : ChildEventListener {
override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
addTechniker.add(snapshot.value.toString())
}
override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) {}
override fun onChildRemoved(snapshot: DataSnapshot) {}
override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {}
override fun onCancelled(error: DatabaseError) {
databaseRef.removeEventListener(this)
}
}
}
private fun setValueEventListener(childName: String, textView: TextView) { private fun setValueEventListener(childName: String, textView: TextView) {
val valueEventListener = object: ValueEventListener { val valueEventListener = object: ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) { override fun onDataChange(snapshot: DataSnapshot) {
@ -225,7 +307,7 @@ class ProjectActivity : AppCompatActivity() {
FirebaseDatabase.getInstance().reference.addChildEventListener(listener) FirebaseDatabase.getInstance().reference.addChildEventListener(listener)
MaterialDialog(this) MaterialDialog(this)
.title(0, "Bitte gebe den Admin-Key ein") .title(text = "Bitte gebe den Admin-Key ein")
.show { .show {
input(hint = "Admin-Key", waitForPositiveButton = false) { _, inputKey -> input(hint = "Admin-Key", waitForPositiveButton = false) { _, inputKey ->
positiveButton { positiveButton {
@ -239,7 +321,7 @@ class ProjectActivity : AppCompatActivity() {
} }
} }
} }
positiveButton(0, "Speichern") positiveButton(text = "Speichern")
} }
} else { } else {
sharedPref.edit().putBoolean("admin", false).apply() sharedPref.edit().putBoolean("admin", false).apply()

Loading…
Cancel
Save