Skip to content

Commit

Permalink
revert: ⏪ change v3 back to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
TobyBridle committed Nov 22, 2023
1 parent 41d914f commit 5c548f7
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,13 +22,13 @@ import kotlinx.serialization.json.Json
import okhttp3.RequestBody.Companion.toRequestBody
import javax.inject.Inject

class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayload<Action_V3>> @Inject constructor(
class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayload<Action_V2>> @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<Action_V3, BaseResultPayload> {
private val resultConverter: (Action_V2, String) -> BaseResultPayload
) : WebviewHandler<Action_V2, BaseResultPayload> {

override val formatVersion: Int = 2

Expand Down Expand Up @@ -79,7 +79,7 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
* @throws IllegalStateException if the common code is not initialized
*/
override suspend fun load(
code: String, payload: WebviewHandler.Companion.WebviewPayload<Action_V3>
code: String, payload: WebviewHandler.Companion.WebviewPayload<Action_V2>
) {
if (!::webview.isInitialized) {
throw IllegalStateException("Webview handler is not initialized")
Expand All @@ -92,7 +92,7 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
// We need to wait for the webview to finish loading before we can inject the payload
override fun onPageFinished(view: WebView?, url: String?) {
val response = WebviewHandler.Companion.BasePayload(
requestId = "-1", action = Action_V3.LOGIC, payload = payload
requestId = "-1", action = Action_V2.LOGIC, payload = payload
)

webview.postWebMessage(response)
Expand All @@ -112,7 +112,7 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
* @throws IllegalStateException if the webview handler is not initialized
* @throws IllegalStateException if the common code is not initialized
*/
override suspend fun submitPayload(payload: WebviewHandler.Companion.RequestPayload<Action_V3>) {
override suspend fun submitPayload(payload: WebviewHandler.Companion.RequestPayload<Action_V2>) {
if (!::webview.isInitialized) {
throw IllegalStateException("Webview handler is not initialized")
}
Expand All @@ -124,14 +124,14 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
}

when (payload.action) {
Action_V3.HTTP_REQUEST -> 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")
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
* @throws IllegalArgumentException if the method is invalid
*/
private suspend fun handleHttpRequest(
payload: WebviewHandler.Companion.RequestPayload<Action_V3>
payload: WebviewHandler.Companion.RequestPayload<Action_V2>
) {
val responseText = withContext(Dispatchers.IO) {
when (payload.method) {
Expand Down Expand Up @@ -220,14 +220,14 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
runBlocking {
try {
val payload =
json.decodeFromString<WebviewHandler.Companion.RequestPayload<Action_V3>>(
json.decodeFromString<WebviewHandler.Companion.RequestPayload<Action_V2>>(
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)
}
Expand All @@ -243,13 +243,13 @@ class WebviewHandlerImpl<BaseResultPayload : WebviewHandler.Companion.ActionPayl
runBlocking {
try {
val parsed = resultConverter(
Action_V3.RESULT, data
Action_V2.RESULT, data
)
callback(parsed)
} catch (e: Exception) {
e.printStackTrace()
val error = resultConverter(
Action_V3.ERROR, e.message ?: "Unknown Error"
Action_V2.ERROR, e.message ?: "Unknown Error"
)
callback(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.chouten.app.di

import android.util.Log
import com.chouten.app.data.repository.WebviewHandlerImpl
import com.chouten.app.domain.model.Payloads_V3
import com.chouten.app.domain.model.Payloads_V2
import com.chouten.app.domain.repository.WebviewHandler
import com.chouten.app.presentation.ui.screens.info.InfoResult
import com.chouten.app.presentation.ui.screens.info.SwitchConfig
Expand All @@ -20,79 +20,79 @@ import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object WebviewModuleV3 {
object WebviewModuleV2 {

@Singleton
@Provides
fun provideSearchWebviewHandler(client: Requests): WebviewHandler<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<List<SearchResult>>> {
fun provideSearchWebviewHandler(client: Requests): WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<List<SearchResult>>> {
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<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<SwitchConfig>> {
fun provideSwitchConfigWebviewHandler(client: Requests): WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<SwitchConfig>> {
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<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<InfoResult>> {
fun provideInfoMetadataWebviewHandler(client: Requests): WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<InfoResult>> {
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<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<List<InfoResult.MediaListItem>>> {
fun provideInfoEpListWebviewHandler(client: Requests): WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<List<InfoResult.MediaListItem>>> {
val json = Json {
ignoreUnknownKeys = true
isLenient = true
explicitNulls = false
}
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<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<List<WatchResult.ServerData>>> {
fun provideServerWebviewHandler(client: Requests): WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<List<WatchResult.ServerData>>> {
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<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<WatchResult>> {
fun provideSourceWebviewHandler(client: Requests): WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<WatchResult>> {
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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -59,12 +59,12 @@ object Payloads_V3 {
*/
@Serializable
data class GenericPayload<T>(
override val action: Action_V3,
override val action: Action_V2,
val result: Result<T>,
) : WebviewHandler.Companion.ActionPayload<Action_V3> {
) : WebviewHandler.Companion.ActionPayload<Action_V2> {
@Serializable
data class Result<T>(
val action: Action_V3,
val action: Action_V2,
val result: T,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Action : Enum<Action>, ResultPayload : WebviewHandler.Companion.ActionPayload<Action>> {
Expand Down Expand Up @@ -146,7 +146,7 @@ interface WebviewHandler<Action : Enum<Action>, 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<Action : Enum<Action>> {
val action: Action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Action_V3, GenericPayload<List<SearchResult>>>,
// val webviewHandler: WebviewHandler<Action_V2, GenericPayload<List<SearchResult>>>,
// private val logUseCases: LogUseCases
//) : ViewModel() {
// init {
Expand All @@ -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 {
Expand All @@ -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
// )
// )
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,9 +94,9 @@ data class SwitchConfig(
class InfoViewModel @Inject constructor(
val application: Application,
private val moduleUseCases: ModuleUseCases,
private val switchConfigHandler: WebviewHandler<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<SwitchConfig>>,
private val metadataHandler: WebviewHandler<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<InfoResult>>,
val epListHandler: WebviewHandler<Payloads_V3.Action_V3, Payloads_V3.GenericPayload<List<InfoResult.MediaListItem>>>,
private val switchConfigHandler: WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<SwitchConfig>>,
private val metadataHandler: WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<InfoResult>>,
val epListHandler: WebviewHandler<Payloads_V2.Action_V2, Payloads_V2.GenericPayload<List<InfoResult.MediaListItem>>>,
private val logUseCases: LogUseCases,
private val savedStateHandle: SavedStateHandle,
) : ViewModel() {
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
)
)
}
Expand Down Expand Up @@ -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<String>, 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(
Expand Down Expand Up @@ -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
)
)

Expand Down
Loading

0 comments on commit 5c548f7

Please sign in to comment.