-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-RecyclerView as well its own adapter AddNoteFragment: -logic for save a note NoteDetail: -logic for display a given note index
- Loading branch information
Showing
12 changed files
with
240 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
Notes/app/src/main/java/com/training/notes/ui/detail/NoteDetailViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
Notes/app/src/main/java/com/training/notes/ui/list/NotesFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
9 changes: 6 additions & 3 deletions
9
Notes/app/src/main/java/com/training/notes/ui/list/NotesViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.