package com.cyb3rko.techniklogger import android.content.SharedPreferences import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View 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.ProjectEntryViewHolder import com.cyb3rko.techniklogger.recycler.ProjectViewState import com.google.firebase.database.* import java.lang.IndexOutOfBoundsException import kotlinx.android.synthetic.main.activity_main.* import me.ibrahimyilmaz.kiel.adapter.RecyclerViewAdapter.Companion.adapterOf class MainActivity : AppCompatActivity() { private val data: MutableList = mutableListOf() private lateinit var databaseReference: DatabaseReference private lateinit var sharedPref: SharedPreferences override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) sharedPref = getSharedPreferences("Safe", 0) databaseReference = FirebaseDatabase.getInstance().reference if (sharedPref.getString("name", "") == "") { showNameDialog() } val adapter = adapterOf { register( layoutResource = R.layout.item_recycler_projects, viewHolder = ::ProjectEntryViewHolder, onBindBindViewHolder = { vh, _, text -> vh.textView.text = text.text vh.locationView.text = text.location vh.dateView.text = text.date vh.itemView.setOnClickListener { ProjectActivityBuilder(applicationContext) .setKey(text.childKey) .start() } } ) } val listener = object: ChildEventListener { override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) { data.add(data.size, ProjectViewState.ProjectEntry( snapshot.key!!, snapshot.child("name").value.toString(), snapshot.child("location").value.toString(), snapshot.child("date").value.toString()) ) hideProgess() recycler_view.scheduleLayoutAnimation() adapter.notifyDataSetChanged() } override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) { } override fun onChildRemoved(snapshot: DataSnapshot) { data.remove(ProjectViewState.ProjectEntry( snapshot.key!!, snapshot.child("name").value.toString(), snapshot.child("location").value.toString(), snapshot.child("date").value.toString()) ) recycler_view.scheduleLayoutAnimation() adapter.notifyDataSetChanged() } override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) { } override fun onCancelled(error: DatabaseError) { Toast.makeText(applicationContext, "Abruf fehlgeschlagen", Toast.LENGTH_SHORT).show() } } adapter.submitList(data as List?) val linearLayoutManager = LinearLayoutManager(applicationContext) linearLayoutManager.reverseLayout = true linearLayoutManager.stackFromEnd = true recycler_view.layoutManager = linearLayoutManager recycler_view.adapter = adapter databaseReference.addChildEventListener(listener) } 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 { sharedPref.edit().putString("name", inputName.toString()).apply() } } positiveButton(0, "Speichern") } } private fun hideProgess() { progress_bar.visibility = View.GONE } 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) { 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) } }