Skip to content

Commit

Permalink
Remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Feb 18, 2020
1 parent 8ddfad0 commit 44e9932
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 47 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?,
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -252,16 +251,15 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen
viewModel.autoScroll = false
val log = adapter[pos]
FilterExclusionDialogFragment.newInstance(log)
.show(fragmentManager!!, FilterExclusionDialogFragment.TAG)
.show(parentFragmentManager, FilterExclusionDialogFragment.TAG)
}
}

if (!checkReadLogsPermission() && !viewModel.showedGrantPermissionInstruction) {
viewModel.showedGrantPermissionInstruction = true
NeedPermissionDialogFragment().let {
it.setTargetFragment(this, 0)
it.show(fragmentManager!!,
NeedPermissionDialogFragment.TAG)
it.show(parentFragmentManager, NeedPermissionDialogFragment.TAG)
}
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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}"
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)))
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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<List<FileHolder>> {
if (it != null) {
recyclerViewAdapter.setData(it)
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/com/dp/logcatapp/util/ViewModelUtil.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : ViewModel> ComponentActivity.getAndroidViewModel(): T {
val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
return ViewModelProvider(this, factory).get(T::class.java)
}

inline fun <reified T : ViewModel> Fragment.getAndroidViewModel(): T {
val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(activity!!.application)
return ViewModelProvider(this, factory).get(T::class.java)
}

inline fun <reified T : ViewModel> AppCompatActivity.getViewModel(): T {
return ViewModelProvider(this).get(T::class.java)
}

inline fun <reified T : ViewModel> Fragment.getViewModel(): T {
return ViewModelProvider(this).get(T::class.java)
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 44e9932

Please sign in to comment.