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,75 @@
|
||||
package dev.mygo
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.MediaStore
|
||||
import androidx.core.content.FileProvider
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
|
||||
class ImagePickerPlugin : MyGoPlugin {
|
||||
override fun call(method: String, args: JSONObject, reply: PluginReply) {
|
||||
when (method) {
|
||||
"pick" -> pick(args, reply)
|
||||
"capture" -> capture(reply)
|
||||
else -> reply.failure(PluginFailure("NoMethod", method))
|
||||
}
|
||||
}
|
||||
|
||||
private fun pick(args: JSONObject, reply: PluginReply) {
|
||||
val type = when (args.optString("kind")) {
|
||||
"videos" -> "video/*"
|
||||
"any" -> "*/*"
|
||||
else -> "image/*"
|
||||
}
|
||||
val limit = args.optInt("limit", 1)
|
||||
// The Photo Picker, where the system has it; Google Play brings it to
|
||||
// older ones.
|
||||
val photo = Build.VERSION.SDK_INT >= 33 || MediaStore.getPickImagesMaxLimit() > 0
|
||||
val i = if (photo) {
|
||||
Intent(MediaStore.ACTION_PICK_IMAGES).apply {
|
||||
if (type != "*/*") setType(type)
|
||||
if (limit != 1) putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, if (limit < 0) MediaStore.getPickImagesMaxLimit() else limit)
|
||||
}
|
||||
} else {
|
||||
Intent(Intent.ACTION_GET_CONTENT).setType(type).addCategory(Intent.CATEGORY_OPENABLE).putExtra(Intent.EXTRA_ALLOW_MULTIPLE, limit != 1)
|
||||
}
|
||||
PluginHost.startForResult(i) { code, data ->
|
||||
if (code != Activity.RESULT_OK || data == null) {
|
||||
reply.success(JSONObject().put("paths", JSONArray()))
|
||||
return@startForResult
|
||||
}
|
||||
val uris = ArrayList<Uri>()
|
||||
data.clipData?.let { c -> for (k in 0 until c.itemCount) uris.add(c.getItemAt(k).uri) }
|
||||
if (uris.isEmpty()) data.data?.let { uris.add(it) }
|
||||
Thread {
|
||||
try {
|
||||
val dir = File(PluginHost.context.cacheDir, "picked/${System.nanoTime()}").apply { mkdirs() }
|
||||
val out = JSONArray()
|
||||
for (u in uris) out.put(HostImpl.copyToFile(u, dir).absolutePath)
|
||||
reply.success(JSONObject().put("paths", out))
|
||||
} catch (e: Exception) {
|
||||
reply.failure(PluginFailure("Failed", e.toString()))
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
}
|
||||
|
||||
private fun capture(reply: PluginReply) {
|
||||
val ctx = PluginHost.context
|
||||
val dir = File(ctx.cacheDir, "camera").apply { mkdirs() }
|
||||
val f = File(dir, "${System.nanoTime()}.jpg")
|
||||
val uri = FileProvider.getUriForFile(ctx, ctx.packageName + ".mygo.files", f)
|
||||
val i = Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, uri).addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
if (i.resolveActivity(ctx.packageManager) == null) {
|
||||
reply.failure(PluginFailure("NoCamera", "the device has no camera app"))
|
||||
return
|
||||
}
|
||||
PluginHost.startForResult(i) { code, _ ->
|
||||
reply.success(JSONObject().put("path", if (code == Activity.RESULT_OK && f.length() > 0) f.absolutePath else ""))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// Package imagepicker lets the user choose photos and videos from their
|
||||
// library, or take one with the camera, on phones and tablets: the system's
|
||||
// own pickers (PHPickerViewController and UIImagePickerController on iOS, the
|
||||
// Photo Picker and the camera app on Android), which ask the user for no
|
||||
// permission to read the library. Elsewhere its functions report
|
||||
// errors.ErrUnsupported.
|
||||
package imagepicker
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
)
|
||||
|
||||
const pluginID = "imagepicker"
|
||||
|
||||
// Kind is what a picker shows.
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
Images Kind = "images"
|
||||
Videos Kind = "videos"
|
||||
// Any shows both.
|
||||
Any Kind = "any"
|
||||
)
|
||||
|
||||
// Options are what Pick lets the user choose.
|
||||
type Options struct {
|
||||
// Kind is what it shows: Images by default.
|
||||
Kind Kind
|
||||
// Limit is how many the user may choose: 1 by default, 0 meaning 1, and
|
||||
// negative as many as they like.
|
||||
Limit int
|
||||
}
|
||||
|
||||
// Pick shows the system's picker of the user's library, and returns the paths
|
||||
// of copies of what they chose in the app's temporary directory: none when
|
||||
// they went back. Call it off the main goroutine, as it waits for the user.
|
||||
func Pick(ctx context.Context, o Options) ([]string, error) {
|
||||
if o.Kind == "" {
|
||||
o.Kind = Images
|
||||
}
|
||||
if o.Limit == 0 {
|
||||
o.Limit = 1
|
||||
}
|
||||
var r struct {
|
||||
Paths []string `json:"paths"`
|
||||
}
|
||||
err := mygo.Native(pluginID).Call(ctx, "pick", map[string]any{"kind": o.Kind, "limit": o.Limit}, &r)
|
||||
return r.Paths, err
|
||||
}
|
||||
|
||||
// Capture shows the camera, for a photo, and returns the path of a JPEG of it
|
||||
// in the app's temporary directory, "" when the user went back.
|
||||
func Capture(ctx context.Context) (string, error) {
|
||||
var r struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
err := mygo.Native(pluginID).Call(ctx, "capture", nil, &r)
|
||||
return r.Path, err
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import PhotosUI
|
||||
import UIKit
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
final class ImagePickerPlugin: NSObject, MyGoPlugin, PHPickerViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
||||
private var picking: PluginReply?
|
||||
private var capturing: PluginReply?
|
||||
|
||||
func call(_ method: String, _ args: [String: Any], _ reply: @escaping PluginReply) {
|
||||
switch method {
|
||||
case "pick":
|
||||
picking = reply
|
||||
var cfg = PHPickerConfiguration()
|
||||
switch args["kind"] as? String {
|
||||
case "videos": cfg.filter = .videos
|
||||
case "any": cfg.filter = .any(of: [.images, .videos])
|
||||
default: cfg.filter = .images
|
||||
}
|
||||
let limit = args["limit"] as? Int ?? 1
|
||||
cfg.selectionLimit = limit < 0 ? 0 : limit
|
||||
let vc = PHPickerViewController(configuration: cfg)
|
||||
vc.delegate = self
|
||||
PluginHost.present(vc)
|
||||
case "capture":
|
||||
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
|
||||
reply(.failure(PluginFailure("NoCamera", "the device has no camera")))
|
||||
return
|
||||
}
|
||||
capturing = reply
|
||||
let vc = UIImagePickerController()
|
||||
vc.sourceType = .camera
|
||||
vc.delegate = self
|
||||
PluginHost.present(vc)
|
||||
default:
|
||||
reply(.failure(PluginFailure("NoMethod", method)))
|
||||
}
|
||||
}
|
||||
|
||||
func picker(_ vc: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
||||
vc.dismiss(animated: true)
|
||||
guard let reply = picking else { return }
|
||||
picking = nil
|
||||
let group = DispatchGroup()
|
||||
let dir = NSTemporaryDirectory() + UUID().uuidString
|
||||
try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)
|
||||
var paths = [String?](repeating: nil, count: results.count)
|
||||
let lock = NSLock()
|
||||
for (i, r) in results.enumerated() {
|
||||
let p = r.itemProvider
|
||||
guard let type = p.registeredTypeIdentifiers.first else { continue }
|
||||
group.enter()
|
||||
p.loadFileRepresentation(forTypeIdentifier: type) { url, _ in
|
||||
defer { group.leave() }
|
||||
guard let url else { return }
|
||||
let to = URL(fileURLWithPath: dir).appendingPathComponent("\(i)-" + url.lastPathComponent)
|
||||
if (try? FileManager.default.copyItem(at: url, to: to)) != nil {
|
||||
lock.lock()
|
||||
paths[i] = to.path
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
group.notify(queue: .main) { reply(.success(["paths": paths.compactMap { $0 }])) }
|
||||
}
|
||||
|
||||
func imagePickerController(_ vc: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
||||
vc.dismiss(animated: true)
|
||||
guard let reply = capturing else { return }
|
||||
capturing = nil
|
||||
guard let image = info[.originalImage] as? UIImage, let jpeg = image.jpegData(compressionQuality: 0.9) else {
|
||||
reply(.failure(PluginFailure("Failed", "no photo")))
|
||||
return
|
||||
}
|
||||
let path = NSTemporaryDirectory() + UUID().uuidString + ".jpg"
|
||||
do {
|
||||
try jpeg.write(to: URL(fileURLWithPath: path))
|
||||
reply(.success(["path": path]))
|
||||
} catch {
|
||||
reply(.failure(PluginFailure("Failed", error.localizedDescription)))
|
||||
}
|
||||
}
|
||||
|
||||
func imagePickerControllerDidCancel(_ vc: UIImagePickerController) {
|
||||
vc.dismiss(animated: true)
|
||||
capturing?(.success(["path": ""]))
|
||||
capturing = nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"mobile": {
|
||||
"id": "imagepicker",
|
||||
"ios": {
|
||||
"sources": ["ios/*.swift"],
|
||||
"class": "ImagePickerPlugin",
|
||||
"frameworks": ["PhotosUI", "Photos"],
|
||||
"infoPlist": {"NSCameraUsageDescription": "This app takes photos with the camera."}
|
||||
},
|
||||
"android": {
|
||||
"sources": ["android/*.kt"],
|
||||
"class": "ImagePickerPlugin"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user