Skip to content

Commit

Permalink
NotesFragment:
Browse files Browse the repository at this point in the history
-RecyclerView as well its own adapter

AddNoteFragment:
-logic for save a note

NoteDetail:
-logic for display a given note index
  • Loading branch information
jclemus91 committed Jul 14, 2019
1 parent 83359d1 commit 4d172da
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 78 deletions.
1 change: 0 additions & 1 deletion Notes/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.training.notes.R
import kotlinx.android.synthetic.main.add_note_fragment.*
import org.koin.android.viewmodel.ext.android.viewModel

class AddNoteFragment : Fragment() {

companion object {
fun newInstance() = AddNoteFragment()
}

private val viewModel: AddNoteViewModel by viewModel()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.add_note_fragment, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.addNote("title", "message")
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

buttonSave.setOnClickListener {
viewModel.addNote(
etTitle.text.toString(),
etDescription.text.toString()
)
findNavController().popBackStack()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ class AddNoteViewModel(
fun addNote(title: String, message: String) {
notesRepository.addNote(Note(title, message))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.training.notes.R
import kotlinx.android.synthetic.main.note_detail_fragment.*
import org.koin.android.viewmodel.ext.android.viewModel

class NoteDetailFragment : Fragment() {

companion object {
fun newInstance() = NoteDetailFragment()
}

private val viewModel: NoteDetailViewModel by viewModel()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.note_detail_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.setUp(arguments?.getInt("note_index") ?: -1)
viewModel.noteLive.observe(this, Observer {
tvTitle.text = it.title
tvDescription.text = it.message
})
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.training.notes.ui.detail

import androidx.lifecycle.ViewModel;
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.training.notes.model.Note
import com.training.notes.repository.NotesRepository

class NoteDetailViewModel(
private val notesRepository: NotesRepository
) : ViewModel() {

fun getNote(position: Int): Note {
return notesRepository.getNote(position)
}
val noteLive = MutableLiveData<Note>()

fun setUp(position: Int) {
noteLive.value = notesRepository.getNote(position)
}
}
107 changes: 93 additions & 14 deletions Notes/app/src/main/java/com/training/notes/ui/list/NotesFragment.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,111 @@
package com.training.notes.ui.list

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.*
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.training.notes.R
import com.training.notes.model.Note
import kotlinx.android.synthetic.main.notes_fragment.*
import org.koin.android.viewmodel.ext.android.viewModel

class NotesFragment : Fragment() {

companion object {
fun newInstance() = NotesFragment()
private val viewModel: NotesViewModel by viewModel()
private lateinit var notesAdapter: NotesAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.notes_fragment, container, false)
}

private val viewModel: NotesViewModel by viewModel()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Adapter
notesAdapter = NotesAdapter()
rvNotes.adapter = notesAdapter
notesAdapter.setOnNoteClick {
val args = Bundle()
args.putInt("note_index", it)
findNavController().navigate(R.id.action_notesFragment_to_noteDetailFragment, args)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.notes_fragment, container, false)
// View Model
viewModel.setUp()
viewModel.notesLive.observe(this, Observer {
if (it.isNotEmpty()) {
notesAdapter.addNotes(it)
if (rvNotes.visibility != View.VISIBLE) {
rvNotes.visibility = View.VISIBLE
tvEmpty.visibility = View.GONE
}
} else if (tvEmpty.visibility != View.VISIBLE) {
tvEmpty.visibility = View.VISIBLE
rvNotes.visibility = View.GONE
}
})
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
super.onCreateOptionsMenu(menu, inflater)
inflater?.inflate(R.menu.menu_main, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_add -> {
findNavController().navigate(R.id.action_notesFragment_to_addNoteFragment)
true
}
else -> super.onOptionsItemSelected(item)
}
}

}
private class NotesAdapter : RecyclerView.Adapter<NotesAdapter.NoteVH>() {

private val notes = arrayListOf<Note>()
private var onNoteClick: ((noteIndex: Int) -> Unit)? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteVH {
val rowView = LayoutInflater.from(parent.context)
.inflate(R.layout.note_row, parent, false)
return NoteVH(rowView)
}

override fun getItemCount(): Int = notes.size

override fun onBindViewHolder(holder: NoteVH, position: Int) {
holder.bind(notes[position])
}

fun addNotes(notes: List<Note>) {
this.notes.clear()
this.notes.addAll(notes)
notifyDataSetChanged()
}

fun setOnNoteClick(onNoteClick: ((noteIndex: Int) -> Unit)?) {
this.onNoteClick = onNoteClick
}

private inner class NoteVH(itemView: View) : RecyclerView.ViewHolder(itemView) {

private val tvTitle: TextView = itemView.findViewById(R.id.tvTitle)

init {
itemView.setOnClickListener { onNoteClick?.invoke(adapterPosition) }
}

fun bind(note: Note) {
tvTitle.text = note.title
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.training.notes.ui.list

import androidx.lifecycle.ViewModel;
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.training.notes.model.Note
import com.training.notes.repository.NotesRepository

class NotesViewModel(
private val notesRepository: NotesRepository
) : ViewModel() {

fun getNotes() : List<Note> {
return notesRepository.getNotes()
val notesLive = MutableLiveData<List<Note>>()

fun setUp() {
notesLive.value = notesRepository.getNotes()
}
}
45 changes: 37 additions & 8 deletions Notes/app/src/main/res/layout/add_note_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.add.AddNoteFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Add Note"/>

</FrameLayout>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etTitle"
android:layout_marginTop="32dp"
android:hint="@string/add_note_title_hint"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="16dp"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="@string/add_note_description_hint"
android:id="@+id/etDescription"
app:layout_constraintEnd_toEndOf="@+id/etTitle"
app:layout_constraintStart_toStartOf="@+id/etTitle"
app:layout_constraintTop_toBottomOf="@+id/etTitle"
android:layout_marginTop="24dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSave"
android:text="@string/add_note_save"
app:layout_constraintEnd_toEndOf="@+id/etDescription"
app:layout_constraintStart_toStartOf="@+id/etDescription"
app:layout_constraintTop_toBottomOf="@+id/etDescription"
android:layout_marginTop="24dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
33 changes: 25 additions & 8 deletions Notes/app/src/main/res/layout/note_detail_fragment.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:tools="http:https://schemas.android.com/tools"
xmlns:app="http:https://schemas.android.com/apk/res-auto" xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.detail.NoteDetailFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Note Detail"/>

</FrameLayout>
android:text="@string/note_detail_title_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTitle"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintStart_toStartOf="@+id/textView"
android:layout_marginTop="8dp"/>
<TextView
android:text="@string/note_detail_description_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3" app:layout_constraintStart_toStartOf="@+id/tvTitle"
app:layout_constraintTop_toBottomOf="@+id/tvTitle" android:layout_marginTop="32dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvDescription" app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" android:layout_marginTop="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
22 changes: 22 additions & 0 deletions Notes/app/src/main/res/layout/note_row.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTitle" android:layout_margin="16dp"/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 4d172da

Please sign in to comment.