Skip to content

Commit

Permalink
Merge pull request #765 from aiselp/dev-test
Browse files Browse the repository at this point in the history
编辑器更新,添加rxjs特有调度器
  • Loading branch information
kkevsekk1 authored Dec 13, 2023
2 parents 10b0bb2 + 803d0d0 commit 0ab8ec6
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 51 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ dependencies {
// Android job
implementation("com.evernote:android-job:1.4.2")
// Optional, if you use support library fragments:
implementation(project(":automator"))
implementation(project(":common"))
implementation(project(":autojs"))
implementation(project(":apkbuilder"))
implementation(project(":codeeditor"))
Expand Down Expand Up @@ -330,4 +328,9 @@ tasks.register("buildDebugTemplateApp") {
doFirst {
copyTemplateToAPP(true, assetsDir)
}
}
tasks.named("clean").configure {
doFirst {
delete(File(assetsDir, "template.apk"))
}
}
24 changes: 19 additions & 5 deletions app/src/main/java/org/autojs/autojs/devplugin/DevPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import com.stardust.app.GlobalAppContext
import io.ktor.server.plugins.*
import io.ktor.websocket.*
import kotlinx.coroutines.*
import io.ktor.websocket.CloseReason
import io.ktor.websocket.Frame
import io.ktor.websocket.FrameType
import io.ktor.websocket.WebSocketSession
import io.ktor.websocket.close
import io.ktor.websocket.readBytes
import io.ktor.websocket.readText
import io.ktor.websocket.send
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okio.ByteString.Companion.toByteString
import org.autojs.autojs.devplugin.message.Hello
import org.autojs.autojs.devplugin.message.HelloResponse
Expand All @@ -21,7 +36,6 @@ import org.autojs.autoxjs.BuildConfig
import org.autojs.autoxjs.R
import java.io.File
import java.net.SocketTimeoutException
import java.util.*

object DevPlugin {

Expand Down
191 changes: 191 additions & 0 deletions autojs/src/main/assets/modules/npm/rxjs/ext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
let { Subscription, asyncScheduler } = require("./index.js");
const t = threads;
const mainTimer = timers.mainTimer;
const uiTimer = timers.uiTimer;

const ioScheduler = {
bid: 0,
schedule(work, delay, state) {
const action = new SchedulerAction(this, work);
return action.schedule(state, delay);
},
now() {
return Date.now();
},
getexecutor() {
return {
id: this.bid++,
th: null,
run(fn, delay) {
if (this.th) {
this.th.setTimeout(fn, delay);
} else {
this.th = t.start(() => {
setTimeout(fn, delay);
});
}
},
close() {
this.th.interrupt();
this.th = null;
//console.log("关闭", this.id);
},
cancel() {
//console.log("取消", this.id);
},
};
},
};

const SchedulerAction = function (scheduler, work) {
Subscription.call(this);
this.work = work;
//this.subAction = [];
this.executor = scheduler.getexecutor();
};
SchedulerAction.prototype = Object.create(Subscription.prototype, {
constructor: { value: SchedulerAction },
});
Object.assign(SchedulerAction.prototype, {
schedule(state, delay) {
if (this.closed) return new Subscription();
const call = this.executor.run(() => {
this.work.call(this, state);
}, delay);
const callAction = new Subscription();
callAction.call = call;
callAction.executor = this.executor;
callAction.unsubscribe = function () {
this.closed = true;
const cancel = this.executor.cancel;
if (cancel) {
cancel.call(this.executor, this.call);
}
};
return callAction;
},
unsubscribe() {
this.closed = true;
this.executor.close();
},
});

const mainScheduler = {
bid: 0,
schedule(work, delay, state) {
const action = new SchedulerAction(this, work);
return action.schedule(state, delay);
},
now() {
return Date.now();
},
getexecutor() {
return {
id: null,
run(fn, delay) {
const id = mainTimer.setTimeout(fn, delay);
return id;
},
close() {
//console.log("关闭");
},
cancel(id) {
mainTimer.clearTimeout(id);
},
};
},
};

const uiScheduler = {
bid: 0,
schedule(work, delay, state) {
const action = new SchedulerAction(this, work);
return action.schedule(state, delay);
},
now() {
return Date.now();
},
getexecutor() {
return {
id: null,
run(fn, delay) {
const id = uiTimer.setTimeout(fn, delay);
return id;
},
close() {
//console.log("关闭");
},
cancel(id) {
uiTimer.clearTimeout(id);
},
};
},
};
const workScheduler = {
bid: 0,
schedule(work, delay, state) {
const action = new SchedulerAction(this, work);
return action.schedule(state, delay);
},
now() {
return Date.now();
},
getexecutor() {
return {
id: null,
run(fn, delay) {
if (delay) {
const id = uiTimer.setTimeout(() => {
t.runTaskForThreadPool(fn);
}, delay);
return id;
} else {
t.runTaskForThreadPool(fn);
return null;
}
},
close() {},
cancel(id) {
if (id) uiTimer.clearTimeout(id);
},
};
},
};
function newSingleScheduler() {
const th = t.start(() => {
setInterval(() => {}, 1000);
});
th.waitFor();
return {
th,
bid: 0,
schedule(work, delay, state) {
const action = new SchedulerAction(this, work);
return action.schedule(state, delay);
},
now() {
return Date.now();
},
getexecutor() {
return {
th: this.th,
run(fn, delay) {
return this.th.setTimeout(fn, delay);
},
close() {},
cancel(id) {
this.th.clearTimeout(id);
},
};
},
recycle(){
this.th.interrupt()
}
};
}

exports.ioScheduler = ioScheduler;
exports.mainScheduler = mainScheduler;
exports.uiScheduler = uiScheduler;
exports.workScheduler = workScheduler;
exports.newSingleScheduler = newSingleScheduler;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.stardust.autojs.runtime.ScriptRuntime
*/
class Timers(private val mRuntime: ScriptRuntime) {
private val mThreads: Threads = mRuntime.threads
private val mUiTimer: Timer = Timer(mRuntime, Looper.getMainLooper())
val uiTimer: Timer = Timer(mRuntime, Looper.getMainLooper())
val mainTimer
get() = mRuntime.loopers.mTimer

Expand All @@ -23,7 +23,7 @@ class Timers(private val mRuntime: ScriptRuntime) {
}
val timer = TimerThread.getTimerForThread(thread)
return if (timer == null && Looper.myLooper() == Looper.getMainLooper()) {
mUiTimer
uiTimer
} else timer ?: mainTimer
}

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ buildscript {
classpath("com.jakewharton:butterknife-gradle-plugin:10.2.3")
classpath("org.codehaus.groovy:groovy-json:3.0.8")
classpath("com.yanzhenjie.andserver:plugin:2.1.12")
classpath(libs.okhttp)
}
}

Expand Down
22 changes: 13 additions & 9 deletions codeeditor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import java.net.URL
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.use
plugins {
id("com.android.library")
id("kotlin-android")
Expand Down Expand Up @@ -38,27 +40,25 @@ android {
dependencies {

implementation(libs.andserver.api)
implementation(libs.androidx.constraintlayout)
kapt(libs.andserver.processor)
implementation(libs.kotlinx.coroutines.android)
api(libs.androidx.webkit)
implementation(libs.google.gson)
implementation(libs.core.ktx)
implementation(libs.androidx.activity.ktx)
implementation(libs.appcompat)
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.5.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0")
implementation(libs.material)
implementation(project(":autojs"))

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core){
exclude(group = "com.android.support",module = "support-annotations")
}
androidTestImplementation(libs.espresso.core)
}

tasks.register("downloadEditor") {
val tag = "dev-0.3.0"
val version = 3
val tag = "v0.4.0"
val version = 4
val uri = "https://github.com/aiselp/vscode-mobile/releases/download/${tag}/dist.zip"
val assetsDir = File(projectDir, "/src/main/assets/codeeditor")
val versionFile = File(assetsDir, "version.txt")
Expand All @@ -72,7 +72,11 @@ tasks.register("downloadEditor") {
return@doFirst
}
}
URL(uri).openStream().use {
val response = OkHttpClient.Builder().build().newCall(
Request.Builder().url(uri).build()
).execute()
check(response.isSuccessful){"download error response code:${response.code}"}
response.body!!.byteStream().use {
File(assetsDir, "dist.zip").outputStream().use { out->
it.copyTo(out)
}
Expand Down
4 changes: 3 additions & 1 deletion codeeditor/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<application>
<activity
android:name=".EditActivity"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
android:configChanges="orientation|screenSize|uiMode"
android:launchMode="singleInstance"
android:launchMode="singleTask"
android:taskAffinity="task.editor"
>

</activity>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.aiselp.autojs.codeeditor.dialogs

import android.app.Activity
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.aiselp.autojs.codeeditor.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class LoadDialog(activity: Activity) {
val dialog: AlertDialog = AlertDialog.Builder(
activity
)
.setTitle("加载中")
.setView(R.layout.load)
.setCancelable(false)
.create()
val textView: TextView by lazy {
dialog.findViewById(R.id.textView)!!
}

init {
}

suspend fun setContent(text: String) {
withContext(Dispatchers.Main) {
textView.text = text
}
}

fun show() {
dialog.show()
}
}
Loading

0 comments on commit 0ab8ec6

Please sign in to comment.