2021-09-14 21:38:12 -07:00
//
2021-11-29 21:35:23 -08:00
// N o d e L i s t . s w i f t
2021-09-14 21:38:12 -07:00
// M e s h t a s t i c C l i e n t
//
// C r e a t e d b y G a r t h V a n d e r H o u w e n o n 8 / 7 / 2 1 .
//
// A b s t r a c t :
2021-09-23 20:10:53 -07:00
// A v i e w s h o w i n g a l i s t o f d e v i c e s t h a t h a v e b e e n s e e n o n t h e m e s h n e t w o r k f r o m t h e p e r s p e c t i v e o f t h e c o n n e c t e d d e v i c e .
2021-09-14 21:38:12 -07:00
import SwiftUI
struct NodeList : View {
2021-12-25 23:48:12 -08:00
2021-12-15 23:53:45 -08:00
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
2022-02-22 09:08:06 -10:00
@ EnvironmentObject var userSettings : UserSettings
2021-12-25 23:48:12 -08:00
2021-12-15 23:53:45 -08:00
@ FetchRequest (
2021-12-16 22:45:18 -08:00
sortDescriptors : [ NSSortDescriptor ( key : " lastHeard " , ascending : false ) ] ,
2021-12-15 23:53:45 -08:00
animation : . default )
2021-12-25 23:48:12 -08:00
2021-12-18 01:01:56 -08:00
private var nodes : FetchedResults < NodeInfoEntity >
2021-12-25 23:48:12 -08:00
2021-11-29 15:59:06 -08:00
@ State private var selection : String ?
2021-09-14 21:38:12 -07:00
var body : some View {
2021-12-25 23:48:12 -08:00
2021-09-14 21:38:12 -07:00
NavigationView {
2021-11-29 15:59:06 -08:00
2021-09-14 21:38:12 -07:00
List {
2021-09-23 20:10:53 -07:00
2021-12-15 23:53:45 -08:00
if nodes . count = = 0 {
2021-12-25 23:48:12 -08:00
2021-10-15 00:52:48 -07:00
Text ( " Scan for Radios " ) . font ( . largeTitle )
Text ( " No LoRa Mesh Nodes Found " ) . font ( . title2 )
2021-09-23 20:10:53 -07:00
Text ( " Go to the bluetooth section in the bottom right menu and click the Start Scanning button to scan for nearby radios and find your Meshtastic device. Make sure your device is powered on and near your phone or tablet. " )
. font ( . body )
Text ( " Once the device shows under Available Devices touch the device you want to connect to and it will pull node information over BLE and populate the node list and mesh map in the Meshtastic app. " )
Text ( " Views with bluetooth functionality will show an indicator in the upper right hand corner show if bluetooth is on, and if a device is connected. " )
2021-12-16 22:45:18 -08:00
. listRowSeparator ( . visible )
2021-12-25 23:48:12 -08:00
2021-11-29 15:59:06 -08:00
} else {
2021-12-15 23:53:45 -08:00
ForEach ( nodes ) { node in
2021-12-25 23:48:12 -08:00
2021-12-15 23:53:45 -08:00
let index = nodes . firstIndex ( where : { $0 . id = = node . id } )
2021-12-25 23:48:12 -08:00
2021-12-16 14:13:54 -08:00
NavigationLink ( destination : NodeDetail ( node : node ) , tag : String ( index ! ) , selection : $ selection ) {
2021-12-25 23:48:12 -08:00
2022-02-03 23:52:09 -08:00
let connected : Bool = ( bleManager . connectedPeripheral != nil && bleManager . connectedPeripheral . num = = node . num )
2021-12-25 23:48:12 -08:00
2021-12-16 23:08:26 -08:00
VStack ( alignment : . leading ) {
2021-12-25 23:48:12 -08:00
2021-12-16 23:08:26 -08:00
HStack {
2021-11-29 15:59:06 -08:00
2021-12-16 23:08:26 -08:00
CircleText ( text : node . user ? . shortName ? ? " ??? " , color : Color . accentColor ) . offset ( y : 1 ) . padding ( . trailing , 5 )
. offset ( x : - 15 )
2021-12-16 22:45:18 -08:00
2021-12-16 23:08:26 -08:00
if UIDevice . current . userInterfaceIdiom = = . pad { Text ( node . user ? . longName ? ? " Unknown " ) . font ( . headline )
. offset ( x : - 15 )
} else {
Text ( node . user ? . longName ? ? " Unknown " ) . font ( . title ) . offset ( x : - 15 )
2021-12-16 22:45:18 -08:00
}
2021-12-16 23:08:26 -08:00
}
. padding ( . bottom , 10 )
2021-12-25 23:48:12 -08:00
2021-12-16 23:08:26 -08:00
if connected {
2022-05-25 22:30:48 -07:00
2021-12-16 23:08:26 -08:00
HStack ( alignment : . bottom ) {
2021-12-25 23:48:12 -08:00
2022-05-27 19:18:33 -07:00
Image ( systemName : " repeat.circle.fill " ) . font ( . title2 )
2021-12-16 23:08:26 -08:00
. foregroundColor ( . accentColor ) . symbolRenderingMode ( . hierarchical )
2022-05-25 22:30:48 -07:00
if UIDevice . current . userInterfaceIdiom = = . pad || UIDevice . current . userInterfaceIdiom = = . mac {
Text ( " Currently Connected " ) . font ( . headline ) . foregroundColor ( Color . accentColor )
} else {
Text ( " Currently Connected " ) . font ( . title3 ) . foregroundColor ( Color . accentColor )
}
2021-12-16 22:45:18 -08:00
}
2021-12-16 23:08:26 -08:00
Spacer ( )
}
2021-12-25 23:48:12 -08:00
2021-12-16 23:08:26 -08:00
HStack ( alignment : . bottom ) {
2021-12-16 22:45:18 -08:00
2022-05-27 19:18:33 -07:00
Image ( systemName : " clock.badge.checkmark.fill " ) . font ( . title3 )
. foregroundColor ( . accentColor ) . symbolRenderingMode ( . hierarchical )
2022-05-25 22:30:48 -07:00
if UIDevice . current . userInterfaceIdiom = = . pad || UIDevice . current . userInterfaceIdiom = = . mac {
LastHeardText ( lastHeard : node . lastHeard ) . font ( . subheadline ) . foregroundColor ( . gray )
2021-12-16 23:08:26 -08:00
} else {
2022-05-25 22:30:48 -07:00
LastHeardText ( lastHeard : node . lastHeard ) . font ( . title3 ) . foregroundColor ( . gray )
2021-12-16 22:45:18 -08:00
}
}
}
2021-12-16 23:08:26 -08:00
. padding ( [ . leading , . top , . bottom ] )
2021-12-15 23:53:45 -08:00
}
}
2021-09-14 21:38:12 -07:00
}
2021-09-23 07:48:25 -07:00
}
2021-09-14 21:38:12 -07:00
. navigationTitle ( " All Nodes " )
2021-12-25 23:48:12 -08:00
. onAppear {
2022-05-25 22:30:48 -07:00
2021-12-15 23:53:45 -08:00
self . bleManager . context = context
2022-02-22 09:08:06 -10:00
self . bleManager . userSettings = userSettings
2021-12-15 23:53:45 -08:00
if UIDevice . current . userInterfaceIdiom = = . pad {
if nodes . count > 0 {
selection = " 0 "
2021-10-22 10:03:50 -07:00
}
}
2021-12-15 23:53:45 -08:00
}
2021-09-18 15:33:35 -07:00
}
2021-12-16 23:08:26 -08:00
. ignoresSafeArea ( . all , edges : [ . leading , . trailing ] )
2021-10-17 01:05:44 -07:00
. navigationViewStyle ( DoubleColumnNavigationViewStyle ( ) )
2021-09-14 21:38:12 -07:00
}
}