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 ad7b765
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 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
6 changes: 1 addition & 5 deletions app/src/main/java/com/mashup/app/notices/NoticesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import org.koin.androidx.viewmodel.ext.android.viewModel

class NoticesFragment : Fragment() {

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

private val viewModel: NoticesViewModel by viewModel()
private lateinit var viewDataBinding: NoticesFragmentBinding
private lateinit var listAdapter: NoticeAdapter
Expand Down Expand Up @@ -56,7 +52,7 @@ class NoticesFragment : Fragment() {
}

private fun setupSnackbar() {
view?.setupSnackbar(this, viewModel.snackbarText, Snackbar.LENGTH_SHORT)
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 ad7b765

Please sign in to comment.