Skip to content

Commit

Permalink
(#2) Fix observer lifecycle bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
myung jun Hyun committed Nov 27, 2019
1 parent 3a30b8d commit 968fabb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AttendeesFragment(attendanceList: List<NoticeAttendance>) : Fragment() {
}

private fun setupEventObserver() {
viewModel.closeEvent.observe(this, EventObserver {
viewModel.closeEvent.observe(viewLifecycleOwner, EventObserver {
if (it) {
activity?.finish()
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/mashup/app/notices/NoticesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mashup.app.notices

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -56,7 +57,8 @@ class NoticesFragment : Fragment() {
}

private fun setupSnackbar() {
view?.setupSnackbar(this, viewModel.snackbarText, Snackbar.LENGTH_SHORT)
Log.d("lolos", "setup")
view?.setupSnackbar(viewLifecycleOwner, viewModel.snackbarText, Snackbar.LENGTH_SHORT)
}

private fun setupEventObserver() {
Expand Down
13 changes: 5 additions & 8 deletions app/src/main/java/com/mashup/util/ViewExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ fun View.setupSnackbar(
snackbarEvent: LiveData<Event<Any>>,
timeLength: Int
) {

snackbarEvent.observe(lifecycleOwner, Observer { event ->
event.getContentIfNotHandled()?.let {
when (it) {
is Int -> showSnackbar(context.getString(it), timeLength)
is String -> showSnackbar(it, timeLength)
else -> throw TypeCastException()
}
snackbarEvent.observe(lifecycleOwner, EventObserver {
when (it) {
is Int -> showSnackbar(context.getString(it), timeLength)
is String -> showSnackbar(it, timeLength)
else -> throw TypeCastException()
}
})
}

0 comments on commit 968fabb

Please sign in to comment.