Improve code organization and documentation (#325)

* Add MARK headers to improve code organization in major files

* Reorganize peer management functions in BluetoothMeshService

- Removed duplicate getCurrentPeerID(for:) function
- Consolidated peer identity functions in Peer Identity Mapping section
- Moved getPeerFingerprint(), getFingerprint(for:), isPeerIDOurs() to proper location
- Moved getCurrentPeerIDForFingerprint() and getCurrentPeerIDs() from Message Sending section
- Moved notifyPeerIDChange() to Peer Management section

* Consolidate peer management functions in BluetoothMeshService

- Moved getCachedPublicKey() and getCachedSigningKey() from Identity Cache Methods to Peer Connection Management
- Moved getPeerNicknames() and getPeerRSSI() to Peer Connection Management section
- Moved getAllConnectedPeerIDs(), notifyPeerListUpdate(), and cleanupStalePeers() to Peer Connection Management
- Removed duplicate function declarations after consolidation
- Improved code organization by grouping all peer-related functions together

* Consolidate message handling functions in ChatViewModel

- Moved handleHandshakeRequest() from floating location to Message Reception section
- Moved trimMessagesIfNeeded() and trimPrivateChatMessagesIfNeeded() to Message Batching section
- Improved code organization by grouping related message handling functions together
- Removed unnecessary comments from trim functions

* Improve ContentView organization with better documentation

- Added descriptive comments for complex inline computations
- Documented message extraction logic for private vs public chats
- Documented peer data computation and sorting logic
- Improved code readability by explaining complex operations inline
- Note: Attempted to extract complex computations into helper functions, but SwiftUI scope limitations made inline documentation a better approach

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
jack
2025-07-26 00:11:23 +02:00
committed by GitHub
co-authored by jack
parent 4568951736
commit 54c7eba8cb
4 changed files with 310 additions and 137 deletions
+22
View File
@@ -9,6 +9,8 @@
import Foundation
import CryptoKit
// MARK: - Message Padding
// Privacy-preserving padding utilities
struct MessagePadding {
// Standard block sizes for padding
@@ -75,6 +77,8 @@ struct MessagePadding {
}
}
// MARK: - Message Types
enum MessageType: UInt8 {
case announce = 0x01
case leave = 0x03
@@ -127,6 +131,8 @@ enum MessageType: UInt8 {
}
}
// MARK: - Handshake State
// Lazy handshake state tracking
enum LazyHandshakeState {
case none // No session, no handshake attempted
@@ -136,11 +142,15 @@ enum LazyHandshakeState {
case failed(Error) // Handshake failed
}
// MARK: - Special Recipients
// Special recipient ID for broadcast messages
struct SpecialRecipients {
static let broadcast = Data(repeating: 0xFF, count: 8) // All 0xFF = broadcast
}
// MARK: - Core Protocol Structures
struct BitchatPacket: Codable {
let version: UInt8
let type: UInt8
@@ -197,6 +207,8 @@ struct BitchatPacket: Codable {
}
}
// MARK: - Delivery Acknowledgments
// Delivery acknowledgment structure
struct DeliveryAck: Codable {
let originalMessageID: String
@@ -287,6 +299,8 @@ struct DeliveryAck: Codable {
}
}
// MARK: - Read Receipts
// Read receipt structure
struct ReadReceipt: Codable {
let originalMessageID: String
@@ -371,6 +385,8 @@ struct ReadReceipt: Codable {
}
}
// MARK: - Handshake Requests
// Handshake request for pending messages
struct HandshakeRequest: Codable {
let requestID: String
@@ -1031,6 +1047,8 @@ struct VersionAck: Codable {
}
}
// MARK: - Delivery Status
// Delivery status for messages
enum DeliveryStatus: Codable, Equatable {
case sending
@@ -1058,6 +1076,8 @@ enum DeliveryStatus: Codable, Equatable {
}
}
// MARK: - Message Model
class BitchatMessage: Codable {
let id: String
let sender: String
@@ -1120,6 +1140,8 @@ extension BitchatMessage: Equatable {
}
}
// MARK: - Delegate Protocol
protocol BitchatDelegate: AnyObject {
func didReceiveMessage(_ message: BitchatMessage)
func didConnectToPeer(_ peerID: String)