diff --git a/app/src/main/java/com/chouten/app/data/repository/WebviewHandlerImpl.kt b/app/src/main/java/com/chouten/app/data/repository/WebviewHandlerImpl.kt index 7ecf639..e5f073b 100644 --- a/app/src/main/java/com/chouten/app/data/repository/WebviewHandlerImpl.kt +++ b/app/src/main/java/com/chouten/app/data/repository/WebviewHandlerImpl.kt @@ -9,7 +9,7 @@ import android.webkit.WebView import android.webkit.WebViewClient import com.chouten.app.BuildConfig import com.chouten.app.R -import com.chouten.app.domain.model.Payloads_V3.Action_V3 +import com.chouten.app.domain.model.Payloads_V2.Action_V2 import com.chouten.app.domain.repository.WebviewHandler import com.chouten.app.domain.repository.postWebMessage import com.chouten.app.presentation.ui.screens.info.SwitchConfig @@ -22,13 +22,13 @@ import kotlinx.serialization.json.Json import okhttp3.RequestBody.Companion.toRequestBody import javax.inject.Inject -class WebviewHandlerImpl> @Inject constructor( +class WebviewHandlerImpl> @Inject constructor( private val httpClient: Requests, /** * Converts an object such as { "action": "search", "result": "..." } to the correct payload */ - private val resultConverter: (Action_V3, String) -> BaseResultPayload -) : WebviewHandler { + private val resultConverter: (Action_V2, String) -> BaseResultPayload +) : WebviewHandler { override val formatVersion: Int = 2 @@ -79,7 +79,7 @@ class WebviewHandlerImpl + code: String, payload: WebviewHandler.Companion.WebviewPayload ) { if (!::webview.isInitialized) { throw IllegalStateException("Webview handler is not initialized") @@ -92,7 +92,7 @@ class WebviewHandlerImpl) { + override suspend fun submitPayload(payload: WebviewHandler.Companion.RequestPayload) { if (!::webview.isInitialized) { throw IllegalStateException("Webview handler is not initialized") } @@ -124,14 +124,14 @@ class WebviewHandlerImpl handleHttpRequest(payload) - Action_V3.RESULT -> callback( + Action_V2.HTTP_REQUEST -> handleHttpRequest(payload) + Action_V2.RESULT -> callback( resultConverter( payload.action, payload.result ?: "null" ) ) - Action_V3.ERROR -> destroy() + Action_V2.ERROR -> destroy() else -> throw IllegalArgumentException("Invalid action") } } @@ -173,7 +173,7 @@ class WebviewHandlerImpl + payload: WebviewHandler.Companion.RequestPayload ) { val responseText = withContext(Dispatchers.IO) { when (payload.method) { @@ -220,14 +220,14 @@ class WebviewHandlerImpl>( + json.decodeFromString>( data ) submitPayload(payload) } catch (e: Exception) { e.printStackTrace() val error = resultConverter( - Action_V3.ERROR, e.message ?: "Unknown Error" + Action_V2.ERROR, e.message ?: "Unknown Error" ) callback(error) } @@ -243,13 +243,13 @@ class WebviewHandlerImpl>> { + fun provideSearchWebviewHandler(client: Requests): WebviewHandler>> { return WebviewHandlerImpl(client) { action, result: String -> - Payloads_V3.GenericPayload(action, Json.decodeFromString(result)) + Payloads_V2.GenericPayload(action, Json.decodeFromString(result)) } } @OptIn(ExperimentalSerializationApi::class) @Provides - fun provideSwitchConfigWebviewHandler(client: Requests): WebviewHandler> { + fun provideSwitchConfigWebviewHandler(client: Requests): WebviewHandler> { val json = Json { ignoreUnknownKeys = true isLenient = true explicitNulls = false } return WebviewHandlerImpl(client) { action, result: String -> - Payloads_V3.GenericPayload(action, json.decodeFromString(result)) + Payloads_V2.GenericPayload(action, json.decodeFromString(result)) } } @OptIn(ExperimentalSerializationApi::class) @Provides - fun provideInfoMetadataWebviewHandler(client: Requests): WebviewHandler> { + fun provideInfoMetadataWebviewHandler(client: Requests): WebviewHandler> { val json = Json { ignoreUnknownKeys = true isLenient = true explicitNulls = false } return WebviewHandlerImpl(client) { action, result: String -> - Payloads_V3.GenericPayload(action, json.decodeFromString(result)) + Payloads_V2.GenericPayload(action, json.decodeFromString(result)) } } @OptIn(ExperimentalSerializationApi::class) @Provides - fun provideInfoEpListWebviewHandler(client: Requests): WebviewHandler>> { + fun provideInfoEpListWebviewHandler(client: Requests): WebviewHandler>> { val json = Json { ignoreUnknownKeys = true isLenient = true @@ -66,33 +66,33 @@ object WebviewModuleV3 { } return WebviewHandlerImpl(client) { action, result: String -> Log.d("WEBVIEW", "RESULT: $result") - Payloads_V3.GenericPayload(action, json.decodeFromString(result)) + Payloads_V2.GenericPayload(action, json.decodeFromString(result)) } } @OptIn(ExperimentalSerializationApi::class) @Provides - fun provideServerWebviewHandler(client: Requests): WebviewHandler>> { + fun provideServerWebviewHandler(client: Requests): WebviewHandler>> { val json = Json { ignoreUnknownKeys = true isLenient = true explicitNulls = false } return WebviewHandlerImpl(client) { action, result: String -> - Payloads_V3.GenericPayload(action, json.decodeFromString(result)) + Payloads_V2.GenericPayload(action, json.decodeFromString(result)) } } @OptIn(ExperimentalSerializationApi::class) @Provides - fun provideSourceWebviewHandler(client: Requests): WebviewHandler> { + fun provideSourceWebviewHandler(client: Requests): WebviewHandler> { val json = Json { ignoreUnknownKeys = true isLenient = true explicitNulls = false } return WebviewHandlerImpl(client) { action, result: String -> - Payloads_V3.GenericPayload(action, json.decodeFromString(result)) + Payloads_V2.GenericPayload(action, json.decodeFromString(result)) } } } diff --git a/app/src/main/java/com/chouten/app/domain/model/ModuleModel.kt b/app/src/main/java/com/chouten/app/domain/model/ModuleModel.kt index e251c1f..0fa2259 100644 --- a/app/src/main/java/com/chouten/app/domain/model/ModuleModel.kt +++ b/app/src/main/java/com/chouten/app/domain/model/ModuleModel.kt @@ -204,7 +204,7 @@ data class ModuleModel( * with the current version of the app. * The value is inclusive (e.g if 3, 3 is the maximum working version). */ - const val MAX_FORMAT_VERSION = 3 + const val MAX_FORMAT_VERSION = 2 /** * Convert the ByteArray icon to a Bitmap. diff --git a/app/src/main/java/com/chouten/app/domain/model/Payloads_V3.kt b/app/src/main/java/com/chouten/app/domain/model/Payloads_V2.kt similarity index 88% rename from app/src/main/java/com/chouten/app/domain/model/Payloads_V3.kt rename to app/src/main/java/com/chouten/app/domain/model/Payloads_V2.kt index b7a64c7..75fe96d 100644 --- a/app/src/main/java/com/chouten/app/domain/model/Payloads_V3.kt +++ b/app/src/main/java/com/chouten/app/domain/model/Payloads_V2.kt @@ -8,9 +8,9 @@ import kotlinx.serialization.Serializable * The payloads and actions for format version 2 * @see WebviewHandler */ -object Payloads_V3 { +object Payloads_V2 { @Serializable - enum class Action_V3 { + enum class Action_V2 { @SerialName("HTTPRequest") HTTP_REQUEST, @@ -59,12 +59,12 @@ object Payloads_V3 { */ @Serializable data class GenericPayload( - override val action: Action_V3, + override val action: Action_V2, val result: Result, - ) : WebviewHandler.Companion.ActionPayload { + ) : WebviewHandler.Companion.ActionPayload { @Serializable data class Result( - val action: Action_V3, + val action: Action_V2, val result: T, ) } diff --git a/app/src/main/java/com/chouten/app/domain/repository/WebviewHandler.kt b/app/src/main/java/com/chouten/app/domain/repository/WebviewHandler.kt index d24bd26..ee356d2 100644 --- a/app/src/main/java/com/chouten/app/domain/repository/WebviewHandler.kt +++ b/app/src/main/java/com/chouten/app/domain/repository/WebviewHandler.kt @@ -17,8 +17,8 @@ import kotlinx.serialization.serializer * @param ResultPayload the payload that the webview handler returns to the callback * @property formatVersion The version that the webview handler is targeting * @property callback The callback that the app uses to handle data from the webview handler - * @see com.chouten.app.domain.model.Payloads_V3.Action_V3 - * @see com.chouten.app.domain.model.Payloads_V3.GenericPayload + * @see com.chouten.app.domain.model.Payloads_V2.Action_V2 + * @see com.chouten.app.domain.model.Payloads_V2.GenericPayload * @see com.chouten.app.data.repository.WebviewHandlerImpl */ interface WebviewHandler, ResultPayload : WebviewHandler.Companion.ActionPayload> { @@ -146,7 +146,7 @@ interface WebviewHandler, ResultPayload : WebviewHandler.C * The interface used by the app to build a specific ResultPayload * @param Action the enum containing all the actions that the webview handler can handle * @property action The action - * @see com.chouten.app.domain.model.Payloads_V3.GenericPayload + * @see com.chouten.app.domain.model.Payloads_V2.GenericPayload */ interface ActionPayload> { val action: Action diff --git a/app/src/main/java/com/chouten/app/presentation/ui/screens/home/HomeViewModel.kt b/app/src/main/java/com/chouten/app/presentation/ui/screens/home/HomeViewModel.kt index 83354d6..d59de38 100644 --- a/app/src/main/java/com/chouten/app/presentation/ui/screens/home/HomeViewModel.kt +++ b/app/src/main/java/com/chouten/app/presentation/ui/screens/home/HomeViewModel.kt @@ -4,7 +4,7 @@ package com.chouten.app.presentation.ui.screens.home //class HomeViewModel @Inject constructor( // val application: Application, // val moduleUseCases: ModuleUseCases, -// val webviewHandler: WebviewHandler>>, +// val webviewHandler: WebviewHandler>>, // private val logUseCases: LogUseCases //) : ViewModel() { // init { @@ -21,7 +21,7 @@ package com.chouten.app.presentation.ui.screens.home // } // // webviewHandler.initialize(application) { res -> -// if (res.action != Action_V3.RESULT) return@initialize +// if (res.action != Action_V2.RESULT) return@initialize // Log.d("WEBVIEW", "RESULT: ${res.result.result}") // } // moduleUseCases.getModuleUris().find { @@ -30,7 +30,7 @@ package com.chouten.app.presentation.ui.screens.home // val code: String = model.code?.search?.getOrNull(0)?.code ?: "" // webviewHandler.load( // code, WebviewHandler.Companion.WebviewPayload( -// query = "Operation Barbershop", action = Action_V3.SEARCH +// query = "Operation Barbershop", action = Action_V2.SEARCH // ) // ) // } diff --git a/app/src/main/java/com/chouten/app/presentation/ui/screens/info/InfoViewModel.kt b/app/src/main/java/com/chouten/app/presentation/ui/screens/info/InfoViewModel.kt index af328e6..ee2c2b8 100644 --- a/app/src/main/java/com/chouten/app/presentation/ui/screens/info/InfoViewModel.kt +++ b/app/src/main/java/com/chouten/app/presentation/ui/screens/info/InfoViewModel.kt @@ -8,7 +8,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.chouten.app.common.Resource import com.chouten.app.domain.model.LogEntry -import com.chouten.app.domain.model.Payloads_V3 +import com.chouten.app.domain.model.Payloads_V2 import com.chouten.app.domain.proto.moduleDatastore import com.chouten.app.domain.repository.WebviewHandler import com.chouten.app.domain.use_case.log_use_cases.LogUseCases @@ -94,9 +94,9 @@ data class SwitchConfig( class InfoViewModel @Inject constructor( val application: Application, private val moduleUseCases: ModuleUseCases, - private val switchConfigHandler: WebviewHandler>, - private val metadataHandler: WebviewHandler>, - val epListHandler: WebviewHandler>>, + private val switchConfigHandler: WebviewHandler>, + private val metadataHandler: WebviewHandler>, + val epListHandler: WebviewHandler>>, private val logUseCases: LogUseCases, private val savedStateHandle: SavedStateHandle, ) : ViewModel() { @@ -183,7 +183,7 @@ class InfoViewModel @Inject constructor( epListHandler.logFn = { log(content = it) } switchConfigHandler.initialize(application) { res -> - if (res.action == Payloads_V3.Action_V3.ERROR) { + if (res.action == Payloads_V2.Action_V2.ERROR) { viewModelScope.launch { if (code.contains("function getSwitchConfig")) { // Not all modules have a switch config, we should only log if @@ -205,7 +205,7 @@ class InfoViewModel @Inject constructor( } } metadataHandler.initialize(application) { res -> - if (res.action == Payloads_V3.Action_V3.ERROR) { + if (res.action == Payloads_V2.Action_V2.ERROR) { viewModelScope.launch { log(content = "Failed to get info for $_title.\n${res.result.result}") _infoResults.emit( @@ -254,7 +254,7 @@ class InfoViewModel @Inject constructor( switchConfigHandler.load( getCode(), WebviewHandler.Companion.WebviewPayload( - query = "", action = Payloads_V3.Action_V3.GET_SWITCH_CONFIG + query = "", action = Payloads_V2.Action_V2.GET_SWITCH_CONFIG ) ) } @@ -284,14 +284,14 @@ class InfoViewModel @Inject constructor( } metadataHandler.load( getCode(), WebviewHandler.Companion.WebviewPayload( - query = _url, action = Payloads_V3.Action_V3.GET_METADATA + query = _url, action = Payloads_V2.Action_V2.GET_METADATA ) ) } suspend fun getEpisodes(eplistUrls: List, offset: Int = 0) { epListHandler.initialize(application) { res -> - if (res.action == Payloads_V3.Action_V3.ERROR) { + if (res.action == Payloads_V2.Action_V2.ERROR) { viewModelScope.launch { log(content = "Failed to get episodes for $_title.\n${res.result.result}") _episodeList.emit( @@ -359,7 +359,7 @@ class InfoViewModel @Inject constructor( epListHandler.load( getCode(), WebviewHandler.Companion.WebviewPayload( query = eplistUrls.getOrNull(offset) ?: "", - action = Payloads_V3.Action_V3.GET_EPISODE_LIST + action = Payloads_V2.Action_V2.GET_EPISODE_LIST ) ) diff --git a/app/src/main/java/com/chouten/app/presentation/ui/screens/search/SearchViewModel.kt b/app/src/main/java/com/chouten/app/presentation/ui/screens/search/SearchViewModel.kt index 2b2be06..294615e 100644 --- a/app/src/main/java/com/chouten/app/presentation/ui/screens/search/SearchViewModel.kt +++ b/app/src/main/java/com/chouten/app/presentation/ui/screens/search/SearchViewModel.kt @@ -7,7 +7,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.chouten.app.common.Resource import com.chouten.app.domain.model.LogEntry -import com.chouten.app.domain.model.Payloads_V3 +import com.chouten.app.domain.model.Payloads_V2 import com.chouten.app.domain.proto.moduleDatastore import com.chouten.app.domain.repository.WebviewHandler import com.chouten.app.domain.use_case.log_use_cases.LogUseCases @@ -43,7 +43,7 @@ data class SearchResult( class SearchViewModel @Inject constructor( val application: Application, val moduleUseCases: ModuleUseCases, - val webviewHandler: WebviewHandler>>, + val webviewHandler: WebviewHandler>>, private val logUseCases: LogUseCases, val savedStateHandle: SavedStateHandle ) : ViewModel() { @@ -108,7 +108,7 @@ class SearchViewModel @Inject constructor( } webviewHandler.initialize(application) { res -> - if (res.action == Payloads_V3.Action_V3.ERROR) { + if (res.action == Payloads_V2.Action_V2.ERROR) { viewModelScope.launch { _searchResults.emit( Resource.Error( @@ -160,7 +160,7 @@ class SearchViewModel @Inject constructor( webviewHandler.load( code, WebviewHandler.Companion.WebviewPayload( - query = searchQuery, action = Payloads_V3.Action_V3.SEARCH + query = searchQuery, action = Payloads_V2.Action_V2.SEARCH ) ) lastUsedModule = application.moduleDatastore.data.firstOrNull()?.selectedModuleId ?: "" diff --git a/app/src/main/java/com/chouten/app/presentation/ui/screens/watch/WatchViewModel.kt b/app/src/main/java/com/chouten/app/presentation/ui/screens/watch/WatchViewModel.kt index ec9136c..c60838a 100644 --- a/app/src/main/java/com/chouten/app/presentation/ui/screens/watch/WatchViewModel.kt +++ b/app/src/main/java/com/chouten/app/presentation/ui/screens/watch/WatchViewModel.kt @@ -8,7 +8,7 @@ import androidx.lifecycle.viewModelScope import com.chouten.app.R import com.chouten.app.common.UiText import com.chouten.app.domain.model.LogEntry -import com.chouten.app.domain.model.Payloads_V3 +import com.chouten.app.domain.model.Payloads_V2 import com.chouten.app.domain.proto.moduleDatastore import com.chouten.app.domain.repository.WebviewHandler import com.chouten.app.domain.use_case.log_use_cases.LogUseCases @@ -89,8 +89,8 @@ data class WatchViewModelState( @HiltViewModel class WatchViewModel @Inject constructor( - private val serverHandler: WebviewHandler>>, - private val videoHandler: WebviewHandler>, + private val serverHandler: WebviewHandler>>, + private val videoHandler: WebviewHandler>, private val moduleUseCases: ModuleUseCases, val savedStateHandle: SavedStateHandle, val application: Application, @@ -172,7 +172,7 @@ class WatchViewModel @Inject constructor( viewModelScope.launch { serverHandler.initialize(application) { res -> - if (res.action == Payloads_V3.Action_V3.ERROR) { + if (res.action == Payloads_V2.Action_V2.ERROR) { viewModelScope.launch { val statusValue = status.firstOrNull() savedStateHandle[STATUS] = statusValue?.copy( @@ -208,7 +208,7 @@ class WatchViewModel @Inject constructor( } videoHandler.initialize(application) { res -> - if (res.action == Payloads_V3.Action_V3.ERROR) { + if (res.action == Payloads_V2.Action_V2.ERROR) { viewModelScope.launch { val statusValue = status.firstOrNull() savedStateHandle[STATUS] = statusValue?.copy( @@ -279,7 +279,7 @@ class WatchViewModel @Inject constructor( serverHandler.load( getCode(), WebviewHandler.Companion.WebviewPayload( - action = Payloads_V3.Action_V3.GET_SERVER, query = url + action = Payloads_V2.Action_V2.GET_SERVER, query = url ) ) } @@ -287,7 +287,7 @@ class WatchViewModel @Inject constructor( suspend fun getSource(url: String) { videoHandler.load( getCode(), WebviewHandler.Companion.WebviewPayload( - action = Payloads_V3.Action_V3.GET_VIDEO, query = url + action = Payloads_V2.Action_V2.GET_VIDEO, query = url ) ) }