Skip to content

Commit

Permalink
Bugfix: fix bug coming with lateinit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindjet committed Jan 3, 2018
1 parent 3858dc6 commit 6268ab1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ package io.github.mindjet.livemvvm.viewmodel

import android.databinding.ViewDataBinding
import android.support.annotation.LayoutRes
import android.util.Log

/**
* Created by Mindjet on 2017/10/16.
*/
abstract class BaseItemViewModel<B : ViewDataBinding> : BaseViewModel<B>() {

private val TAG = "BaseItemViewModel"

@LayoutRes
abstract fun needLayoutId(): Int

fun onAttachedToAdapter(binding: B) {
if (this.binding == null) {
try {
val dump = this.binding
} catch (e: UninitializedPropertyAccessException) {
Log.i(TAG, "This is the first time ${this} attached to the adapter")
this.binding = binding
this.context = binding.root.context
onAttachedTheFirstTime(binding)
onAttached(binding)
} else {
} finally {
onAttached(binding)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ abstract class BaseViewModel<B : ViewDataBinding> : ViewModel() {

protected lateinit var binding: B
protected lateinit var context: Context
protected lateinit var activity: BaseActivity<B>
protected lateinit var activity:BaseActivity<B>

fun onAttachedWithActivity(binding: B, baseActivity: BaseActivity<B>) {
this.binding = binding
this.context = binding.root.context
this.activity = baseActivity
this.binding.setVariable(BR.data, this)
this.binding?.setVariable(BR.data, this)
onAttachedTheFirstTime(binding)
onAttached(binding)
}
Expand Down

0 comments on commit 6268ab1

Please sign in to comment.