@ -1,8 +1,16 @@
package org.koitharu.kotatsu.settings.sources
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.plus
@ -13,8 +21,11 @@ import javax.inject.Inject
@HiltViewModel
class SourcesSettingsViewModel @Inject constructor (
sourcesRepository : MangaSourcesRepository ,
@ApplicationContext private val context : Context ,
) : BaseViewModel ( ) {
private val linksHandlerActivity = ComponentName ( context , " org.koitharu.kotatsu.details.ui.DetailsByLinkActivity " )
val enabledSourcesCount = sourcesRepository . observeEnabledSourcesCount ( )
. withErrorHandling ( )
. stateIn ( viewModelScope + Dispatchers . Default , SharingStarted . Eagerly , - 1 )
@ -22,4 +33,20 @@ class SourcesSettingsViewModel @Inject constructor(
val availableSourcesCount = sourcesRepository . observeAvailableSourcesCount ( )
. withErrorHandling ( )
. stateIn ( viewModelScope + Dispatchers . Default , SharingStarted . Eagerly , - 1 )
val isLinksEnabled = MutableStateFlow ( isLinksEnabled ( ) )
fun setLinksEnabled ( isEnabled : Boolean ) {
context . packageManager . setComponentEnabledSetting (
linksHandlerActivity ,
if ( isEnabled ) COMPONENT _ENABLED _STATE _ENABLED else COMPONENT _ENABLED _STATE _DISABLED ,
PackageManager . DONT _KILL _APP ,
)
isLinksEnabled . value = isLinksEnabled ( )
}
private fun isLinksEnabled ( ) : Boolean {
val state = context . packageManager . getComponentEnabledSetting ( linksHandlerActivity )
return state == COMPONENT _ENABLED _STATE _ENABLED || state == COMPONENT _ENABLED _STATE _DEFAULT
}
}