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
+119
@@ -0,0 +1,119 @@
|
||||
package dev.mygo
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.osmdroid.config.Configuration
|
||||
import org.osmdroid.events.MapEventsReceiver
|
||||
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase
|
||||
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
|
||||
import org.osmdroid.util.GeoPoint
|
||||
import org.osmdroid.util.MapTileIndex
|
||||
import org.osmdroid.views.CustomZoomButtonsController
|
||||
import org.osmdroid.views.MapView
|
||||
import org.osmdroid.views.overlay.MapEventsOverlay
|
||||
import org.osmdroid.views.overlay.Marker
|
||||
import java.io.File
|
||||
|
||||
/** A map's tiles from a server of the app's. */
|
||||
private class Tiles(private val template: String) : OnlineTileSourceBase("mygo", 0, 22, 256, "", arrayOf(template)) {
|
||||
override fun getTileURLString(index: Long): String = template
|
||||
.replace("{z}", MapTileIndex.getZoom(index).toString())
|
||||
.replace("{x}", MapTileIndex.getX(index).toString())
|
||||
.replace("{y}", MapTileIndex.getY(index).toString())
|
||||
}
|
||||
|
||||
class MapsPlugin : MyGoPlugin {
|
||||
private class Holder(val id: Long, val map: MapView) {
|
||||
val pins = HashMap<String, Marker>()
|
||||
}
|
||||
|
||||
private val maps = HashMap<Long, Holder>()
|
||||
|
||||
init {
|
||||
PluginHost.registerView("map") { context, id, args ->
|
||||
val conf = Configuration.getInstance()
|
||||
conf.userAgentValue = context.packageName
|
||||
conf.osmdroidBasePath = File(context.cacheDir, "osmdroid")
|
||||
conf.osmdroidTileCache = File(context.cacheDir, "osmdroid/tiles")
|
||||
val map = MapView(context)
|
||||
val template = args.optString("tileURL")
|
||||
map.setTileSource(if (template.isEmpty()) TileSourceFactory.MAPNIK else Tiles(template))
|
||||
map.setMultiTouchControls(true)
|
||||
map.isTilesScaledToDpi = true // legible on a dense screen
|
||||
map.zoomController.setVisibility(CustomZoomButtonsController.Visibility.NEVER)
|
||||
map.controller.setZoom(args.optDouble("zoom", 2.0))
|
||||
map.controller.setCenter(GeoPoint(args.optDouble("lat"), args.optDouble("lng")))
|
||||
val h = Holder(id, map)
|
||||
maps[id] = h
|
||||
map.overlays.add(MapEventsOverlay(object : MapEventsReceiver {
|
||||
override fun singleTapConfirmedHelper(p: GeoPoint): Boolean {
|
||||
PluginHost.emit("maps", "tap", mapOf("id" to id, "lat" to p.latitude, "lng" to p.longitude))
|
||||
return true
|
||||
}
|
||||
override fun longPressHelper(p: GeoPoint) = false
|
||||
}))
|
||||
map.onResume()
|
||||
map
|
||||
}
|
||||
}
|
||||
|
||||
override fun call(method: String, args: JSONObject, reply: PluginReply) {
|
||||
val id = args.optLong("id")
|
||||
val h = maps[id]
|
||||
if (h == null) {
|
||||
reply.failure(PluginFailure("NoMap", "no map $id"))
|
||||
return
|
||||
}
|
||||
val at = GeoPoint(args.optDouble("lat"), args.optDouble("lng"))
|
||||
when (method) {
|
||||
"setCamera" -> {
|
||||
if (args.optBoolean("animated")) {
|
||||
h.map.controller.animateTo(at, args.optDouble("zoom", h.map.zoomLevelDouble), null)
|
||||
} else {
|
||||
h.map.controller.setZoom(args.optDouble("zoom", h.map.zoomLevelDouble))
|
||||
h.map.controller.setCenter(at)
|
||||
}
|
||||
reply.success(null)
|
||||
}
|
||||
"camera" -> {
|
||||
val c = h.map.mapCenter
|
||||
reply.success(mapOf("Lat" to c.latitude, "Lng" to c.longitude, "Zoom" to h.map.zoomLevelDouble))
|
||||
}
|
||||
"addMarker" -> {
|
||||
val name = args.optString("marker")
|
||||
h.pins.remove(name)?.let {
|
||||
it.closeInfoWindow()
|
||||
h.map.overlays.remove(it)
|
||||
}
|
||||
val m = Marker(h.map)
|
||||
m.position = at
|
||||
m.title = args.optString("title").ifEmpty { null }
|
||||
m.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM)
|
||||
m.setOnMarkerClickListener { marker, map ->
|
||||
PluginHost.emit("maps", "markerTap", mapOf("id" to id, "marker" to name))
|
||||
if (marker.title != null) marker.showInfoWindow()
|
||||
map.controller.animateTo(marker.position)
|
||||
true
|
||||
}
|
||||
h.pins[name] = m
|
||||
h.map.overlays.add(m)
|
||||
h.map.invalidate()
|
||||
reply.success(null)
|
||||
}
|
||||
"removeMarker" -> {
|
||||
h.pins.remove(args.optString("marker"))?.let {
|
||||
it.closeInfoWindow()
|
||||
h.map.overlays.remove(it)
|
||||
}
|
||||
h.map.invalidate()
|
||||
reply.success(null)
|
||||
}
|
||||
"dispose" -> {
|
||||
maps.remove(id)
|
||||
h.map.onPause()
|
||||
h.map.onDetach()
|
||||
reply.success(null)
|
||||
}
|
||||
else -> reply.failure(PluginFailure("UnknownMethod", method))
|
||||
}
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
import MapKit
|
||||
import UIKit
|
||||
|
||||
/// A pin, which knows its ID.
|
||||
private final class Pin: MKPointAnnotation {
|
||||
var id = ""
|
||||
}
|
||||
|
||||
/// Zoom as web maps have it, from MapKit's span of longitude.
|
||||
private func span(zoom: Double, height: CGFloat, width: CGFloat) -> MKCoordinateSpan {
|
||||
let lng = 360 / pow(2, zoom)
|
||||
let ratio = width > 0 ? Double(height / width) : 1
|
||||
return MKCoordinateSpan(latitudeDelta: min(lng * ratio, 180), longitudeDelta: min(lng, 360))
|
||||
}
|
||||
|
||||
private final class MapHolder: NSObject, MKMapViewDelegate, UIGestureRecognizerDelegate {
|
||||
let id: Int
|
||||
let map = MKMapView()
|
||||
var pins: [String: Pin] = [:]
|
||||
/// What to show once the view has a size.
|
||||
var pending: (CLLocationCoordinate2D, Double)?
|
||||
/// The pin selected by the tap being handled, which has told already.
|
||||
weak var justSelected: Pin?
|
||||
|
||||
init(id: Int) {
|
||||
self.id = id
|
||||
super.init()
|
||||
map.delegate = self
|
||||
// A tap once a double tap, which zooms, is ruled out.
|
||||
let twice = UITapGestureRecognizer()
|
||||
twice.numberOfTapsRequired = 2
|
||||
twice.delegate = self
|
||||
map.addGestureRecognizer(twice)
|
||||
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
|
||||
tap.delegate = self
|
||||
tap.require(toFail: twice)
|
||||
map.addGestureRecognizer(tap)
|
||||
}
|
||||
|
||||
func show(_ place: CLLocationCoordinate2D, zoom: Double, animated: Bool) {
|
||||
// MapKit raises on what's off its map.
|
||||
let at = CLLocationCoordinate2D(latitude: max(-85, min(85, place.latitude)), longitude: max(-180, min(180, place.longitude)))
|
||||
guard map.bounds.width > 0 else {
|
||||
pending = (at, zoom)
|
||||
return
|
||||
}
|
||||
map.setRegion(MKCoordinateRegion(center: at, span: span(zoom: zoom, height: map.bounds.height, width: map.bounds.width)), animated: animated)
|
||||
}
|
||||
|
||||
func gestureRecognizer(_ g: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer) -> Bool { true }
|
||||
|
||||
@objc func tapped(_ g: UITapGestureRecognizer) {
|
||||
let p = g.location(in: map)
|
||||
// A pin's own tap, not the map's: mapView(_:didSelect:) tells of its first,
|
||||
// and this of those while it stays selected.
|
||||
var v = map.hitTest(p, with: nil)
|
||||
while let view = v, !(view is MKAnnotationView) { v = view.superview }
|
||||
if let pin = (v as? MKAnnotationView)?.annotation as? Pin {
|
||||
if justSelected !== pin { PluginHost.emit("maps", "markerTap", ["id": id, "marker": pin.id]) }
|
||||
justSelected = nil
|
||||
return
|
||||
}
|
||||
justSelected = nil
|
||||
let at = map.convert(p, toCoordinateFrom: map)
|
||||
PluginHost.emit("maps", "tap", ["id": id, "lat": at.latitude, "lng": at.longitude])
|
||||
}
|
||||
|
||||
func mapViewDidChangeVisibleRegion(_ m: MKMapView) {
|
||||
if let (at, zoom) = pending, m.bounds.width > 0 {
|
||||
pending = nil
|
||||
show(at, zoom: zoom, animated: false)
|
||||
}
|
||||
}
|
||||
|
||||
func mapView(_ m: MKMapView, didSelect annotation: MKAnnotation) {
|
||||
if let pin = annotation as? Pin {
|
||||
justSelected = pin
|
||||
PluginHost.emit("maps", "markerTap", ["id": id, "marker": pin.id])
|
||||
}
|
||||
}
|
||||
|
||||
func mapView(_ m: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
|
||||
guard annotation is Pin else { return nil }
|
||||
let v = m.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKMarkerAnnotationView
|
||||
?? MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
|
||||
v.annotation = annotation
|
||||
v.canShowCallout = annotation.title != nil
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
final class MapsPlugin: MyGoPlugin {
|
||||
private var maps: [Int: MapHolder] = [:]
|
||||
|
||||
init() {
|
||||
PluginHost.registerView("map") { [weak self] id, args in
|
||||
let h = MapHolder(id: id)
|
||||
self?.maps[id] = h
|
||||
h.show(CLLocationCoordinate2D(latitude: args["lat"] as? Double ?? 0, longitude: args["lng"] as? Double ?? 0),
|
||||
zoom: args["zoom"] as? Double ?? 2, animated: false)
|
||||
return h.map
|
||||
}
|
||||
}
|
||||
|
||||
func call(_ method: String, _ args: [String: Any], _ reply: @escaping PluginReply) {
|
||||
guard let id = args["id"] as? Int, let h = maps[id] else {
|
||||
reply(.failure(PluginFailure("NoMap", "no map \(args["id"] ?? "")")))
|
||||
return
|
||||
}
|
||||
let at = CLLocationCoordinate2D(latitude: args["lat"] as? Double ?? 0, longitude: args["lng"] as? Double ?? 0)
|
||||
switch method {
|
||||
case "setCamera":
|
||||
h.show(at, zoom: args["zoom"] as? Double ?? 2, animated: args["animated"] as? Bool ?? false)
|
||||
reply(.success(nil))
|
||||
case "camera":
|
||||
let r = h.map.region
|
||||
let zoom = r.span.longitudeDelta > 0 ? log2(360 / r.span.longitudeDelta) : 0
|
||||
reply(.success(["Lat": r.center.latitude, "Lng": r.center.longitude, "Zoom": zoom]))
|
||||
case "addMarker":
|
||||
let name = args["marker"] as? String ?? ""
|
||||
if let old = h.pins[name] { h.map.removeAnnotation(old) }
|
||||
let pin = Pin()
|
||||
pin.id = name
|
||||
pin.coordinate = at
|
||||
if let t = args["title"] as? String, !t.isEmpty { pin.title = t }
|
||||
h.pins[name] = pin
|
||||
h.map.addAnnotation(pin)
|
||||
reply(.success(nil))
|
||||
case "removeMarker":
|
||||
let name = args["marker"] as? String ?? ""
|
||||
if let old = h.pins.removeValue(forKey: name) { h.map.removeAnnotation(old) }
|
||||
reply(.success(nil))
|
||||
case "dispose":
|
||||
maps[id] = nil
|
||||
reply(.success(nil))
|
||||
default:
|
||||
reply(.failure(PluginFailure("UnknownMethod", method)))
|
||||
}
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
// Package maps shows a map in a window's native UI: MapKit on iOS, and on Android
|
||||
// osmdroid, with OpenStreetMap's tiles by default, which needs no account or key.
|
||||
//
|
||||
// m, err := maps.New(win, maps.Options{Center: maps.LatLng{Lat: 48.86, Lng: 2.35}, Zoom: 12})
|
||||
// ...
|
||||
// ui.WebView(c, m).Fill() // in the view; what it draws after shows over the map
|
||||
// m.OnTap(func(at maps.LatLng) { ... })
|
||||
// m.AddMarker(ctx, maps.Marker{ID: "home", At: at, Title: "Home"})
|
||||
//
|
||||
// A finger pans, pinches and taps it. OpenStreetMap's tile servers are for light
|
||||
// use: an app with many users gives Options.TileURL a server of its own (Android;
|
||||
// iOS shows Apple's maps). On desktops and in browsers New reports
|
||||
// errors.ErrUnsupported.
|
||||
package maps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/internal/platform"
|
||||
"github.com/egoist/mygo/internal/surface"
|
||||
)
|
||||
|
||||
const pluginID = "maps"
|
||||
|
||||
// LatLng is a place, in degrees.
|
||||
type LatLng struct {
|
||||
Lat float64 `json:"lat"`
|
||||
Lng float64 `json:"lng"`
|
||||
}
|
||||
|
||||
// Options say what a map shows first.
|
||||
type Options struct {
|
||||
Center LatLng
|
||||
// Zoom is as on web maps: 0 shows the whole world, 18 a street; 2 by default.
|
||||
Zoom float64
|
||||
// TileURL is where Android's map gets its tiles, with {z}, {x} and {y}; by
|
||||
// default OpenStreetMap's.
|
||||
TileURL string
|
||||
}
|
||||
|
||||
// Marker is a pin on a map.
|
||||
type Marker struct {
|
||||
// ID tells it apart: OnMarkerTap reports it, and adding it again moves it.
|
||||
ID string `json:"id"`
|
||||
At LatLng `json:"at"`
|
||||
// Title shows as it's tapped.
|
||||
Title string `json:"title,omitempty"`
|
||||
}
|
||||
|
||||
// Map is a map in a window. Show it with ui.WebView.
|
||||
type Map struct {
|
||||
view *mygo.PluginView
|
||||
offs []func()
|
||||
}
|
||||
|
||||
// New makes a map in a window showing native UI.
|
||||
func New(w *mygo.Window, o Options) (*Map, error) {
|
||||
zoom := o.Zoom
|
||||
if zoom == 0 {
|
||||
zoom = 2
|
||||
}
|
||||
v, err := mygo.NewPluginView(w, "map", map[string]any{"lat": o.Center.Lat, "lng": o.Center.Lng, "zoom": zoom, "tileURL": o.TileURL})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Map{view: v}, nil
|
||||
}
|
||||
|
||||
// SurfaceWebView gives package ui the view to show.
|
||||
func (m *Map) SurfaceWebView(conn *surface.Conn) platform.WebView {
|
||||
return m.view.SurfaceWebView(conn)
|
||||
}
|
||||
|
||||
func (m *Map) call(ctx context.Context, method string, args map[string]any, reply any) error {
|
||||
args["id"] = m.view.ID
|
||||
return mygo.Native(pluginID).Call(ctx, method, args, reply)
|
||||
}
|
||||
|
||||
// SetCamera moves the map to a place, gliding there if animated.
|
||||
func (m *Map) SetCamera(ctx context.Context, center LatLng, zoom float64, animated bool) error {
|
||||
return m.call(ctx, "setCamera", map[string]any{"lat": center.Lat, "lng": center.Lng, "zoom": zoom, "animated": animated}, nil)
|
||||
}
|
||||
|
||||
// Camera returns what the map shows now, as the user panned it.
|
||||
func (m *Map) Camera(ctx context.Context) (center LatLng, zoom float64, err error) {
|
||||
var r struct {
|
||||
Lat, Lng, Zoom float64
|
||||
}
|
||||
err = m.call(ctx, "camera", map[string]any{}, &r)
|
||||
return LatLng{r.Lat, r.Lng}, r.Zoom, err
|
||||
}
|
||||
|
||||
// AddMarker puts a pin on the map, in place of one of its ID.
|
||||
func (m *Map) AddMarker(ctx context.Context, marker Marker) error {
|
||||
return m.call(ctx, "addMarker", map[string]any{"marker": marker.ID, "lat": marker.At.Lat, "lng": marker.At.Lng, "title": marker.Title}, nil)
|
||||
}
|
||||
|
||||
// RemoveMarker takes a pin off the map.
|
||||
func (m *Map) RemoveMarker(ctx context.Context, id string) error {
|
||||
return m.call(ctx, "removeMarker", map[string]any{"marker": id}, nil)
|
||||
}
|
||||
|
||||
// OnTap calls fn on the main thread as a finger taps the map, not one of its pins.
|
||||
func (m *Map) OnTap(fn func(at LatLng)) {
|
||||
id := m.view.ID
|
||||
m.offs = append(m.offs, mygo.Native(pluginID).On("tap", func(data []byte) {
|
||||
var e struct {
|
||||
ID int
|
||||
Lat, Lng float64
|
||||
}
|
||||
if json.Unmarshal(data, &e) == nil && e.ID == id {
|
||||
fn(LatLng{e.Lat, e.Lng})
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
// OnMarkerTap calls fn on the main thread as a finger taps a pin.
|
||||
func (m *Map) OnMarkerTap(fn func(id string)) {
|
||||
view := m.view.ID
|
||||
m.offs = append(m.offs, mygo.Native(pluginID).On("markerTap", func(data []byte) {
|
||||
var e struct {
|
||||
ID int
|
||||
Marker string
|
||||
}
|
||||
if json.Unmarshal(data, &e) == nil && e.ID == view {
|
||||
fn(e.Marker)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
// Destroy takes the map away, which closing its window doesn't: call it before.
|
||||
func (m *Map) Destroy() {
|
||||
for _, off := range m.offs {
|
||||
off()
|
||||
}
|
||||
m.offs = nil
|
||||
go mygo.Native(pluginID).Call(context.Background(), "dispose", map[string]any{"id": m.view.ID}, nil)
|
||||
m.view.Destroy()
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"mobile": {
|
||||
"id": "maps",
|
||||
"ios": {
|
||||
"sources": ["ios/*.swift"],
|
||||
"class": "MapsPlugin",
|
||||
"frameworks": ["MapKit"]
|
||||
},
|
||||
"android": {
|
||||
"sources": ["android/*.kt"],
|
||||
"class": "MapsPlugin",
|
||||
"permissions": ["INTERNET", "ACCESS_NETWORK_STATE"],
|
||||
"dependencies": ["org.osmdroid:osmdroid-android:6.1.20"]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user