Skip to content

Commit

Permalink
Refactor to viewbinding
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcarrano committed Sep 2, 2021
1 parent 9fd9729 commit 3e17d36
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
4 changes: 3 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.jlleitschuh.gradle.ktlint"

android {
Expand All @@ -15,6 +14,9 @@ android {
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures{
viewBinding = true
}
lintOptions {
disable 'GoogleAppIndexingWarning'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.ww.roxiesample.R
import com.ww.roxiesample.databinding.NoteDetailBinding
import com.ww.roxiesample.domain.DeleteNoteUseCase
import com.ww.roxiesample.domain.GetNoteDetailUseCase
import com.ww.roxiesample.domain.Note
import kotlinx.android.synthetic.main.note_detail.*

private const val NOTE_ID = "noteId"

Expand All @@ -51,12 +51,16 @@ class NoteDetailFragment : Fragment() {

private lateinit var viewModel: NoteDetailViewModel

private var _binding: NoteDetailBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.note_detail, container, false)
_binding = NoteDetailBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -76,11 +80,16 @@ class NoteDetailFragment : Fragment() {
viewModel.dispatch(Action.LoadNoteDetail(noteId))
}

deleteNoteButton.setOnClickListener {
binding.deleteNoteButton.setOnClickListener {
viewModel.dispatch(Action.DeleteNote(noteId))
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun renderState(state: State) {
with(state) {
when {
Expand All @@ -93,16 +102,16 @@ class NoteDetailFragment : Fragment() {
}

private fun renderNoteDetailState(note: Note) {
noteIdView.visibility = View.VISIBLE
noteTextView.visibility = View.VISIBLE
noteIdView.text = String.format(getString(R.string.note_detail_id), note.id)
noteTextView.text = String.format(getString(R.string.note_detail_text), note.text)
binding.noteIdView.visibility = View.VISIBLE
binding.noteTextView.visibility = View.VISIBLE
binding.noteIdView.text = String.format(getString(R.string.note_detail_id), note.id)
binding.noteTextView.text = String.format(getString(R.string.note_detail_text), note.text)
}

private fun renderLoadNoteDetailError() {
Toast.makeText(requireContext(), R.string.error_loading_note, Toast.LENGTH_LONG).show()
noteIdView.visibility = View.GONE
noteTextView.visibility = View.GONE
binding.noteIdView.visibility = View.GONE
binding.noteTextView.visibility = View.GONE
}

private fun renderNoteDeleteError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.ww.roxiesample.R
import com.ww.roxiesample.databinding.NoteListBinding
import com.ww.roxiesample.domain.GetNoteListUseCase
import com.ww.roxiesample.domain.Note
import com.ww.roxiesample.presentation.notedetail.NoteDetailFragment
import kotlinx.android.synthetic.main.note_list.*

class NoteListFragment : Fragment() {

Expand All @@ -42,12 +42,16 @@ class NoteListFragment : Fragment() {

private lateinit var viewModel: NoteListViewModel

private var _binding: NoteListBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.note_list, container, false)
_binding = NoteListBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -65,6 +69,11 @@ class NoteListFragment : Fragment() {
viewModel.dispatch(Action.LoadNotes)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun renderState(state: State) {
with(state) {
when {
Expand All @@ -76,24 +85,24 @@ class NoteListFragment : Fragment() {
}

private fun renderLoadingState() {
loadingIndicator.visibility = View.VISIBLE
binding.loadingIndicator.visibility = View.VISIBLE
}

private fun renderErrorState() {
loadingIndicator.visibility = View.GONE
binding.loadingIndicator.visibility = View.GONE
Toast.makeText(requireContext(), R.string.error_loading_notes, Toast.LENGTH_LONG).show()
}

private fun renderNotesState(notes: List<Note>) {
loadingIndicator.visibility = View.GONE
binding.loadingIndicator.visibility = View.GONE
recyclerViewAdapter.updateNotes(notes)
notesRecyclerView.visibility = View.VISIBLE
binding.notesRecyclerView.visibility = View.VISIBLE
}

private fun setupRecyclerView() {
notesRecyclerView.layoutManager = LinearLayoutManager(requireContext())
notesRecyclerView.adapter = recyclerViewAdapter
notesRecyclerView.setHasFixedSize(true)
binding.notesRecyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.notesRecyclerView.adapter = recyclerViewAdapter
binding.notesRecyclerView.setHasFixedSize(true)
}

private fun onNoteClicked(note: Note) {
Expand Down

0 comments on commit 3e17d36

Please sign in to comment.