(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:
parent
02363d9940
commit
a88eeb79e4
1 changed files with 9 additions and 4 deletions
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue