diff --git a/app/build.gradle b/app/build.gradle index 4fd422d8..866de452 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -79,6 +79,7 @@ dependencies { implementation "androidx.legacy:legacy-preference-v14:$legacy_preference_v14_version" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_common_java8_version" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_extensions_version" + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_viewmodel_ktx_version" implementation "androidx.constraintlayout:constraintlayout:$constraint_layout_version" implementation "androidx.room:room-runtime:$room_version" implementation "androidx.documentfile:documentfile:$documentfile_version" diff --git a/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt index e71158c2..52cb55a8 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt @@ -6,7 +6,6 @@ import android.view.* import android.widget.ImageButton import android.widget.TextView import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager @@ -17,6 +16,7 @@ import com.dp.logcatapp.db.FilterInfo import com.dp.logcatapp.fragments.base.BaseFragment import com.dp.logcatapp.fragments.filters.dialogs.FilterDialogFragment import com.dp.logcatapp.model.LogcatMsg +import com.dp.logcatapp.util.getAndroidViewModel import com.dp.logcatapp.util.inflateLayout class FiltersFragment : BaseFragment() { @@ -44,8 +44,7 @@ class FiltersFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) - viewModel = ViewModelProviders.of(activity!!) - .get(FiltersViewModel::class.java) + viewModel = activity!!.getAndroidViewModel() recyclerViewAdapter = MyRecyclerViewAdapter { onRemoveClicked(it) } @@ -119,13 +118,13 @@ class FiltersFragment : BaseFragment() { } private fun showAddFilter() { - var frag = fragmentManager?.findFragmentByTag(FilterDialogFragment.TAG) as? FilterDialogFragment + var frag = parentFragmentManager.findFragmentByTag(FilterDialogFragment.TAG) as? FilterDialogFragment if (frag == null) { frag = FilterDialogFragment.newInstance(getLog()) } frag.setTargetFragment(this, 0) - frag.show(fragmentManager!!, FilterDialogFragment.TAG) + frag.show(parentFragmentManager, FilterDialogFragment.TAG) } @SuppressLint("CheckResult") diff --git a/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt index 42046f68..c3b524f9 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt @@ -8,15 +8,13 @@ import android.widget.CheckBox import android.widget.EditText import androidx.appcompat.app.AlertDialog import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProviders import com.dp.logcat.Log import com.dp.logcat.LogPriority import com.dp.logcatapp.R -import com.dp.logcatapp.activities.FiltersActivity.Companion.KEY_LOG - import com.dp.logcatapp.fragments.base.BaseDialogFragment import com.dp.logcatapp.fragments.filters.FiltersFragment import com.dp.logcatapp.model.LogcatMsg +import com.dp.logcatapp.util.getViewModel import com.dp.logcatapp.util.inflateLayout class FilterDialogFragment : BaseDialogFragment() { @@ -39,8 +37,7 @@ class FilterDialogFragment : BaseDialogFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - viewModel = ViewModelProviders.of(this) - .get(MyViewModel::class.java) + viewModel = getViewModel() initViewModel(getLog()) } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt index ec028ff8..fd7f314c 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt @@ -12,7 +12,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.SearchView import androidx.core.content.ContextCompat import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -190,7 +189,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen adapter = MyRecyclerViewAdapter(activity!!, maxLogs) activity!!.getDefaultSharedPreferences().registerOnSharedPreferenceChangeListener(adapter) - viewModel = ViewModelProviders.of(activity!!).get(LogcatLiveViewModel::class.java) + viewModel = activity!!.getAndroidViewModel() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, @@ -242,7 +241,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen viewModel.autoScroll = false val log = adapter[pos] CopyToClipboardDialogFragment.newInstance(log) - .show(fragmentManager!!, CopyToClipboardDialogFragment.TAG) + .show(parentFragmentManager, CopyToClipboardDialogFragment.TAG) } } @@ -252,7 +251,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen viewModel.autoScroll = false val log = adapter[pos] FilterExclusionDialogFragment.newInstance(log) - .show(fragmentManager!!, FilterExclusionDialogFragment.TAG) + .show(parentFragmentManager, FilterExclusionDialogFragment.TAG) } } @@ -260,8 +259,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen viewModel.showedGrantPermissionInstruction = true NeedPermissionDialogFragment().let { it.setTargetFragment(this, 0) - it.show(fragmentManager!!, - NeedPermissionDialogFragment.TAG) + it.show(parentFragmentManager, NeedPermissionDialogFragment.TAG) } } @@ -309,7 +307,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen when (it.result) { SaveInfo.SUCCESS -> { OnSavedBottomSheetDialogFragment.newInstance(it.fileName!!, it.uri!!) - .show(fragmentManager!!, OnSavedBottomSheetDialogFragment.TAG) + .show(parentFragmentManager, OnSavedBottomSheetDialogFragment.TAG) } SaveInfo.ERROR_EMPTY_LOGS -> { showSnackbar(view, getString(R.string.nothing_to_save)) @@ -678,7 +676,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen fun useRootToGrantPermission() { scope.launch { val dialog = AskingForRootAccessDialogFragment() - dialog.show(fragmentManager!!, AskingForRootAccessDialogFragment.TAG) + dialog.show(parentFragmentManager, AskingForRootAccessDialogFragment.TAG) val result = withContext(IO) { val cmd = "pm grant ${BuildConfig.APPLICATION_ID} ${Manifest.permission.READ_LOGS}" @@ -687,11 +685,11 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen dialog.dismissAllowingStateLoss() if (result) { - RestartAppMessageDialogFragment.newInstance().show(fragmentManager!!, + RestartAppMessageDialogFragment.newInstance().show(parentFragmentManager, RestartAppMessageDialogFragment.TAG) } else { activity!!.showToast(getString(R.string.fail)) - ManualMethodToGrantPermissionDialogFragment().show(fragmentManager!!, + ManualMethodToGrantPermissionDialogFragment().show(parentFragmentManager, ManualMethodToGrantPermissionDialogFragment.TAG) } } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt index ee91884d..fa2ce2df 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt @@ -14,7 +14,7 @@ class NeedPermissionDialogFragment : BaseDialogFragment() { .setTitle(R.string.read_logs_permission_required) .setMessage(R.string.read_logs_permission_required_msg) .setPositiveButton(R.string.manual_method) { _, _ -> - ManualMethodToGrantPermissionDialogFragment().show(fragmentManager!!, + ManualMethodToGrantPermissionDialogFragment().show(parentFragmentManager, ManualMethodToGrantPermissionDialogFragment.TAG) } .setNegativeButton(R.string.root_method) { _, _ -> diff --git a/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt index 60fee947..1c6e1413 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt @@ -23,7 +23,6 @@ import androidx.core.net.toFile import androidx.core.net.toUri import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -64,8 +63,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - viewModel = ViewModelProviders.of(this) - .get(SavedLogsViewModel::class.java) + viewModel = getAndroidViewModel() recyclerViewAdapter = MyRecyclerViewAdapter(activity!!, this, this, viewModel.selectedItems) @@ -89,7 +87,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick recyclerView.layoutManager = linearLayoutManager recyclerView.adapter = recyclerViewAdapter - fragmentManager?.findFragmentByTag(RenameDialogFragment.TAG) + parentFragmentManager.findFragmentByTag(RenameDialogFragment.TAG) ?.setTargetFragment(this, 0) viewModel.getFileNames().observe(viewLifecycleOwner, Observer { @@ -198,13 +196,13 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick val fileInfo = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0]) val frag = RenameDialogFragment.newInstance(fileInfo.info.fileName, fileInfo.info.path) frag.setTargetFragment(this, 0) - frag.show(fragmentManager!!, RenameDialogFragment.TAG) + frag.show(parentFragmentManager, RenameDialogFragment.TAG) true } R.id.action_export -> { val dialog = ChooseExportFormatTypeDialogFragment() dialog.setTargetFragment(this, 0) - dialog.show(fragmentManager!!, ChooseExportFormatTypeDialogFragment.TAG) + dialog.show(parentFragmentManager, ChooseExportFormatTypeDialogFragment.TAG) true } R.id.action_share -> { diff --git a/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt index 45ab2ccc..a1b997de 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt @@ -7,7 +7,6 @@ import android.widget.ProgressBar import android.widget.TextView import androidx.appcompat.widget.SearchView import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -16,10 +15,7 @@ import com.dp.logcatapp.R import com.dp.logcatapp.activities.BaseActivityWithToolbar import com.dp.logcatapp.fragments.base.BaseFragment import com.dp.logcatapp.fragments.shared.dialogs.CopyToClipboardDialogFragment -import com.dp.logcatapp.util.LifecycleScope -import com.dp.logcatapp.util.containsIgnoreCase -import com.dp.logcatapp.util.inflateLayout -import com.dp.logcatapp.util.showToast +import com.dp.logcatapp.util.* import com.google.android.material.floatingactionbutton.FloatingActionButton import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job @@ -161,8 +157,7 @@ class SavedLogsViewerFragment : BaseFragment() { super.onCreate(savedInstanceState) setHasOptionsMenu(true) adapter = MyRecyclerViewAdapter(activity!!) - viewModel = ViewModelProviders.of(this) - .get(SavedLogsViewerViewModel::class.java) + viewModel = getAndroidViewModel() viewModel.init(Uri.parse(arguments!!.getString(KEY_FILE_URI))) } @@ -212,7 +207,7 @@ class SavedLogsViewerFragment : BaseFragment() { viewModel.autoScroll = false val log = adapter[pos] CopyToClipboardDialogFragment.newInstance(log) - .show(fragmentManager!!, CopyToClipboardDialogFragment.TAG) + .show(parentFragmentManager, CopyToClipboardDialogFragment.TAG) } } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt index 0f2c09e5..6db0e04e 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt @@ -198,15 +198,15 @@ class SettingsFragment : PreferenceFragmentCompat() { prefSaveLocation.setOnPreferenceClickListener { val frag = SaveLocationDialogFragment() frag.setTargetFragment(this@SettingsFragment, 0) - frag.show(fragmentManager!!, SaveLocationDialogFragment.TAG) + frag.show(parentFragmentManager, SaveLocationDialogFragment.TAG) true } - val frag = fragmentManager?.findFragmentByTag(SaveLocationDialogFragment.TAG) + val frag = parentFragmentManager.findFragmentByTag(SaveLocationDialogFragment.TAG) frag?.setTargetFragment(this, 0) - val folderChooserFragment = fragmentManager - ?.findFragmentByTag(FolderChooserDialogFragment.TAG) + val folderChooserFragment = parentFragmentManager + .findFragmentByTag(FolderChooserDialogFragment.TAG) folderChooserFragment?.setTargetFragment(this, 0) } @@ -220,7 +220,7 @@ class SettingsFragment : PreferenceFragmentCompat() { if (isExternalStorageWritable()) { val frag = FolderChooserDialogFragment() frag.setTargetFragment(this, 0) - frag.show(fragmentManager!!, FolderChooserDialogFragment.TAG) + frag.show(parentFragmentManager, FolderChooserDialogFragment.TAG) } else { activity!!.showToast(getString(R.string.err_msg_external_storage_not_writable)) } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt index 1f96257d..e8fc3ca3 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt @@ -14,16 +14,12 @@ import androidx.appcompat.app.AlertDialog import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.dp.logcatapp.R import com.dp.logcatapp.fragments.base.BaseDialogFragment import com.dp.logcatapp.fragments.settings.SettingsFragment -import com.dp.logcatapp.util.PreferenceKeys -import com.dp.logcatapp.util.getAttributeDrawable -import com.dp.logcatapp.util.getDefaultSharedPreferences -import com.dp.logcatapp.util.inflateLayout +import com.dp.logcatapp.util.* import java.io.File @@ -41,8 +37,7 @@ class FolderChooserDialogFragment : BaseDialogFragment(), View.OnClickListener { super.onCreate(savedInstanceState) recyclerViewAdapter = MyRecyclerViewAdapter(context!!, this) - viewModel = ViewModelProviders.of(this) - .get(MyViewModel::class.java) + viewModel = getAndroidViewModel() viewModel.files.observe(this, Observer> { if (it != null) { recyclerViewAdapter.setData(it) diff --git a/app/src/main/java/com/dp/logcatapp/util/ViewModelUtil.kt b/app/src/main/java/com/dp/logcatapp/util/ViewModelUtil.kt new file mode 100644 index 00000000..f57403e8 --- /dev/null +++ b/app/src/main/java/com/dp/logcatapp/util/ViewModelUtil.kt @@ -0,0 +1,25 @@ +package com.dp.logcatapp.util + +import androidx.activity.ComponentActivity +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider + +inline fun ComponentActivity.getAndroidViewModel(): T { + val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application) + return ViewModelProvider(this, factory).get(T::class.java) +} + +inline fun Fragment.getAndroidViewModel(): T { + val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(activity!!.application) + return ViewModelProvider(this, factory).get(T::class.java) +} + +inline fun AppCompatActivity.getViewModel(): T { + return ViewModelProvider(this).get(T::class.java) +} + +inline fun Fragment.getViewModel(): T { + return ViewModelProvider(this).get(T::class.java) +} diff --git a/build.gradle b/build.gradle index c747fcbb..02062268 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,7 @@ ext { legacy_preference_v14_version = "1.0.0" lifecycle_common_java8_version = "2.2.0" lifecycle_extensions_version = "2.2.0" + lifecycle_viewmodel_ktx_version = "2.2.0" constraint_layout_version = "1.1.3" room_version = "2.2.3" kotlin_coroutines_core_version = "1.2.2"