diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index b468083..664e4d2 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -1,8 +1,10 @@ package it.danieleverducci.lunatracker import android.os.Bundle +import android.os.Handler import android.view.View import android.widget.NumberPicker +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager @@ -11,6 +13,7 @@ import it.danieleverducci.lunatracker.adapters.LunaEventRecyclerAdapter import it.danieleverducci.lunatracker.entities.Logbook import it.danieleverducci.lunatracker.entities.LunaEvent import it.danieleverducci.lunatracker.entities.LunaEventType +import kotlinx.coroutines.Runnable class MainActivity : AppCompatActivity() { companion object { @@ -19,6 +22,7 @@ class MainActivity : AppCompatActivity() { lateinit var logbook: Logbook lateinit var adapter: LunaEventRecyclerAdapter + lateinit var recyclerView: RecyclerView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -30,7 +34,7 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) // Show logbook - val recyclerView = findViewById(R.id.list_events) + recyclerView = findViewById(R.id.list_events) recyclerView.setLayoutManager(LinearLayoutManager(this)) adapter = LunaEventRecyclerAdapter(this) adapter.items.addAll(logbook.logs) @@ -65,6 +69,13 @@ class MainActivity : AppCompatActivity() { ) } } + override fun onStart() { + super.onStart() + + // Update list dates + adapter.notifyDataSetChanged() + } + fun askBabyBottleContent() { // Show number picker dialog val d = AlertDialog.Builder(this) @@ -88,8 +99,12 @@ class MainActivity : AppCompatActivity() { fun logEvent(event: LunaEvent) { adapter.items.add(0, event) adapter.notifyItemInserted(0) + recyclerView.smoothScrollToPosition(0) + logbook.logs.add(0, event) logbook.save(this) + + Toast.makeText(this, R.string.toast_event_added, Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt index 92a529a..8680f46 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/adapters/LunaEventRecyclerAdapter.kt @@ -58,7 +58,12 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter 0) item.quantity.toString() else "" - holder.time.text = DateUtils.getRelativeTimeSpanString(item.time * 1000) + // holder.time.text = DateUtils.getRelativeTimeSpanString(item.time * 1000) + holder.time.text = DateUtils.getRelativeTimeSpanString( + item.time * 1000, + System.currentTimeMillis(), + DateUtils.MINUTE_IN_MILLIS, + DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH or DateUtils.FORMAT_SHOW_TIME) } override fun getItemCount(): Int { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e99c4f5..da02c6e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,4 +23,6 @@ Cambio (con pipì) + Evento aggiunto! + \ No newline at end of file