From 9557a0aa92cf2dcd59e5934b5c5cee43d870d553 Mon Sep 17 00:00:00 2001 From: David Gassner Date: Mon, 20 Jul 2020 10:29:12 -0700 Subject: [PATCH] 02_05e --- .../com/example/plainolnotes4/MainFragment.kt | 6 +++++ .../example/plainolnotes4/MainViewModel.kt | 10 ++++++- .../plainolnotes4/data/SampleDataProvider.kt | 27 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/example/plainolnotes4/data/SampleDataProvider.kt diff --git a/app/src/main/java/com/example/plainolnotes4/MainFragment.kt b/app/src/main/java/com/example/plainolnotes4/MainFragment.kt index 831a54b..5ae07f4 100644 --- a/app/src/main/java/com/example/plainolnotes4/MainFragment.kt +++ b/app/src/main/java/com/example/plainolnotes4/MainFragment.kt @@ -1,10 +1,12 @@ package com.example.plainolnotes4 import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager @@ -30,6 +32,10 @@ class MainFragment : Fragment() { addItemDecoration(divider) } + viewModel.notesList.observe(viewLifecycleOwner, Observer { + Log.i("noteLogging", it.toString()) + }) + return binding.root } diff --git a/app/src/main/java/com/example/plainolnotes4/MainViewModel.kt b/app/src/main/java/com/example/plainolnotes4/MainViewModel.kt index 6152c05..109f58a 100644 --- a/app/src/main/java/com/example/plainolnotes4/MainViewModel.kt +++ b/app/src/main/java/com/example/plainolnotes4/MainViewModel.kt @@ -1,7 +1,15 @@ package com.example.plainolnotes4 +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import com.example.plainolnotes4.data.NoteEntity +import com.example.plainolnotes4.data.SampleDataProvider class MainViewModel : ViewModel() { - // TODO: Implement the ViewModel + + val notesList = MutableLiveData>() + + init { + notesList.value = SampleDataProvider.getNotes() + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/plainolnotes4/data/SampleDataProvider.kt b/app/src/main/java/com/example/plainolnotes4/data/SampleDataProvider.kt new file mode 100644 index 0000000..e6f208a --- /dev/null +++ b/app/src/main/java/com/example/plainolnotes4/data/SampleDataProvider.kt @@ -0,0 +1,27 @@ +package com.example.plainolnotes4.data + +import java.util.* + +class SampleDataProvider { + + companion object { + private val sampleText1 = "A simple note" + private val sampleText2 = "A note a\nline feed" + private val sampleText3 = """ + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque finibus, magna eget ullamcorper eleifend, neque justo cursus nibh, sit amet fermentum nisi dui sed justo. Nulla ac nisl ut nulla euismod mollis. Aenean ullamcorper eu odio a rutrum. Mauris eu augue tortor. Pellentesque erat justo, gravida sed maximus eu, faucibus at justo. Aliquam ut nulla consectetur odio vestibulum vulputate vel id est. Aliquam erat volutpat. + + Fusce maximus sagittis dolor in tempor. Duis vehicula congue lectus eu lobortis. Integer placerat fermentum sapien, vel feugiat sapien pellentesque non. Integer nec nibh sit amet ex lacinia pretium sed et eros. Nam id consequat erat, eleifend mollis quam. In dictum lobortis quam vel tincidunt. Vestibulum non lobortis neque. Phasellus pharetra malesuada mauris eget blandit. Sed ornare nisl id nisl tristique placerat. + """.trimIndent() + + private fun getDate(diff: Long): Date { + return Date(Date().time + diff) + } + + fun getNotes() = arrayListOf( + NoteEntity(1, getDate(0), sampleText1), + NoteEntity(2, getDate(1), sampleText2), + NoteEntity(3, getDate(2), sampleText3) + ) + + } +} \ No newline at end of file