(Hopefully) fix crash due to accessing viewModel in fragment not attached to activity

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-06-11 21:00:05 -07:00
parent 02363d9940
commit a88eeb79e4

View file

@ -1,5 +1,6 @@
package com.wbrawner.simplemarkdown.view.fragment package com.wbrawner.simplemarkdown.view.fragment
import android.content.Context
import android.content.res.Configuration.UI_MODE_NIGHT_MASK import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.content.res.Configuration.UI_MODE_NIGHT_YES import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.os.Bundle import android.os.Bundle
@ -57,13 +58,17 @@ class PreviewFragment : Fragment(), CoroutineScope {
} }
} }
style = String.format(FORMAT_CSS, css ?: "") style = String.format(FORMAT_CSS, css ?: "")
updateWebContent(viewModel.markdownUpdates.value ?: "")
viewModel.markdownUpdates.observe(viewLifecycleOwner, Observer {
updateWebContent(it)
})
} }
} }
override fun onAttach(context: Context) {
super.onAttach(context)
updateWebContent(viewModel.markdownUpdates.value ?: "")
viewModel.markdownUpdates.observe(viewLifecycleOwner, Observer {
updateWebContent(it)
})
}
private fun updateWebContent(markdown: String) { private fun updateWebContent(markdown: String) {
markdownPreview?.post { markdownPreview?.post {
launch { launch {