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.
173 lines
6.5 KiB
Kotlin
173 lines
6.5 KiB
Kotlin
package com.cyb3rko.techniklogger
|
|
|
|
import android.os.Bundle
|
|
import android.view.Menu
|
|
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.cyb3rko.techniklogger.recycler.ProjectTechnikerViewHolder
|
|
import com.cyb3rko.techniklogger.recycler.ProjectTechnikerViewState
|
|
import com.google.firebase.database.*
|
|
|
|
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
|
|
|
|
class ProjectActivity : AppCompatActivity() {
|
|
|
|
private var childKey = ""
|
|
private val techniker: MutableList<ProjectTechnikerViewState.ProjectTechniker> = mutableListOf()
|
|
private lateinit var databaseReference: DatabaseReference
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
childKey = intent.extras?.getString("childKey").toString()
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_project)
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
|
databaseReference = FirebaseDatabase.getInstance().getReference(childKey)
|
|
|
|
val adapter = adapterOf<ProjectTechnikerViewState> {
|
|
register(
|
|
layoutResource = R.layout.item_recycler_techniker,
|
|
viewHolder = ::ProjectTechnikerViewHolder,
|
|
onBindBindViewHolder = { vh, _, text ->
|
|
vh.textView.text = text.name
|
|
vh.itemView.setOnClickListener {
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
val listener = object: ChildEventListener {
|
|
override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
|
|
techniker.add(techniker.size, ProjectTechnikerViewState.ProjectTechniker(snapshot.value.toString()))
|
|
techniker.sortBy { it.name }
|
|
recycler_view.scheduleLayoutAnimation()
|
|
adapter.notifyDataSetChanged()
|
|
showDivider()
|
|
}
|
|
|
|
override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) {
|
|
}
|
|
|
|
override fun onChildRemoved(snapshot: DataSnapshot) {
|
|
techniker.remove(ProjectTechnikerViewState.ProjectTechniker(snapshot.value.toString()))
|
|
recycler_view.scheduleLayoutAnimation()
|
|
adapter.notifyDataSetChanged()
|
|
emptyCheck()
|
|
}
|
|
|
|
override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {
|
|
}
|
|
|
|
override fun onCancelled(error: DatabaseError) {
|
|
Toast.makeText(applicationContext, "Abruf fehlgeschlagen", Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
|
|
adapter.submitList(techniker as List<ProjectTechnikerViewState>?)
|
|
recycler_view.layoutManager = LinearLayoutManager(applicationContext)
|
|
recycler_view.adapter = adapter
|
|
|
|
databaseReference.child("techniker").addChildEventListener(listener)
|
|
|
|
setValueEventListener("name", title_view)
|
|
setValueEventListener("location", location_view)
|
|
setValueEventListener("date", date_view)
|
|
|
|
add_button.setOnClickListener {
|
|
MaterialDialog(this)
|
|
.show {
|
|
message(0, "Möchtest du dich als involvierter Techniker eintragen?")
|
|
positiveButton(0, "Ja") {
|
|
val name = getSharedPreferences("Safe", 0).getString("name", "invalid")
|
|
databaseReference.child("techniker").push().setValue(name)
|
|
}
|
|
negativeButton(0, "Abbrechen")
|
|
}
|
|
}
|
|
|
|
emptyCheck()
|
|
}
|
|
|
|
private fun setValueEventListener(childName: String, textView: TextView) {
|
|
val valueEventListener = object: ValueEventListener {
|
|
override fun onDataChange(snapshot: DataSnapshot) {
|
|
textView.text = snapshot.value.toString()
|
|
}
|
|
|
|
override fun onCancelled(error: DatabaseError) {
|
|
}
|
|
}
|
|
databaseReference.child(childName).addListenerForSingleValueEvent(valueEventListener)
|
|
}
|
|
|
|
private fun showDivider() {
|
|
divider.visibility = View.VISIBLE
|
|
}
|
|
|
|
private fun emptyCheck() {
|
|
if (techniker.isEmpty()) {
|
|
divider.visibility = View.GONE
|
|
}
|
|
}
|
|
|
|
private fun showNameDialog() {
|
|
val currentName = getSharedPreferences("Safe", 0).getString("name", "")
|
|
|
|
MaterialDialog(this)
|
|
.cancelable(false)
|
|
.title(0, "Bitte gebe deinen Namen ein")
|
|
.show {
|
|
input(hint = "Dein Name", prefill = currentName, waitForPositiveButton = false) { dialog, inputName ->
|
|
try {
|
|
if (!inputName[0].isUpperCase()) {
|
|
dialog.getInputField().error = "Der Anfangsbuchstabe sollte groß sein"
|
|
}
|
|
} catch (ignored: IndexOutOfBoundsException) {
|
|
}
|
|
|
|
positiveButton {
|
|
getSharedPreferences("Safe", 0).edit().putString("name", inputName.toString()).apply()
|
|
}
|
|
}
|
|
positiveButton(0, "Speichern")
|
|
}
|
|
}
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
menuInflater.inflate(R.menu.menu_main, menu)
|
|
return true
|
|
}
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
// Handle action bar item clicks here. The action bar will
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
when (item.itemId) {
|
|
android.R.id.home -> finish()
|
|
R.id.action_rename -> showNameDialog()
|
|
// R.id.action_icons -> setContentView(AboutIcons(applicationContext, R.drawable::class.java).setTitle("Benutzte Icons").get())
|
|
// R.id.action_libraries -> LibsBuilder().start(this)
|
|
// R.id.action_privacy_policy ->
|
|
// R.id.action_terms_of_use ->
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
}
|
|
}
|