Files
tether/ios/TetherApp/Sources/RoomView.swift
Patrick Ecord cb0df2af81 ios: Nearby-devices picker (AirDrop-style), matching the Mac
Port the nearby picker to iOS: the store polls engine.nearby() while connected
and RoomView shows discovered devices as tap-to-join chips with platform icons,
same as MacTetherApp. pair() joins the device's room.

Dormant on iOS until the multicast entitlement lands (Rust mdns-sd uses a raw
multicast socket iOS blocks without it) — the UI is ready; it'll populate once
the entitlement is granted. No effect when the list is empty.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 02:39:27 -05:00

142 lines
5.1 KiB
Swift

import SwiftUI
struct RoomView: View {
@Bindable var store: TetherStore
@State private var draft = ""
var body: some View {
NavigationStack {
VStack(spacing: 0) {
connectionBar
Divider()
if store.connected && !store.nearby.isEmpty {
nearbyStrip
Divider()
}
feedList
Divider()
composer
}
.navigationTitle("tether")
.navigationBarTitleDisplayMode(.inline)
.task { store.connect() } // auto-connect on launch (defaults are production-ready)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Circle()
.fill(store.connected ? .green : .secondary)
.frame(width: 10, height: 10)
}
}
}
}
private var connectionBar: some View {
VStack(spacing: 8) {
TextField("https://tether.pecord.io", text: $store.serverURL)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.URL)
HStack {
TextField("room id (8 hex)", text: $store.room)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
Button(store.connected ? "Disconnect" : "Connect") {
store.connected ? store.disconnect() : store.connect()
}
.buttonStyle(.borderedProminent)
}
if let err = store.lastError {
Text(err).font(.caption).foregroundStyle(.red)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.textFieldStyle(.roundedBorder)
.padding()
}
// MARK: - Nearby devices (AirDrop-style picker)
private var nearbyStrip: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 8) {
Label("Nearby", systemImage: "dot.radiowaves.left.and.right")
.font(.caption).foregroundStyle(.secondary)
ForEach(store.nearby, id: \.id) { peer in
let here = peer.room == store.room
Button {
store.pair(with: peer)
} label: {
HStack(spacing: 6) {
Image(systemName: sourceIcon(peer.source))
VStack(alignment: .leading, spacing: 1) {
Text(peer.name).font(.caption).lineLimit(1)
Text(here ? "in your room" : "tap to join")
.font(.caption2)
.foregroundStyle(here ? .green : .secondary)
}
}
.padding(.horizontal, 10).padding(.vertical, 6)
.background(Color.primary.opacity(0.06), in: Capsule())
}
.buttonStyle(.plain)
.disabled(here)
}
}
.padding(.horizontal, 12).padding(.vertical, 6)
}
}
private func sourceIcon(_ source: String) -> String {
if source.contains("iphone") || source.contains("ios") { return "iphone" }
if source.contains("ipad") { return "ipad" }
if source.contains("mac") { return "laptopcomputer" }
if source.contains("windows") { return "pc" }
return "desktopcomputer"
}
private var feedList: some View {
List(store.feed) { msg in
VStack(alignment: .leading, spacing: 4) {
Text(msg.text ?? "")
.lineLimit(4)
HStack {
Text(msg.source ?? "?").font(.caption2)
Spacer()
Text(msg.date, style: .time).font(.caption2)
}
.foregroundStyle(.secondary)
}
.contentShape(Rectangle())
.onTapGesture { store.copyToPasteboard(msg) }
.swipeActions {
Button("Copy") { store.copyToPasteboard(msg) }.tint(.blue)
}
}
.listStyle(.plain)
.overlay {
if store.feed.isEmpty {
ContentUnavailableView("No messages yet",
systemImage: "doc.on.clipboard",
description: Text("Connect, then send from any device in the room."))
}
}
}
private var composer: some View {
HStack {
TextField("Type or paste…", text: $draft, axis: .vertical)
.textFieldStyle(.roundedBorder)
.lineLimit(1...4)
Button {
if draft.isEmpty { store.sendClipboard() }
else { store.send(draft); draft = "" }
} label: {
Image(systemName: draft.isEmpty ? "doc.on.clipboard" : "paperplane.fill")
.font(.title2)
}
.disabled(!store.connected)
}
.padding()
}
}