diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.kt index a23a46f..f49f697 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.kt @@ -6,19 +6,18 @@ import android.os.Build import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate +import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.viewmodel.PREF_KEY_AUTOSAVE_URI import kotlinx.coroutines.* -import kotlin.coroutines.CoroutineContext +import timber.log.Timber -class SplashActivity : AppCompatActivity(), CoroutineScope { - - override val coroutineContext: CoroutineContext = Dispatchers.Main +class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - launch { + lifecycleScope.launch { val darkMode = withContext(Dispatchers.IO) { val darkModeValue = PreferenceManager.getDefaultSharedPreferences(this@SplashActivity) .getString( @@ -58,11 +57,4 @@ class SplashActivity : AppCompatActivity(), CoroutineScope { finish() } } - - override fun onDestroy() { - super.onDestroy() - coroutineContext[Job]?.let { - cancel() - } - } } 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 c5ac0b4..81e7225 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 @@ -18,6 +18,7 @@ import androidx.core.widget.NestedScrollView import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.Observer +import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.model.Readability @@ -26,14 +27,12 @@ import com.wbrawner.simplemarkdown.utility.showKeyboard import com.wbrawner.simplemarkdown.view.ViewPagerPage import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel import kotlinx.coroutines.* -import kotlin.coroutines.CoroutineContext import kotlin.math.abs -class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { +class EditFragment : Fragment(), ViewPagerPage { private var markdownEditor: EditText? = null private var markdownEditorScroller: NestedScrollView? = null private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() }) - override val coroutineContext: CoroutineContext = Dispatchers.Main private var readabilityWatcher: TextWatcher? = null @SuppressLint("ClickableViewAccessibility") @@ -56,7 +55,7 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { searchFor = searchText - launch { + lifecycleScope.launch { delay(50) if (searchText != searchFor) return@launch @@ -101,7 +100,7 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { markdownEditor?.setText(it.markdown) } }) - launch { + lifecycleScope.launch { val enableReadability = withContext(Dispatchers.IO) { context?.let { PreferenceManager.getDefaultSharedPreferences(it) @@ -122,13 +121,6 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { } } - override fun onDestroyView() { - coroutineContext[Job]?.let { - cancel() - } - super.onDestroyView() - } - override fun onSelected() { markdownEditor?.showKeyboard() } @@ -148,7 +140,7 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { searchFor = searchText - launch { + lifecycleScope.launch { delay(250) if (searchText != searchFor) return@launch 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 53f1158..f325183 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 @@ -6,10 +6,8 @@ import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.content.res.Configuration -import android.net.Uri import android.os.Build import android.os.Bundle -import android.util.Log import android.view.* import android.webkit.MimeTypeMap import android.widget.Toast @@ -21,6 +19,7 @@ import androidx.core.content.edit import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.Observer +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupWithNavController @@ -33,13 +32,10 @@ import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel import com.wbrawner.simplemarkdown.viewmodel.PREF_KEY_AUTOSAVE_URI import kotlinx.android.synthetic.main.fragment_main.* import kotlinx.coroutines.* -import java.io.File -import kotlin.coroutines.CoroutineContext +import timber.log.Timber -class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallback, CoroutineScope { +class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallback { - private var shouldAutoSave = true - override val coroutineContext: CoroutineContext = Dispatchers.Main private val viewModel: MarkdownViewModel by viewModels() private var appBarConfiguration: AppBarConfiguration? = null private val errorHandler: ErrorHandler by errorHandlerImpl() @@ -47,11 +43,9 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba 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 - } + lifecycleScope.launch { + viewModel.load(context, context.intent?.data) + context.intent?.data = null } } @@ -99,8 +93,8 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba true } R.id.action_save -> { - launch { - Log.d("SimpleMarkdown", "Saving file from onOptionsItemSelected") + Timber.d("Save clicked") + lifecycleScope.launch { if (!viewModel.save(requireContext())) { requestFileOp(REQUEST_SAVE_FILE) } else { @@ -146,7 +140,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba override fun onStart() { super.onStart() - launch { + lifecycleScope.launch { withContext(Dispatchers.IO) { val enableErrorReports = PreferenceManager.getDefaultSharedPreferences(requireContext()) .getBoolean(getString(R.string.pref_key_error_reports_enabled), true) @@ -158,7 +152,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba override fun onPause() { super.onPause() val context = context?.applicationContext ?: return - launch { + lifecycleScope.launch { viewModel.autosave(context, PreferenceManager.getDefaultSharedPreferences(context)) } } @@ -200,7 +194,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba return } - launch { + lifecycleScope.launch { val fileLoaded = context?.let { viewModel.load(it, data.data) } @@ -221,7 +215,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba return } - launch { + lifecycleScope.launch { context?.let { Log.d("SimpleMarkdown", "Saving file from onActivityResult") viewModel.save(it, data.data) @@ -300,13 +294,6 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba shouldAutoSave = true } - override fun onDestroy() { - super.onDestroy() - coroutineContext[Job]?.let { - cancel() - } - } - companion object { // Request codes const val REQUEST_OPEN_FILE = 1 diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt index 96a205e..d3a439b 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt @@ -9,6 +9,7 @@ import android.view.ViewGroup import android.widget.Toast import androidx.appcompat.app.AppCompatDelegate import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.navigation.ui.setupWithNavController import com.wbrawner.simplemarkdown.R @@ -17,14 +18,9 @@ import com.wbrawner.simplemarkdown.utility.errorHandlerImpl import com.wbrawner.simplemarkdown.utility.readAssetToString import com.wbrawner.simplemarkdown.utility.toHtml import kotlinx.android.synthetic.main.fragment_markdown_info.* -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job import kotlinx.coroutines.launch -import kotlin.coroutines.CoroutineContext -class MarkdownInfoFragment : Fragment(), CoroutineScope { - override val coroutineContext: CoroutineContext = Dispatchers.Main +class MarkdownInfoFragment : Fragment() { private val errorHandler: ErrorHandler by errorHandlerImpl() override fun onCreate(savedInstanceState: Bundle?) { @@ -52,7 +48,7 @@ class MarkdownInfoFragment : Fragment(), CoroutineScope { R.string.pref_custom_css_default } val css: String? = getString(defaultCssId) - launch { + lifecycleScope.launch { try { val html = view.context.assets?.readAssetToString(fileName) ?.toHtml() @@ -70,12 +66,6 @@ class MarkdownInfoFragment : Fragment(), CoroutineScope { } } - override fun onDestroy() { - coroutineContext[Job]?.cancel() - super.onDestroy() - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { if (item.itemId == android.R.id.home) { findNavController().navigateUp() 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 f2fa3f6..a031deb 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 @@ -11,17 +11,15 @@ import android.webkit.WebView import androidx.appcompat.app.AppCompatDelegate import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels -import androidx.lifecycle.Observer +import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.BuildConfig import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.utility.toHtml import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel import kotlinx.coroutines.* -import kotlin.coroutines.CoroutineContext -class PreviewFragment : Fragment(), CoroutineScope { - override val coroutineContext: CoroutineContext = Dispatchers.Main +class PreviewFragment : Fragment() { private val viewModel: MarkdownViewModel by viewModels({ requireParentFragment() }) private var markdownPreview: WebView? = null private var style: String = "" @@ -35,7 +33,7 @@ class PreviewFragment : Fragment(), CoroutineScope { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { markdownPreview = view.findViewById(R.id.markdown_view) WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG) - launch { + lifecycleScope.launch { val isNightMode = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES || requireContext().resources.configuration.uiMode and UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES @@ -64,14 +62,14 @@ class PreviewFragment : Fragment(), CoroutineScope { override fun onAttach(context: Context) { super.onAttach(context) updateWebContent(viewModel.markdownUpdates.value ?: "") - viewModel.markdownUpdates.observe(this, Observer { + viewModel.markdownUpdates.observe(this, { updateWebContent(it) }) } private fun updateWebContent(markdown: String) { markdownPreview?.post { - launch { + lifecycleScope.launch { markdownPreview?.loadDataWithBaseURL(null, style + markdown.toHtml(), "text/html", @@ -82,9 +80,6 @@ class PreviewFragment : Fragment(), CoroutineScope { } override fun onDestroyView() { - coroutineContext[Job]?.let { - cancel() - } markdownPreview?.let { (it.parent as ViewGroup).removeView(it) it.destroy() diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt index 7fbdae5..4c051da 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt @@ -4,21 +4,19 @@ import android.content.SharedPreferences import android.os.Build import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate +import androidx.lifecycle.lifecycleScope import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.BuildConfig import com.wbrawner.simplemarkdown.R -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlin.coroutines.CoroutineContext class SettingsFragment : PreferenceFragmentCompat(), - SharedPreferences.OnSharedPreferenceChangeListener, - CoroutineScope { + SharedPreferences.OnSharedPreferenceChangeListener { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_general) if (BuildConfig.DEBUG) { @@ -31,11 +29,9 @@ class SettingsFragment } } - override val coroutineContext: CoroutineContext = Dispatchers.Main - override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - launch(context = Dispatchers.IO) { + lifecycleScope.launch(context = Dispatchers.IO) { val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity) sharedPreferences.registerOnSharedPreferenceChangeListener(this@SettingsFragment) (findPreference(getString(R.string.pref_key_dark_mode)) as? ListPreference)?.let {