diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt index 8e54dee..c9950f5 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt @@ -1,39 +1,21 @@ package com.wbrawner.simplemarkdown.view.activity import android.os.Bundle -import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.navigation.findNavController import com.wbrawner.simplemarkdown.R -import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel -import kotlinx.coroutines.* -import kotlin.coroutines.CoroutineContext -class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback, CoroutineScope { - - override val coroutineContext: CoroutineContext = Dispatchers.Main - private val viewModel: MarkdownViewModel by viewModels() +class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - intent?.data?.let { - launch { - viewModel.load(this@MainActivity, it) - intent?.data = null - } - } } override fun onBackPressed() { - findNavController(R.id.content).navigateUp() - } - - override fun onDestroy() { - super.onDestroy() - coroutineContext[Job]?.let { - cancel() + if (!findNavController(R.id.content).navigateUp()) { + super.onBackPressed() } } } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt index 46958fa..b0ce8f3 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt @@ -16,7 +16,7 @@ import android.widget.EditText import android.widget.TextView import androidx.core.widget.NestedScrollView import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.R @@ -32,7 +32,7 @@ import kotlin.math.abs class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { private var markdownEditor: EditText? = 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 private var readabilityWatcher: TextWatcher? = null diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt index 7e02ea0..3de6168 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt @@ -2,6 +2,7 @@ package com.wbrawner.simplemarkdown.view.fragment import android.Manifest import android.app.Activity +import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.res.Configuration @@ -17,7 +18,7 @@ import androidx.appcompat.app.AlertDialog import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.navigation.fragment.findNavController import androidx.navigation.ui.AppBarConfiguration @@ -36,9 +37,20 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba private var shouldAutoSave = true override val coroutineContext: CoroutineContext = Dispatchers.Main - private val viewModel: MarkdownViewModel by activityViewModels() + private val viewModel: MarkdownViewModel by viewModels() 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?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt index 8931524..f2fa3f6 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt @@ -10,7 +10,7 @@ import android.view.ViewGroup import android.webkit.WebView import androidx.appcompat.app.AppCompatDelegate import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.BuildConfig @@ -22,7 +22,7 @@ import kotlin.coroutines.CoroutineContext class PreviewFragment : Fragment(), CoroutineScope { 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 style: String = ""