Skip to content

Commit

Permalink
update js api
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Jan 26, 2025
1 parent 3e60c7e commit 8af3037
Show file tree
Hide file tree
Showing 18 changed files with 15,873 additions and 2,428 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.dergoogler.mmrl.ui.activity.webui.interfaces.ksu.AdvancedKernelSUAPI
import com.dergoogler.mmrl.ui.activity.webui.interfaces.ksu.BaseKernelSUAPI
import com.dergoogler.mmrl.ui.activity.webui.interfaces.mmrl.FileInterface
import com.dergoogler.mmrl.ui.activity.webui.interfaces.mmrl.MMRLInterface
import com.dergoogler.mmrl.ui.activity.webui.interfaces.mmrl.VersionInterface
import com.dergoogler.mmrl.ui.component.ConfirmDialog
import com.dergoogler.mmrl.ui.component.Loading
import com.dergoogler.mmrl.ui.providable.LocalUserPreferences
Expand Down Expand Up @@ -130,8 +131,6 @@ fun WebUIScreen(
AndroidView(
factory = {
webView.apply {


layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
Expand All @@ -148,6 +147,14 @@ fun WebUIScreen(
userPrefs = userPrefs,
viewModel = viewModel,
)

addJavascriptInterface(
VersionInterface(
context = context,
webView = this,
viewModel = viewModel,
), "mmrl"
)
}
},
update = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.dergoogler.mmrl.ui.activity.webui.interfaces.mmrl

import android.content.Context
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.widget.Toast
import com.dergoogler.mmrl.BuildConfig
import com.dergoogler.mmrl.viewmodel.WebUIViewModel

class VersionInterface(
private val context: Context,
private val webView: WebView,
private val viewModel: WebUIViewModel,
) {
inner class BuildConfigDetails {
@get:JavascriptInterface
val applicationId = BuildConfig.APPLICATION_ID

@get:JavascriptInterface
val buildType = BuildConfig.BUILD_TYPE

@get:JavascriptInterface
val versionCode = BuildConfig.VERSION_CODE

@get:JavascriptInterface
val versionName = BuildConfig.VERSION_NAME

@get:JavascriptInterface
val isDevVersion: Boolean = BuildConfig.IS_DEV_VERSION

@get:JavascriptInterface
val isGooglePlayBuild: Boolean = BuildConfig.IS_GOOGLE_PLAY_BUILD
}

inner class RootConfigDetails {
@get:JavascriptInterface
val platform = viewModel.platform.current

@get:JavascriptInterface
val versionName = viewModel.versionName

@get:JavascriptInterface
val versionCode = viewModel.versionCode
}

@get:JavascriptInterface
val buildConfig get() = BuildConfigDetails()

@get:JavascriptInterface
val rootConfig get() = RootConfigDetails()

inner class ToastBuilderInterface {
private val toast = Toast(context)

@JavascriptInterface
fun setText(msg: String) {
toast.setText(msg)
}

@JavascriptInterface
fun show() {
webView.post {
toast.show()
}
}

@JavascriptInterface
fun cancel() {
webView.post {
toast.cancel()
}
}

@JavascriptInterface
fun setDuration(duration: Int) {
toast.duration = duration
}
}

@JavascriptInterface
fun toastBuilder(): ToastBuilderInterface {
return ToastBuilderInterface()
}
}
6 changes: 6 additions & 0 deletions js/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
"@babel/plugin-proposal-class-properties"
]
}
1 change: 1 addition & 0 deletions js/.npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Dependency directories
node_modules/
.parcel-cache/

src/
test/
Expand Down
4 changes: 4 additions & 0 deletions js/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"bundler": "@parcel/bundler-library"
}
Binary file added js/bun.lockb
Binary file not shown.
Loading

0 comments on commit 8af3037

Please sign in to comment.