|
|
|
|
@ -9,29 +9,29 @@ import android.view.MenuItem
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.widget.TextView
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
|
|
|
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
|
|
|
|
import com.afollestad.materialdialogs.input.getInputField
|
|
|
|
|
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.ProjectTechnikerViewState
|
|
|
|
|
import com.google.firebase.database.*
|
|
|
|
|
|
|
|
|
|
import es.dmoral.toasty.Toasty
|
|
|
|
|
|
|
|
|
|
import java.lang.IndexOutOfBoundsException
|
|
|
|
|
|
|
|
|
|
import me.ibrahimyilmaz.kiel.adapter.RecyclerViewAdapter.Companion.adapterOf
|
|
|
|
|
|
|
|
|
|
import kotlinx.android.synthetic.main.activity_project.*
|
|
|
|
|
import kotlinx.android.synthetic.main.activity_project.recycler_view
|
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
|
|
class ProjectActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
private var addTechniker = mutableListOf<String>()
|
|
|
|
|
private var adminMode = false
|
|
|
|
|
private var childKey = ""
|
|
|
|
|
private lateinit var databaseReference: DatabaseReference
|
|
|
|
|
@ -63,11 +63,11 @@ class ProjectActivity : AppCompatActivity() {
|
|
|
|
|
vh.itemView.setOnClickListener {
|
|
|
|
|
MaterialDialog(this@ProjectActivity)
|
|
|
|
|
.show {
|
|
|
|
|
message(0, "Möchtest du \'${text.name}\' entfernen?")
|
|
|
|
|
positiveButton(0, "Ja") {
|
|
|
|
|
message(text = "Möchtest du \'${text.name}\' entfernen?")
|
|
|
|
|
positiveButton(text = "Ja") {
|
|
|
|
|
databaseReference.child("techniker").child(text.key).removeValue()
|
|
|
|
|
}
|
|
|
|
|
negativeButton(0, "Abbrechen")
|
|
|
|
|
negativeButton(text = "Abbrechen")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -116,34 +116,116 @@ class ProjectActivity : AppCompatActivity() {
|
|
|
|
|
setDurationEventListener()
|
|
|
|
|
|
|
|
|
|
add_button.setOnClickListener {
|
|
|
|
|
val name = getSharedPreferences("Safe", 0).getString("name", "invalid")
|
|
|
|
|
var existing = false
|
|
|
|
|
val name = getSharedPreferences("Safe", 0).getString("name", "invalid")!!
|
|
|
|
|
if (!adminMode) selfAdd(name) else bothAdd(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emptyCheck()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun technikerExists(name: String): Boolean {
|
|
|
|
|
for (techniker in techniker) {
|
|
|
|
|
if (name == techniker.name) {
|
|
|
|
|
existing = true
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
MaterialDialog(this)
|
|
|
|
|
.show {
|
|
|
|
|
message(0, "Möchtest du dich als involvierter Techniker eintragen?")
|
|
|
|
|
positiveButton(0, "Ja") {
|
|
|
|
|
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(0, "Abbrechen")
|
|
|
|
|
negativeButton(text = "Abbrechen")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
databaseReference.child("techniker").push().setValue(name)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MaterialDialog(this)
|
|
|
|
|
.show {
|
|
|
|
|
message(0, "Du bist bereits eingetragen.")
|
|
|
|
|
positiveButton(0, "Ok")
|
|
|
|
|
message(text = "Du bist bereits eingetragen.")
|
|
|
|
|
positiveButton(text = "Ok")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emptyCheck()
|
|
|
|
|
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) {
|
|
|
|
|
@ -225,7 +307,7 @@ class ProjectActivity : AppCompatActivity() {
|
|
|
|
|
FirebaseDatabase.getInstance().reference.addChildEventListener(listener)
|
|
|
|
|
|
|
|
|
|
MaterialDialog(this)
|
|
|
|
|
.title(0, "Bitte gebe den Admin-Key ein")
|
|
|
|
|
.title(text = "Bitte gebe den Admin-Key ein")
|
|
|
|
|
.show {
|
|
|
|
|
input(hint = "Admin-Key", waitForPositiveButton = false) { _, inputKey ->
|
|
|
|
|
positiveButton {
|
|
|
|
|
@ -239,7 +321,7 @@ class ProjectActivity : AppCompatActivity() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
positiveButton(0, "Speichern")
|
|
|
|
|
positiveButton(text = "Speichern")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
sharedPref.edit().putBoolean("admin", false).apply()
|
|
|
|
|
|