|
@@ -1,12 +1,16 @@
|
|
package com.codeskraps.sbrowser.feature.settings
|
|
package com.codeskraps.sbrowser.feature.settings
|
|
|
|
|
|
import android.webkit.WebSettings.PluginState
|
|
import android.webkit.WebSettings.PluginState
|
|
|
|
+import android.webkit.WebView
|
|
import androidx.lifecycle.viewModelScope
|
|
import androidx.lifecycle.viewModelScope
|
|
import com.codeskraps.sbrowser.MediaWebViewPreferences
|
|
import com.codeskraps.sbrowser.MediaWebViewPreferences
|
|
import com.codeskraps.sbrowser.feature.settings.mvi.SettingsAction
|
|
import com.codeskraps.sbrowser.feature.settings.mvi.SettingsAction
|
|
import com.codeskraps.sbrowser.feature.settings.mvi.SettingsEvent
|
|
import com.codeskraps.sbrowser.feature.settings.mvi.SettingsEvent
|
|
import com.codeskraps.sbrowser.feature.settings.mvi.SettingsState
|
|
import com.codeskraps.sbrowser.feature.settings.mvi.SettingsState
|
|
|
|
+import com.codeskraps.sbrowser.feature.webview.media.ClearCookies
|
|
import com.codeskraps.sbrowser.feature.webview.media.MediaWebView
|
|
import com.codeskraps.sbrowser.feature.webview.media.MediaWebView
|
|
|
|
+import com.codeskraps.sbrowser.feature.webview.media.TextSize
|
|
|
|
+import com.codeskraps.sbrowser.feature.webview.media.UserAgent
|
|
import com.codeskraps.sbrowser.util.StateReducerViewModel
|
|
import com.codeskraps.sbrowser.util.StateReducerViewModel
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.Dispatchers
|
|
@@ -28,8 +32,12 @@ class SettingsViewModel @Inject constructor(
|
|
SettingsState(
|
|
SettingsState(
|
|
homeUrl = mediaWebViewPreferences.homeUrl,
|
|
homeUrl = mediaWebViewPreferences.homeUrl,
|
|
javaScript = mediaWebViewPreferences.javaScript,
|
|
javaScript = mediaWebViewPreferences.javaScript,
|
|
- plugins = mediaWebViewPreferences.plugins,
|
|
|
|
|
|
+ textSize = mediaWebViewPreferences.textSize,
|
|
userAgent = mediaWebViewPreferences.userAgent,
|
|
userAgent = mediaWebViewPreferences.userAgent,
|
|
|
|
+ domStorage = mediaWebViewPreferences.domStorage,
|
|
|
|
+ acceptCookies = mediaWebViewPreferences.acceptCookies,
|
|
|
|
+ thirdPartyCookies = mediaWebViewPreferences.thirdPartyCookies,
|
|
|
|
+ clearCookies = mediaWebViewPreferences.clearCookies,
|
|
showUrl = mediaWebViewPreferences.showUrl
|
|
showUrl = mediaWebViewPreferences.showUrl
|
|
)
|
|
)
|
|
)
|
|
)
|
|
@@ -42,8 +50,12 @@ class SettingsViewModel @Inject constructor(
|
|
is SettingsEvent.Load -> event.state
|
|
is SettingsEvent.Load -> event.state
|
|
is SettingsEvent.Home -> onHome(currentState, event.url)
|
|
is SettingsEvent.Home -> onHome(currentState, event.url)
|
|
is SettingsEvent.JavaScript -> onJavaScript(currentState, event.value)
|
|
is SettingsEvent.JavaScript -> onJavaScript(currentState, event.value)
|
|
- is SettingsEvent.Plugins -> onPlugins(currentState, event.value)
|
|
|
|
|
|
+ is SettingsEvent.TextSize -> onTextSize(currentState, event.value)
|
|
is SettingsEvent.UserAgent -> onUserAgent(currentState, event.value)
|
|
is SettingsEvent.UserAgent -> onUserAgent(currentState, event.value)
|
|
|
|
+ is SettingsEvent.DomStorage -> onDomStorage(currentState, event.value)
|
|
|
|
+ is SettingsEvent.AcceptCookies -> onAcceptCookies(currentState, event.value)
|
|
|
|
+ is SettingsEvent.ThirdPartyCookies -> onThirdPartyCookies(currentState, event.value)
|
|
|
|
+ is SettingsEvent.ClearCookies -> onClearCookies(currentState, event.value)
|
|
is SettingsEvent.ShowUrl -> onShowUrl(currentState, event.value)
|
|
is SettingsEvent.ShowUrl -> onShowUrl(currentState, event.value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -59,20 +71,52 @@ class SettingsViewModel @Inject constructor(
|
|
return currentState.copy(javaScript = value)
|
|
return currentState.copy(javaScript = value)
|
|
}
|
|
}
|
|
|
|
|
|
- private fun onPlugins(currentState: SettingsState, value: PluginState): SettingsState {
|
|
|
|
- mediaWebView.settings.pluginState = value
|
|
|
|
- mediaWebViewPreferences.plugins = value
|
|
|
|
- return currentState.copy(plugins = value)
|
|
|
|
|
|
+ private fun onTextSize(currentState: SettingsState, value: TextSize): SettingsState {
|
|
|
|
+ mediaWebView.settings.textZoom = value.size
|
|
|
|
+ mediaWebViewPreferences.textSize = value
|
|
|
|
+ return currentState.copy(textSize = value)
|
|
}
|
|
}
|
|
|
|
|
|
- private fun onUserAgent(currentState: SettingsState, value: String): SettingsState {
|
|
|
|
- mediaWebView.settings.userAgentString?.let {
|
|
|
|
- mediaWebView.settings.userAgentString = it.replace("Android", value)
|
|
|
|
- }
|
|
|
|
|
|
+ private fun onUserAgent(currentState: SettingsState, value: UserAgent): SettingsState {
|
|
|
|
+ mediaWebView.settings.userAgentString = value.value
|
|
mediaWebViewPreferences.userAgent = value
|
|
mediaWebViewPreferences.userAgent = value
|
|
|
|
+ mediaWebView.reload()
|
|
return currentState.copy(userAgent = value)
|
|
return currentState.copy(userAgent = value)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private fun onDomStorage(currentState: SettingsState, value: Boolean): SettingsState {
|
|
|
|
+ mediaWebView.settings.domStorageEnabled = value
|
|
|
|
+ mediaWebViewPreferences.domStorage = value
|
|
|
|
+ return currentState.copy(domStorage = value)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun onAcceptCookies(currentState: SettingsState, value: Boolean): SettingsState {
|
|
|
|
+ with(mediaWebView.cookieManager) {
|
|
|
|
+ setAcceptCookie(value)
|
|
|
|
+ setAcceptThirdPartyCookies(
|
|
|
|
+ mediaWebView.attachView as WebView,
|
|
|
|
+ currentState.thirdPartyCookies
|
|
|
|
+ )
|
|
|
|
+ if (!value) removeAllCookies {}
|
|
|
|
+ }
|
|
|
|
+ mediaWebViewPreferences.acceptCookies = value
|
|
|
|
+ return currentState.copy(acceptCookies = value)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun onThirdPartyCookies(currentState: SettingsState, value: Boolean): SettingsState {
|
|
|
|
+ mediaWebView.cookieManager.setAcceptThirdPartyCookies(
|
|
|
|
+ mediaWebView.attachView as WebView,
|
|
|
|
+ value
|
|
|
|
+ )
|
|
|
|
+ mediaWebViewPreferences.thirdPartyCookies = value
|
|
|
|
+ return currentState.copy(thirdPartyCookies = value)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun onClearCookies(currentState: SettingsState, value: ClearCookies): SettingsState {
|
|
|
|
+ mediaWebViewPreferences.clearCookies = value
|
|
|
|
+ return currentState.copy(clearCookies = value)
|
|
|
|
+ }
|
|
|
|
+
|
|
private fun onShowUrl(currentState: SettingsState, value: Boolean): SettingsState {
|
|
private fun onShowUrl(currentState: SettingsState, value: Boolean): SettingsState {
|
|
mediaWebViewPreferences.showUrl = value
|
|
mediaWebViewPreferences.showUrl = value
|
|
return currentState.copy(showUrl = value)
|
|
return currentState.copy(showUrl = value)
|