Initial commit: Bluetooth mesh chat app with end-to-end encryption
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"color" : {
|
||||
"color-space" : "srgb",
|
||||
"components" : {
|
||||
"alpha" : "1.000",
|
||||
"blue" : "0.000",
|
||||
"green" : "1.000",
|
||||
"red" : "0.000"
|
||||
}
|
||||
},
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"filename" : "icon_20x20@2x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_20x20@3x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_29x29@2x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_29x29@3x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_40x40@2x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_40x40@3x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_60x60@2x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "60x60"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_60x60@3x.png",
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "60x60"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_20x20.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_20x20@2x.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_29x29.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_29x29@2x.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_40x40.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_40x40@2x.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_76x76.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "76x76"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_76x76@2x.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "76x76"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_83.5x83.5@2x.png",
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "83.5x83.5"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_1024x1024.png",
|
||||
"idiom" : "ios-marketing",
|
||||
"scale" : "1x",
|
||||
"size" : "1024x1024"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_16x16.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "16x16"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_16x16@2x.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "16x16"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_32x32.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "32x32"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_32x32@2x.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "32x32"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_128x128.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "128x128"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_128x128@2x.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "128x128"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_256x256.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "256x256"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_256x256@2x.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "256x256"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_512x512.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "1x",
|
||||
"size" : "512x512"
|
||||
},
|
||||
{
|
||||
"filename" : "icon_512x512@2x.png",
|
||||
"idiom" : "mac",
|
||||
"scale" : "2x",
|
||||
"size" : "512x512"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 5.2 KiB |
|
After Width: | Height: | Size: 848 B |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 356 B |
|
After Width: | Height: | Size: 429 B |
|
After Width: | Height: | Size: 378 B |
|
After Width: | Height: | Size: 497 B |
|
After Width: | Height: | Size: 570 B |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 401 B |
|
After Width: | Height: | Size: 564 B |
|
After Width: | Height: | Size: 668 B |
|
After Width: | Height: | Size: 429 B |
|
After Width: | Height: | Size: 597 B |
|
After Width: | Height: | Size: 497 B |
|
After Width: | Height: | Size: 641 B |
|
After Width: | Height: | Size: 765 B |
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 5.2 KiB |
|
After Width: | Height: | Size: 765 B |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 628 B |
|
After Width: | Height: | Size: 930 B |
|
After Width: | Height: | Size: 976 B |
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct BitchatApp: App {
|
||||
@StateObject private var chatViewModel = ChatViewModel()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environmentObject(chatViewModel)
|
||||
}
|
||||
#if os(macOS)
|
||||
.windowStyle(.hiddenTitleBar)
|
||||
.windowResizability(.contentSize)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleDisplayName</key>
|
||||
<string>bitchat</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>$(MARKETING_VERSION)</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
|
||||
<key>NSBluetoothAlwaysUsageDescription</key>
|
||||
<string>bitchat uses Bluetooth to create a secure mesh network for chatting with nearby users.</string>
|
||||
<key>NSBluetoothPeripheralUsageDescription</key>
|
||||
<string>bitchat uses Bluetooth to discover and connect with other bitchat users nearby.</string>
|
||||
<key>UILaunchScreen</key>
|
||||
<dict>
|
||||
<key>UIColorName</key>
|
||||
<string>Black</string>
|
||||
</dict>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,66 @@
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
|
||||
enum MessageType: UInt8 {
|
||||
case handshake = 0x01
|
||||
case message = 0x02
|
||||
case ack = 0x03
|
||||
case relay = 0x04
|
||||
case announce = 0x05
|
||||
case keyExchange = 0x06
|
||||
}
|
||||
|
||||
struct BitchatPacket: Codable {
|
||||
let version: UInt8
|
||||
let type: UInt8
|
||||
let senderID: Data
|
||||
let recipientID: Data?
|
||||
let timestamp: UInt64
|
||||
let payload: Data
|
||||
let signature: Data?
|
||||
var ttl: UInt8
|
||||
|
||||
init(type: UInt8, senderID: Data, recipientID: Data?, timestamp: UInt64, payload: Data, signature: Data?, ttl: UInt8) {
|
||||
self.version = 1
|
||||
self.type = type
|
||||
self.senderID = senderID
|
||||
self.recipientID = recipientID
|
||||
self.timestamp = timestamp
|
||||
self.payload = payload
|
||||
self.signature = signature
|
||||
self.ttl = ttl
|
||||
}
|
||||
|
||||
var data: Data? {
|
||||
try? JSONEncoder().encode(self)
|
||||
}
|
||||
|
||||
static func from(_ data: Data) -> BitchatPacket? {
|
||||
try? JSONDecoder().decode(BitchatPacket.self, from: data)
|
||||
}
|
||||
}
|
||||
|
||||
struct BitchatMessage: Codable {
|
||||
let id: String
|
||||
let sender: String
|
||||
let content: String
|
||||
let timestamp: Date
|
||||
let isRelay: Bool
|
||||
let originalSender: String?
|
||||
|
||||
init(sender: String, content: String, timestamp: Date, isRelay: Bool, originalSender: String? = nil) {
|
||||
self.id = UUID().uuidString
|
||||
self.sender = sender
|
||||
self.content = content
|
||||
self.timestamp = timestamp
|
||||
self.isRelay = isRelay
|
||||
self.originalSender = originalSender
|
||||
}
|
||||
}
|
||||
|
||||
protocol BitchatDelegate: AnyObject {
|
||||
func didReceiveMessage(_ message: BitchatMessage)
|
||||
func didConnectToPeer(_ peerID: String)
|
||||
func didDisconnectFromPeer(_ peerID: String)
|
||||
func didUpdatePeerList(_ peers: [String])
|
||||
}
|
||||
@@ -0,0 +1,528 @@
|
||||
import Foundation
|
||||
import CoreBluetooth
|
||||
import Combine
|
||||
|
||||
class BluetoothMeshService: NSObject {
|
||||
static let serviceUUID = CBUUID(string: "F47B5E2D-4A9E-4C5A-9B3F-8E1D2C3A4B5C")
|
||||
static let characteristicUUID = CBUUID(string: "A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D")
|
||||
|
||||
private var centralManager: CBCentralManager!
|
||||
private var peripheralManager: CBPeripheralManager!
|
||||
private var discoveredPeripherals: [CBPeripheral] = []
|
||||
private var connectedPeripherals: [String: CBPeripheral] = [:]
|
||||
private var peripheralCharacteristics: [CBPeripheral: CBCharacteristic] = [:]
|
||||
private var characteristic: CBMutableCharacteristic!
|
||||
private var subscribedCentrals: [CBCentral] = []
|
||||
private var peerNicknames: [String: String] = [:]
|
||||
private var activePeers: Set<String> = [] // Track all active peers
|
||||
|
||||
weak var delegate: BitchatDelegate?
|
||||
private let encryptionService = EncryptionService()
|
||||
private let messageQueue = DispatchQueue(label: "bitchat.messageQueue", attributes: .concurrent)
|
||||
private var processedMessages = Set<String>()
|
||||
private let maxTTL: UInt8 = 5
|
||||
|
||||
let myPeerID: String
|
||||
|
||||
override init() {
|
||||
self.myPeerID = UUID().uuidString.prefix(8).lowercased()
|
||||
super.init()
|
||||
|
||||
centralManager = CBCentralManager(delegate: self, queue: nil)
|
||||
peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
|
||||
}
|
||||
|
||||
func startServices() {
|
||||
// Start both central and peripheral services
|
||||
if centralManager.state == .poweredOn {
|
||||
startScanning()
|
||||
}
|
||||
if peripheralManager.state == .poweredOn {
|
||||
setupPeripheral()
|
||||
startAdvertising()
|
||||
}
|
||||
}
|
||||
|
||||
func startAdvertising() {
|
||||
guard peripheralManager.state == .poweredOn else {
|
||||
return
|
||||
}
|
||||
|
||||
let advertisementData: [String: Any] = [
|
||||
CBAdvertisementDataServiceUUIDsKey: [BluetoothMeshService.serviceUUID],
|
||||
CBAdvertisementDataLocalNameKey: "bitchat-\(myPeerID)"
|
||||
]
|
||||
peripheralManager.startAdvertising(advertisementData)
|
||||
}
|
||||
|
||||
func startScanning() {
|
||||
guard centralManager.state == .poweredOn else {
|
||||
return
|
||||
}
|
||||
centralManager.scanForPeripherals(withServices: [BluetoothMeshService.serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
|
||||
}
|
||||
|
||||
private func setupPeripheral() {
|
||||
let characteristic = CBMutableCharacteristic(
|
||||
type: BluetoothMeshService.characteristicUUID,
|
||||
properties: [.read, .write, .writeWithoutResponse, .notify],
|
||||
value: nil,
|
||||
permissions: [.readable, .writeable]
|
||||
)
|
||||
|
||||
let service = CBMutableService(type: BluetoothMeshService.serviceUUID, primary: true)
|
||||
service.characteristics = [characteristic]
|
||||
|
||||
peripheralManager.add(service)
|
||||
self.characteristic = characteristic
|
||||
}
|
||||
|
||||
func sendMessage(_ content: String, to recipientID: String? = nil) {
|
||||
messageQueue.async { [weak self] in
|
||||
guard let self = self else { return }
|
||||
|
||||
let nickname = self.delegate as? ChatViewModel
|
||||
let senderNick = nickname?.nickname ?? self.myPeerID
|
||||
|
||||
let message = BitchatMessage(
|
||||
sender: senderNick,
|
||||
content: content,
|
||||
timestamp: Date(),
|
||||
isRelay: false,
|
||||
originalSender: nil
|
||||
)
|
||||
|
||||
if let messageData = try? JSONEncoder().encode(message) {
|
||||
let packet = BitchatPacket(
|
||||
type: MessageType.message.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: recipientID?.data(using: .utf8),
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: messageData,
|
||||
signature: try? self.encryptionService.sign(messageData),
|
||||
ttl: self.maxTTL
|
||||
)
|
||||
|
||||
self.broadcastPacket(packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func getPeerNicknames() -> [String: String] {
|
||||
return peerNicknames
|
||||
}
|
||||
|
||||
private func getAllConnectedPeerIDs() -> [String] {
|
||||
return Array(activePeers)
|
||||
}
|
||||
|
||||
private func broadcastPacket(_ packet: BitchatPacket) {
|
||||
guard let data = packet.data else { return }
|
||||
|
||||
// Send to connected peripherals (as central)
|
||||
for (_, peripheral) in connectedPeripherals {
|
||||
if let characteristic = peripheralCharacteristics[peripheral] {
|
||||
// Use withResponse for larger data for reliability
|
||||
let writeType: CBCharacteristicWriteType = data.count > 50000 ? .withResponse : .withoutResponse
|
||||
peripheral.writeValue(data, for: characteristic, type: writeType)
|
||||
}
|
||||
}
|
||||
|
||||
// Send to subscribed centrals (as peripheral)
|
||||
if characteristic != nil && !subscribedCentrals.isEmpty {
|
||||
peripheralManager.updateValue(data, for: characteristic, onSubscribedCentrals: subscribedCentrals)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleReceivedPacket(_ packet: BitchatPacket, from peerID: String) {
|
||||
guard packet.ttl > 0 else { return }
|
||||
|
||||
let messageID = "\(packet.timestamp)-\(String(data: packet.senderID, encoding: .utf8) ?? "")"
|
||||
guard !processedMessages.contains(messageID) else { return }
|
||||
processedMessages.insert(messageID)
|
||||
|
||||
if processedMessages.count > 1000 {
|
||||
processedMessages.removeAll()
|
||||
}
|
||||
|
||||
switch MessageType(rawValue: packet.type) {
|
||||
case .message:
|
||||
if let message = try? JSONDecoder().decode(BitchatMessage.self, from: packet.payload) {
|
||||
// Ignore our own messages
|
||||
if let senderID = String(data: packet.senderID, encoding: .utf8), senderID == myPeerID {
|
||||
return
|
||||
}
|
||||
|
||||
// Store nickname mapping
|
||||
if let senderID = String(data: packet.senderID, encoding: .utf8) {
|
||||
peerNicknames[senderID] = message.sender
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didReceiveMessage(message)
|
||||
}
|
||||
|
||||
var relayPacket = packet
|
||||
relayPacket.ttl -= 1
|
||||
if relayPacket.ttl > 0 {
|
||||
self.broadcastPacket(relayPacket)
|
||||
}
|
||||
}
|
||||
|
||||
case .keyExchange:
|
||||
if let publicKeyData = try? JSONDecoder().decode(Data.self, from: packet.payload) {
|
||||
try? encryptionService.addPeerPublicKey(peerID, publicKeyData: publicKeyData)
|
||||
|
||||
// Track this peer temporarily but don't announce until we get their name
|
||||
if peerID != "unknown" && peerID != myPeerID && !peerNicknames.keys.contains(peerID) {
|
||||
// Just track them, don't announce yet
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
|
||||
// Send announce with our nickname immediately
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
if let self = self, let vm = self.delegate as? ChatViewModel {
|
||||
let announcePacket = BitchatPacket(
|
||||
type: MessageType.announce.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: peerID.data(using: .utf8),
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: vm.nickname.data(using: .utf8)!,
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
self.broadcastPacket(announcePacket)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case .announce:
|
||||
if let nickname = String(data: packet.payload, encoding: .utf8) {
|
||||
// Store the nickname
|
||||
peerNicknames[peerID] = nickname
|
||||
|
||||
// Add to active peers if not already there
|
||||
if peerID != "unknown" && peerID != myPeerID && !activePeers.contains(peerID) {
|
||||
activePeers.insert(peerID)
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didConnectToPeer(nickname)
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
} else {
|
||||
// Just update the peer list to refresh nicknames
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothMeshService: CBCentralManagerDelegate {
|
||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||
if central.state == .poweredOn {
|
||||
startScanning()
|
||||
}
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
||||
// Connect to any device we discover - we'll filter by service later
|
||||
if !discoveredPeripherals.contains(peripheral) {
|
||||
discoveredPeripherals.append(peripheral)
|
||||
peripheral.delegate = self
|
||||
central.connect(peripheral, options: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||
peripheral.delegate = self
|
||||
peripheral.discoverServices([BluetoothMeshService.serviceUUID])
|
||||
|
||||
// Extract peer ID from advertisement or generate one
|
||||
var peerID = peripheral.identifier.uuidString.prefix(8).lowercased()
|
||||
if let name = peripheral.name, name.hasPrefix("bitchat-") {
|
||||
peerID = String(name.dropFirst(8))
|
||||
}
|
||||
|
||||
connectedPeripherals[String(peerID)] = peripheral
|
||||
|
||||
// Update peer list to show we're connecting
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
|
||||
if let peerID = connectedPeripherals.first(where: { $0.value == peripheral })?.key {
|
||||
connectedPeripherals.removeValue(forKey: peerID)
|
||||
peripheralCharacteristics.removeValue(forKey: peripheral)
|
||||
|
||||
// Remove from active peers
|
||||
activePeers.remove(peerID)
|
||||
|
||||
// Only show disconnect if we have a resolved nickname
|
||||
if let nickname = peerNicknames[peerID], nickname != peerID {
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didDisconnectFromPeer(nickname)
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
} else {
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove from discovered list to allow reconnection
|
||||
discoveredPeripherals.removeAll { $0 == peripheral }
|
||||
|
||||
// Continue scanning for reconnection
|
||||
if centralManager.state == .poweredOn {
|
||||
// Always restart scan to ensure we can reconnect
|
||||
centralManager.stopScan()
|
||||
centralManager.scanForPeripherals(withServices: [BluetoothMeshService.serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothMeshService: CBPeripheralDelegate {
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||
guard let services = peripheral.services else { return }
|
||||
|
||||
for service in services {
|
||||
peripheral.discoverCharacteristics([BluetoothMeshService.characteristicUUID], for: service)
|
||||
}
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
||||
guard let characteristics = service.characteristics else { return }
|
||||
|
||||
for characteristic in characteristics {
|
||||
if characteristic.uuid == BluetoothMeshService.characteristicUUID {
|
||||
peripheral.setNotifyValue(true, for: characteristic)
|
||||
peripheralCharacteristics[peripheral] = characteristic
|
||||
|
||||
// Wait a moment for subscription to complete before sending data
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
|
||||
let publicKeyData = self.encryptionService.publicKey.rawRepresentation
|
||||
let packet = BitchatPacket(
|
||||
type: MessageType.keyExchange.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: nil,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: try! JSONEncoder().encode(publicKeyData),
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
|
||||
if let data = packet.data {
|
||||
peripheral.writeValue(data, for: characteristic, type: .withResponse)
|
||||
}
|
||||
|
||||
// Also send announce packet immediately
|
||||
if let vm = self.delegate as? ChatViewModel {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
let announcePacket = BitchatPacket(
|
||||
type: MessageType.announce.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: nil,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: vm.nickname.data(using: .utf8)!,
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
if let data = announcePacket.data {
|
||||
peripheral.writeValue(data, for: characteristic, type: .withResponse)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
guard let data = characteristic.value,
|
||||
let packet = BitchatPacket.from(data) else {
|
||||
return
|
||||
}
|
||||
|
||||
let peerID = connectedPeripherals.first(where: { $0.value == peripheral })?.key ?? "unknown"
|
||||
handleReceivedPacket(packet, from: peerID)
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
// Handle write completion if needed
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
|
||||
peripheral.discoverServices([BluetoothMeshService.serviceUUID])
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
|
||||
// Handle notification state update if needed
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothMeshService: CBPeripheralManagerDelegate {
|
||||
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
|
||||
switch peripheral.state {
|
||||
case .poweredOn:
|
||||
setupPeripheral()
|
||||
startAdvertising()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
|
||||
// Handle service addition if needed
|
||||
}
|
||||
|
||||
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
|
||||
for request in requests {
|
||||
if let data = request.value,
|
||||
let packet = BitchatPacket.from(data) {
|
||||
// Try to identify peer from packet
|
||||
let peerID = String(data: packet.senderID, encoding: .utf8) ?? "unknown"
|
||||
|
||||
// Store the central for updates
|
||||
if !subscribedCentrals.contains(request.central) {
|
||||
subscribedCentrals.append(request.central)
|
||||
}
|
||||
|
||||
// Track this peer as connected
|
||||
if peerID != "unknown" && peerID != myPeerID {
|
||||
// Send key exchange back if we haven't already
|
||||
if packet.type == MessageType.keyExchange.rawValue {
|
||||
let publicKeyData = self.encryptionService.publicKey.rawRepresentation
|
||||
let responsePacket = BitchatPacket(
|
||||
type: MessageType.keyExchange.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: peerID.data(using: .utf8),
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: try! JSONEncoder().encode(publicKeyData),
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
if let data = responsePacket.data {
|
||||
peripheral.updateValue(data, for: self.characteristic, onSubscribedCentrals: [request.central])
|
||||
}
|
||||
|
||||
// Also send announce immediately after key exchange
|
||||
if let vm = self.delegate as? ChatViewModel {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
let announcePacket = BitchatPacket(
|
||||
type: MessageType.announce.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: peerID.data(using: .utf8),
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: vm.nickname.data(using: .utf8)!,
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
if let data = announcePacket.data {
|
||||
peripheral.updateValue(data, for: self.characteristic, onSubscribedCentrals: [request.central])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
|
||||
handleReceivedPacket(packet, from: peerID)
|
||||
peripheral.respond(to: request, withResult: .success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
|
||||
if !subscribedCentrals.contains(central) {
|
||||
subscribedCentrals.append(central)
|
||||
|
||||
// Send our public key to the newly connected central
|
||||
let publicKeyData = encryptionService.publicKey.rawRepresentation
|
||||
let keyPacket = BitchatPacket(
|
||||
type: MessageType.keyExchange.rawValue,
|
||||
senderID: myPeerID.data(using: .utf8)!,
|
||||
recipientID: nil,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: try! JSONEncoder().encode(publicKeyData),
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
|
||||
if let data = keyPacket.data {
|
||||
peripheral.updateValue(data, for: self.characteristic, onSubscribedCentrals: [central])
|
||||
|
||||
// Also send announce after key exchange
|
||||
if let vm = delegate as? ChatViewModel {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
let announcePacket = BitchatPacket(
|
||||
type: MessageType.announce.rawValue,
|
||||
senderID: self.myPeerID.data(using: .utf8)!,
|
||||
recipientID: nil,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970),
|
||||
payload: vm.nickname.data(using: .utf8)!,
|
||||
signature: nil,
|
||||
ttl: 1
|
||||
)
|
||||
if let data = announcePacket.data {
|
||||
peripheral.updateValue(data, for: self.characteristic, onSubscribedCentrals: [central])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update peer list to show we're connected (even without peer ID yet)
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
|
||||
subscribedCentrals.removeAll { $0 == central }
|
||||
|
||||
// If no more centrals are subscribed, clear all central-connected peers
|
||||
if subscribedCentrals.isEmpty {
|
||||
// Find and remove peers that were connected as centrals only
|
||||
let peersToRemove = activePeers.filter { peerID in
|
||||
!connectedPeripherals.keys.contains(peerID)
|
||||
}
|
||||
|
||||
for peerID in peersToRemove {
|
||||
activePeers.remove(peerID)
|
||||
if let nickname = peerNicknames[peerID] {
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didDisconnectFromPeer(nickname)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.didUpdatePeerList(self.getAllConnectedPeerIDs())
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure advertising continues for reconnection
|
||||
if peripheralManager.state == .poweredOn && !peripheralManager.isAdvertising {
|
||||
startAdvertising()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
|
||||
class EncryptionService {
|
||||
private var privateKey: Curve25519.KeyAgreement.PrivateKey
|
||||
public let publicKey: Curve25519.KeyAgreement.PublicKey
|
||||
private var peerPublicKeys: [String: Curve25519.KeyAgreement.PublicKey] = [:]
|
||||
private var sharedSecrets: [String: SymmetricKey] = [:]
|
||||
|
||||
init() {
|
||||
self.privateKey = Curve25519.KeyAgreement.PrivateKey()
|
||||
self.publicKey = privateKey.publicKey
|
||||
}
|
||||
|
||||
func addPeerPublicKey(_ peerID: String, publicKeyData: Data) throws {
|
||||
let publicKey = try Curve25519.KeyAgreement.PublicKey(rawRepresentation: publicKeyData)
|
||||
peerPublicKeys[peerID] = publicKey
|
||||
|
||||
let sharedSecret = try privateKey.sharedSecretFromKeyAgreement(with: publicKey)
|
||||
let symmetricKey = sharedSecret.hkdfDerivedSymmetricKey(
|
||||
using: SHA256.self,
|
||||
salt: "bitchat-v1".data(using: .utf8)!,
|
||||
sharedInfo: Data(),
|
||||
outputByteCount: 32
|
||||
)
|
||||
sharedSecrets[peerID] = symmetricKey
|
||||
}
|
||||
|
||||
func encrypt(_ data: Data, for peerID: String) throws -> Data {
|
||||
guard let symmetricKey = sharedSecrets[peerID] else {
|
||||
throw EncryptionError.noSharedSecret
|
||||
}
|
||||
|
||||
let sealedBox = try AES.GCM.seal(data, using: symmetricKey)
|
||||
return sealedBox.combined ?? Data()
|
||||
}
|
||||
|
||||
func decrypt(_ data: Data, from peerID: String) throws -> Data {
|
||||
guard let symmetricKey = sharedSecrets[peerID] else {
|
||||
throw EncryptionError.noSharedSecret
|
||||
}
|
||||
|
||||
let sealedBox = try AES.GCM.SealedBox(combined: data)
|
||||
return try AES.GCM.open(sealedBox, using: symmetricKey)
|
||||
}
|
||||
|
||||
func sign(_ data: Data) throws -> Data {
|
||||
let signingKey = try Curve25519.Signing.PrivateKey(rawRepresentation: privateKey.rawRepresentation)
|
||||
return try signingKey.signature(for: data)
|
||||
}
|
||||
|
||||
func verify(_ signature: Data, for data: Data, from peerID: String) throws -> Bool {
|
||||
guard let peerPublicKey = peerPublicKeys[peerID] else {
|
||||
return false
|
||||
}
|
||||
|
||||
let verifyingKey = try Curve25519.Signing.PublicKey(rawRepresentation: peerPublicKey.rawRepresentation)
|
||||
return verifyingKey.isValidSignature(signature, for: data)
|
||||
}
|
||||
}
|
||||
|
||||
enum EncryptionError: Error {
|
||||
case noSharedSecret
|
||||
case invalidPublicKey
|
||||
case encryptionFailed
|
||||
case decryptionFailed
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import Combine
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
class ChatViewModel: ObservableObject {
|
||||
@Published var messages: [BitchatMessage] = []
|
||||
@Published var connectedPeers: [String] = []
|
||||
@Published var nickname: String = "" {
|
||||
didSet {
|
||||
nicknameSaveTimer?.invalidate()
|
||||
nicknameSaveTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
|
||||
self.saveNickname()
|
||||
}
|
||||
}
|
||||
}
|
||||
@Published var isConnected = false
|
||||
|
||||
let meshService = BluetoothMeshService()
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private let nicknameKey = "bitchat.nickname"
|
||||
private var nicknameSaveTimer: Timer?
|
||||
|
||||
init() {
|
||||
loadNickname()
|
||||
meshService.delegate = self
|
||||
|
||||
// Start mesh service immediately
|
||||
meshService.startServices()
|
||||
}
|
||||
|
||||
private func loadNickname() {
|
||||
if let savedNickname = userDefaults.string(forKey: nicknameKey) {
|
||||
nickname = savedNickname
|
||||
} else {
|
||||
nickname = "user\(Int.random(in: 1000...9999))"
|
||||
saveNickname()
|
||||
}
|
||||
}
|
||||
|
||||
func saveNickname() {
|
||||
userDefaults.set(nickname, forKey: nicknameKey)
|
||||
userDefaults.synchronize() // Force immediate save
|
||||
}
|
||||
|
||||
func sendMessage(_ content: String) {
|
||||
guard !content.isEmpty else { return }
|
||||
|
||||
// Add message to local display
|
||||
let message = BitchatMessage(
|
||||
sender: nickname,
|
||||
content: content,
|
||||
timestamp: Date(),
|
||||
isRelay: false,
|
||||
originalSender: nil
|
||||
)
|
||||
messages.append(message)
|
||||
|
||||
// Send via mesh
|
||||
meshService.sendMessage(content)
|
||||
}
|
||||
|
||||
|
||||
func formatTimestamp(_ date: Date) -> String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "HH:mm:ss"
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
func formatMessage(_ message: BitchatMessage, colorScheme: ColorScheme) -> AttributedString {
|
||||
var result = AttributedString()
|
||||
|
||||
let isDark = colorScheme == .dark
|
||||
let primaryColor = isDark ? Color.green : Color(red: 0, green: 0.5, blue: 0)
|
||||
let secondaryColor = primaryColor.opacity(0.7)
|
||||
|
||||
let timestamp = AttributedString("[\(formatTimestamp(message.timestamp))] ")
|
||||
var timestampStyle = AttributeContainer()
|
||||
timestampStyle.foregroundColor = secondaryColor
|
||||
timestampStyle.font = .system(size: 12, design: .monospaced)
|
||||
result.append(timestamp.mergingAttributes(timestampStyle))
|
||||
|
||||
if message.sender == "system" {
|
||||
let content = AttributedString("* \(message.content) *")
|
||||
var contentStyle = AttributeContainer()
|
||||
contentStyle.foregroundColor = secondaryColor
|
||||
contentStyle.font = .system(size: 12, design: .monospaced).italic()
|
||||
result.append(content.mergingAttributes(contentStyle))
|
||||
} else {
|
||||
let sender = AttributedString("<\(message.sender)> ")
|
||||
var senderStyle = AttributeContainer()
|
||||
senderStyle.foregroundColor = message.sender == nickname ? primaryColor : primaryColor.opacity(0.9)
|
||||
senderStyle.font = .system(size: 12, weight: .medium, design: .monospaced)
|
||||
result.append(sender.mergingAttributes(senderStyle))
|
||||
|
||||
let content = AttributedString(message.content)
|
||||
var contentStyle = AttributeContainer()
|
||||
contentStyle.font = .system(size: 14, design: .monospaced)
|
||||
contentStyle.foregroundColor = isDark ? Color.white : Color.black
|
||||
result.append(content.mergingAttributes(contentStyle))
|
||||
|
||||
if message.isRelay, let originalSender = message.originalSender {
|
||||
let relay = AttributedString(" (via \(originalSender))")
|
||||
var relayStyle = AttributeContainer()
|
||||
relayStyle.foregroundColor = secondaryColor
|
||||
relayStyle.font = .system(size: 11, design: .monospaced)
|
||||
result.append(relay.mergingAttributes(relayStyle))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
extension ChatViewModel: BitchatDelegate {
|
||||
func didReceiveMessage(_ message: BitchatMessage) {
|
||||
messages.append(message)
|
||||
|
||||
#if os(iOS)
|
||||
// Haptic feedback for new messages
|
||||
let impactFeedback = UIImpactFeedbackGenerator(style: .light)
|
||||
impactFeedback.impactOccurred()
|
||||
#endif
|
||||
}
|
||||
|
||||
func didConnectToPeer(_ peerID: String) {
|
||||
isConnected = true
|
||||
let systemMessage = BitchatMessage(
|
||||
sender: "system",
|
||||
content: "\(peerID) has joined",
|
||||
timestamp: Date(),
|
||||
isRelay: false,
|
||||
originalSender: nil
|
||||
)
|
||||
messages.append(systemMessage)
|
||||
}
|
||||
|
||||
func didDisconnectFromPeer(_ peerID: String) {
|
||||
let systemMessage = BitchatMessage(
|
||||
sender: "system",
|
||||
content: "\(peerID) has left",
|
||||
timestamp: Date(),
|
||||
isRelay: false,
|
||||
originalSender: nil
|
||||
)
|
||||
messages.append(systemMessage)
|
||||
}
|
||||
|
||||
func didUpdatePeerList(_ peers: [String]) {
|
||||
connectedPeers = peers
|
||||
isConnected = !peers.isEmpty
|
||||
|
||||
// If we just disconnected from all peers, ensure UI updates
|
||||
if peers.isEmpty && isConnected {
|
||||
isConnected = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@EnvironmentObject var viewModel: ChatViewModel
|
||||
@State private var messageText = ""
|
||||
@FocusState private var isTextFieldFocused: Bool
|
||||
@Environment(\.colorScheme) var colorScheme
|
||||
@State private var showPeerList = false
|
||||
|
||||
private var backgroundColor: Color {
|
||||
colorScheme == .dark ? Color.black : Color.white
|
||||
}
|
||||
|
||||
private var textColor: Color {
|
||||
colorScheme == .dark ? Color.green : Color(red: 0, green: 0.5, blue: 0)
|
||||
}
|
||||
|
||||
private var secondaryTextColor: Color {
|
||||
colorScheme == .dark ? Color.green.opacity(0.8) : Color(red: 0, green: 0.5, blue: 0).opacity(0.8)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
headerView
|
||||
Divider()
|
||||
messagesView
|
||||
Divider()
|
||||
inputView
|
||||
}
|
||||
.background(backgroundColor)
|
||||
.foregroundColor(textColor)
|
||||
#if os(macOS)
|
||||
.frame(minWidth: 600, minHeight: 400)
|
||||
#endif
|
||||
}
|
||||
|
||||
private var headerView: some View {
|
||||
HStack {
|
||||
Text("bitchat")
|
||||
.font(.system(size: 18, weight: .medium, design: .monospaced))
|
||||
.foregroundColor(textColor)
|
||||
|
||||
Spacer()
|
||||
|
||||
Menu {
|
||||
if viewModel.connectedPeers.isEmpty {
|
||||
Text("No peers connected")
|
||||
.font(.system(size: 12, design: .monospaced))
|
||||
} else {
|
||||
let peerNicknames = viewModel.meshService.getPeerNicknames()
|
||||
ForEach(viewModel.connectedPeers, id: \.self) { peerID in
|
||||
if let displayName = peerNicknames[peerID], displayName != peerID {
|
||||
// Only show if we have a real nickname
|
||||
Label(displayName, systemImage: "person.fill")
|
||||
.font(.system(size: 12, design: .monospaced))
|
||||
}
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
HStack(spacing: 4) {
|
||||
Circle()
|
||||
.fill(viewModel.isConnected ? textColor : Color.red)
|
||||
.frame(width: 8, height: 8)
|
||||
|
||||
HStack(spacing: 0) {
|
||||
Text(viewModel.isConnected ? "\(viewModel.connectedPeers.count) \(viewModel.connectedPeers.count == 1 ? "peer" : "peers")" : "Scanning...")
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
.foregroundColor(secondaryTextColor)
|
||||
Text(Image(systemName: "chevron.down"))
|
||||
.font(.system(size: 10))
|
||||
.foregroundColor(secondaryTextColor)
|
||||
.baselineOffset(-1)
|
||||
}
|
||||
}
|
||||
.contentShape(Rectangle()) // Make entire area tappable
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.menuStyle(.borderlessButton)
|
||||
.menuIndicator(.hidden)
|
||||
|
||||
Spacer()
|
||||
|
||||
HStack(spacing: 4) {
|
||||
Text("nick:")
|
||||
.font(.system(size: 11, design: .monospaced))
|
||||
.foregroundColor(secondaryTextColor)
|
||||
|
||||
TextField("nickname", text: $viewModel.nickname)
|
||||
.textFieldStyle(.plain)
|
||||
.font(.system(size: 12, design: .monospaced))
|
||||
.frame(maxWidth: 100)
|
||||
.foregroundColor(textColor)
|
||||
.onChange(of: viewModel.nickname) { _ in
|
||||
viewModel.saveNickname()
|
||||
}
|
||||
.onSubmit {
|
||||
viewModel.saveNickname()
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 8)
|
||||
.background(backgroundColor.opacity(0.95))
|
||||
}
|
||||
|
||||
private var messagesView: some View {
|
||||
ScrollViewReader { proxy in
|
||||
ScrollView {
|
||||
LazyVStack(alignment: .leading, spacing: 2) {
|
||||
ForEach(Array(viewModel.messages.enumerated()), id: \.offset) { index, message in
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(viewModel.formatMessage(message, colorScheme: colorScheme))
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 2)
|
||||
.id(index)
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 8)
|
||||
}
|
||||
.background(backgroundColor)
|
||||
.onChange(of: viewModel.messages.count) { _ in
|
||||
withAnimation {
|
||||
proxy.scrollTo(viewModel.messages.count - 1, anchor: .bottom)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var inputView: some View {
|
||||
HStack(spacing: 4) {
|
||||
Text("[\(viewModel.formatTimestamp(Date()))]")
|
||||
.font(.system(size: 12, design: .monospaced))
|
||||
.foregroundColor(secondaryTextColor)
|
||||
.lineLimit(1)
|
||||
.fixedSize()
|
||||
.padding(.leading, 12)
|
||||
|
||||
Text("<\(viewModel.nickname)>")
|
||||
.font(.system(size: 12, weight: .medium, design: .monospaced))
|
||||
.foregroundColor(textColor)
|
||||
.lineLimit(1)
|
||||
.fixedSize()
|
||||
|
||||
TextField("", text: $messageText)
|
||||
.textFieldStyle(.plain)
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
.foregroundColor(textColor)
|
||||
.focused($isTextFieldFocused)
|
||||
.onSubmit {
|
||||
sendMessage()
|
||||
}
|
||||
|
||||
Button(action: sendMessage) {
|
||||
Image(systemName: "arrow.right.circle.fill")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(textColor)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.padding(.trailing, 12)
|
||||
}
|
||||
.padding(.vertical, 10)
|
||||
.background(backgroundColor.opacity(0.95))
|
||||
.onAppear {
|
||||
isTextFieldFocused = true
|
||||
}
|
||||
}
|
||||
|
||||
private func sendMessage() {
|
||||
viewModel.sendMessage(messageText)
|
||||
messageText = ""
|
||||
}
|
||||
}
|
||||