Fix losing data on screen rotation and weird behavior with loading files

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-08-22 22:16:37 -07:00
parent 894fea4193
commit 56f2efbb18
4 changed files with 21 additions and 27 deletions

View file

@ -1,39 +1,21 @@
package com.wbrawner.simplemarkdown.view.activity package com.wbrawner.simplemarkdown.view.activity
import android.os.Bundle import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.navigation.findNavController import androidx.navigation.findNavController
import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.R
import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback, CoroutineScope { class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {
override val coroutineContext: CoroutineContext = Dispatchers.Main
private val viewModel: MarkdownViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
intent?.data?.let {
launch {
viewModel.load(this@MainActivity, it)
intent?.data = null
}
}
} }
override fun onBackPressed() { override fun onBackPressed() {
findNavController(R.id.content).navigateUp() if (!findNavController(R.id.content).navigateUp()) {
} super.onBackPressed()
override fun onDestroy() {
super.onDestroy()
coroutineContext[Job]?.let {
cancel()
} }
} }
} }

View file

@ -16,7 +16,7 @@ import android.widget.EditText
import android.widget.TextView import android.widget.TextView
import androidx.core.widget.NestedScrollView import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.R
@ -32,7 +32,7 @@ import kotlin.math.abs
class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { class EditFragment : Fragment(), ViewPagerPage, CoroutineScope {
private var markdownEditor: EditText? = null private var markdownEditor: EditText? = null
private var markdownEditorScroller: NestedScrollView? = null private var markdownEditorScroller: NestedScrollView? = null
private val viewModel: MarkdownViewModel by activityViewModels() private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() })
override val coroutineContext: CoroutineContext = Dispatchers.Main override val coroutineContext: CoroutineContext = Dispatchers.Main
private var readabilityWatcher: TextWatcher? = null private var readabilityWatcher: TextWatcher? = null

View file

@ -2,6 +2,7 @@ package com.wbrawner.simplemarkdown.view.fragment
import android.Manifest import android.Manifest
import android.app.Activity import android.app.Activity
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
@ -17,7 +18,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
@ -36,9 +37,20 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
private var shouldAutoSave = true private var shouldAutoSave = true
override val coroutineContext: CoroutineContext = Dispatchers.Main override val coroutineContext: CoroutineContext = Dispatchers.Main
private val viewModel: MarkdownViewModel by activityViewModels() private val viewModel: MarkdownViewModel by viewModels()
private var appBarConfiguration: AppBarConfiguration? = null private var appBarConfiguration: AppBarConfiguration? = null
override fun onAttach(context: Context) {
super.onAttach(context)
if (context !is Activity) return
context.intent?.data?.let {
launch {
viewModel.load(context, it)
context.intent?.data = null
}
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setHasOptionsMenu(true) setHasOptionsMenu(true)

View file

@ -10,7 +10,7 @@ import android.view.ViewGroup
import android.webkit.WebView import android.webkit.WebView
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.wbrawner.simplemarkdown.BuildConfig import com.wbrawner.simplemarkdown.BuildConfig
@ -22,7 +22,7 @@ import kotlin.coroutines.CoroutineContext
class PreviewFragment : Fragment(), CoroutineScope { class PreviewFragment : Fragment(), CoroutineScope {
override val coroutineContext: CoroutineContext = Dispatchers.Main override val coroutineContext: CoroutineContext = Dispatchers.Main
private val viewModel: MarkdownViewModel by activityViewModels() private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() })
private var markdownPreview: WebView? = null private var markdownPreview: WebView? = null
private var style: String = "" private var style: String = ""