feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package dev.mygo
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.location.Location
|
||||
import android.location.LocationListener
|
||||
import android.location.LocationManager
|
||||
import android.os.Build
|
||||
import android.os.Looper
|
||||
import org.json.JSONObject
|
||||
|
||||
class LocationPlugin : MyGoPlugin {
|
||||
private val manager get() = PluginHost.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
private val perms = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||
private var listener: LocationListener? = null
|
||||
|
||||
private fun encode(l: Location) = JSONObject()
|
||||
.put("latitude", l.latitude).put("longitude", l.longitude)
|
||||
.put("altitude", if (l.hasAltitude()) l.altitude else -1.0)
|
||||
.put("speed", if (l.hasSpeed()) l.speed.toDouble() else -1.0)
|
||||
.put("heading", if (l.hasBearing()) l.bearing.toDouble() else -1.0)
|
||||
.put("accuracy", if (l.hasAccuracy()) l.accuracy.toDouble() else -1.0)
|
||||
.put("time", l.time)
|
||||
|
||||
private fun provider(): String? {
|
||||
val providers = manager.getProviders(true)
|
||||
return when {
|
||||
LocationManager.FUSED_PROVIDER in providers -> LocationManager.FUSED_PROVIDER
|
||||
LocationManager.GPS_PROVIDER in providers -> LocationManager.GPS_PROVIDER
|
||||
LocationManager.NETWORK_PROVIDER in providers -> LocationManager.NETWORK_PROVIDER
|
||||
else -> providers.firstOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
override fun call(method: String, args: JSONObject, reply: PluginReply) {
|
||||
when (method) {
|
||||
"requestPermission" -> PluginHost.requestPermissions(perms) { reply.success(JSONObject().put("granted", it)) }
|
||||
"current" -> PluginHost.requestPermissions(perms) { granted ->
|
||||
if (!granted) {
|
||||
reply.failure(PluginFailure("Denied", "the app may not know where the device is"))
|
||||
return@requestPermissions
|
||||
}
|
||||
val p = provider()
|
||||
if (p == null) {
|
||||
reply.failure(PluginFailure("Failed", "the device has location turned off"))
|
||||
return@requestPermissions
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
manager.getCurrentLocation(p, null, PluginHost.context.mainExecutor) { l ->
|
||||
if (l != null) reply.success(encode(l)) else reply.failure(PluginFailure("Failed", "no fix"))
|
||||
}
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
manager.requestSingleUpdate(p, { l: Location -> reply.success(encode(l)) }, Looper.getMainLooper())
|
||||
}
|
||||
}
|
||||
"start" -> PluginHost.requestPermissions(perms) { granted ->
|
||||
val p = provider()
|
||||
if (!granted || p == null) {
|
||||
reply.failure(PluginFailure("Denied", "the app may not know where the device is"))
|
||||
return@requestPermissions
|
||||
}
|
||||
listener?.let { manager.removeUpdates(it) }
|
||||
val l = LocationListener { loc -> PluginHost.emit("location", "fix", encode(loc)) }
|
||||
listener = l
|
||||
manager.requestLocationUpdates(p, 1000L, 1f, l, Looper.getMainLooper())
|
||||
reply.success(null)
|
||||
}
|
||||
"stop" -> {
|
||||
listener?.let { manager.removeUpdates(it) }
|
||||
listener = null
|
||||
reply.success(null)
|
||||
}
|
||||
else -> reply.failure(PluginFailure("NoMethod", method))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import CoreLocation
|
||||
import Foundation
|
||||
|
||||
final class LocationPlugin: NSObject, MyGoPlugin, CLLocationManagerDelegate {
|
||||
private let manager = CLLocationManager()
|
||||
private var waitingFix: [PluginReply] = []
|
||||
private var waitingAuth: [PluginReply] = []
|
||||
private var watching = false
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
manager.delegate = self
|
||||
manager.desiredAccuracy = kCLLocationAccuracyBest
|
||||
}
|
||||
|
||||
private var granted: Bool {
|
||||
let s = manager.authorizationStatus
|
||||
return s == .authorizedWhenInUse || s == .authorizedAlways
|
||||
}
|
||||
|
||||
func call(_ method: String, _ args: [String: Any], _ reply: @escaping PluginReply) {
|
||||
switch method {
|
||||
case "requestPermission":
|
||||
if manager.authorizationStatus == .notDetermined {
|
||||
waitingAuth.append(reply)
|
||||
manager.requestWhenInUseAuthorization()
|
||||
} else {
|
||||
reply(.success(["granted": granted]))
|
||||
}
|
||||
case "current":
|
||||
waitingFix.append(reply)
|
||||
if manager.authorizationStatus == .notDetermined {
|
||||
manager.requestWhenInUseAuthorization()
|
||||
} else if !granted {
|
||||
failFixes(PluginFailure("Denied", "the app may not know where the device is"))
|
||||
} else {
|
||||
manager.requestLocation()
|
||||
}
|
||||
case "start":
|
||||
watching = true
|
||||
if manager.authorizationStatus == .notDetermined { manager.requestWhenInUseAuthorization() }
|
||||
manager.startUpdatingLocation()
|
||||
reply(.success(nil))
|
||||
case "stop":
|
||||
watching = false
|
||||
manager.stopUpdatingLocation()
|
||||
reply(.success(nil))
|
||||
default:
|
||||
reply(.failure(PluginFailure("NoMethod", method)))
|
||||
}
|
||||
}
|
||||
|
||||
private func failFixes(_ f: PluginFailure) {
|
||||
let list = waitingFix
|
||||
waitingFix = []
|
||||
for r in list { r(.failure(f)) }
|
||||
}
|
||||
|
||||
func locationManagerDidChangeAuthorization(_ m: CLLocationManager) {
|
||||
guard m.authorizationStatus != .notDetermined else { return }
|
||||
let auth = waitingAuth
|
||||
waitingAuth = []
|
||||
for r in auth { r(.success(["granted": granted])) }
|
||||
if !waitingFix.isEmpty {
|
||||
if granted {
|
||||
m.requestLocation()
|
||||
} else {
|
||||
failFixes(PluginFailure("Denied", "the app may not know where the device is"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func encode(_ l: CLLocation) -> [String: Any] {
|
||||
["latitude": l.coordinate.latitude, "longitude": l.coordinate.longitude, "altitude": l.verticalAccuracy >= 0 ? l.altitude : -1,
|
||||
"speed": l.speed, "heading": l.course, "accuracy": l.horizontalAccuracy, "time": Int64(l.timestamp.timeIntervalSince1970 * 1000)]
|
||||
}
|
||||
|
||||
func locationManager(_ m: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||
guard let l = locations.last else { return }
|
||||
let fix = encode(l)
|
||||
let list = waitingFix
|
||||
waitingFix = []
|
||||
for r in list { r(.success(fix)) }
|
||||
if watching { PluginHost.emit("location", "fix", fix) }
|
||||
}
|
||||
|
||||
func locationManager(_ m: CLLocationManager, didFailWithError error: Error) {
|
||||
failFixes(PluginFailure("Failed", error.localizedDescription))
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
// Package location tells where the device is, on phones and tablets: Core
|
||||
// Location on iOS, and LocationManager on Android, without Google's services.
|
||||
// Importing it adds the permissions it needs to the app, with a usage
|
||||
// description on iOS that ios.infoPlist in the app's configuration replaces:
|
||||
//
|
||||
// "NSLocationWhenInUseUsageDescription": "Shows nearby stores."
|
||||
//
|
||||
// Elsewhere its functions report errors.ErrUnsupported.
|
||||
package location
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
)
|
||||
|
||||
const pluginID = "location"
|
||||
|
||||
// Fix is a place the device was at.
|
||||
type Fix struct {
|
||||
// Latitude and Longitude are in degrees.
|
||||
Latitude, Longitude float64
|
||||
// Altitude is in meters above sea level, Speed in meters a second, and
|
||||
// Heading in degrees clockwise from north; negative where not known.
|
||||
Altitude, Speed, Heading float64
|
||||
// Accuracy is how far off it may be, in meters.
|
||||
Accuracy float64
|
||||
// Time is when it was measured.
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
type wireFix struct {
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Altitude float64 `json:"altitude"`
|
||||
Speed float64 `json:"speed"`
|
||||
Heading float64 `json:"heading"`
|
||||
Accuracy float64 `json:"accuracy"`
|
||||
TimeMS int64 `json:"time"`
|
||||
}
|
||||
|
||||
func (w wireFix) fix() Fix {
|
||||
return Fix{w.Latitude, w.Longitude, w.Altitude, w.Speed, w.Heading, w.Accuracy, time.UnixMilli(w.TimeMS)}
|
||||
}
|
||||
|
||||
// RequestPermission asks the user to let the app know where the device is,
|
||||
// the first time, and reports whether it may. Call it off the main goroutine,
|
||||
// as it waits for the user.
|
||||
func RequestPermission(ctx context.Context) (bool, error) {
|
||||
var r struct {
|
||||
Granted bool `json:"granted"`
|
||||
}
|
||||
err := mygo.Native(pluginID).Call(ctx, "requestPermission", nil, &r)
|
||||
return r.Granted, err
|
||||
}
|
||||
|
||||
// Current finds where the device is now, asking for permission if it must.
|
||||
func Current(ctx context.Context) (Fix, error) {
|
||||
var w wireFix
|
||||
if err := mygo.Native(pluginID).Call(ctx, "current", nil, &w); err != nil {
|
||||
return Fix{}, err
|
||||
}
|
||||
return w.fix(), nil
|
||||
}
|
||||
|
||||
// Watch calls fn on the main thread as the device moves, until stop is called.
|
||||
func Watch(ctx context.Context, fn func(Fix)) (stop func(), err error) {
|
||||
p := mygo.Native(pluginID)
|
||||
off := p.On("fix", func(data []byte) {
|
||||
var w wireFix
|
||||
if json.Unmarshal(data, &w) == nil {
|
||||
fn(w.fix())
|
||||
}
|
||||
})
|
||||
if err := p.Call(ctx, "start", nil, nil); err != nil {
|
||||
off()
|
||||
return nil, err
|
||||
}
|
||||
return func() {
|
||||
off()
|
||||
go p.Call(context.Background(), "stop", nil, nil)
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"mobile": {
|
||||
"id": "location",
|
||||
"ios": {
|
||||
"sources": ["ios/*.swift"],
|
||||
"class": "LocationPlugin",
|
||||
"frameworks": ["CoreLocation"],
|
||||
"infoPlist": {"NSLocationWhenInUseUsageDescription": "This app shows where you are."}
|
||||
},
|
||||
"android": {
|
||||
"sources": ["android/*.kt"],
|
||||
"class": "LocationPlugin",
|
||||
"permissions": ["ACCESS_FINE_LOCATION", "ACCESS_COARSE_LOCATION"]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user