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
2022-11-11 08:06:45 -08:00
// M e s h t a s t i c
2021-09-14 21:38:12 -07:00
//
2022-11-11 08:06:45 -08:00
// C o p y r i g h t ( c ) G a r t h V a n d e r H o u w e n 8 / 7 / 2 1 .
2021-09-14 21:38:12 -07:00
//
// 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
2022-08-19 23:26:02 -07:00
import CoreLocation
2021-09-14 21:38:12 -07:00
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 (
2022-09-13 05:44:04 -07: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
2022-07-07 00:29:52 -07:00
private var nodes : FetchedResults < NodeInfoEntity >
2021-12-25 23:48:12 -08:00
2022-09-13 05:44:04 -07:00
@ State private var selection : NodeInfoEntity ? = nil // N o t h i n g s e l e c t e d b y d e f a u l t .
2021-11-29 15:59:06 -08:00
2021-09-14 21:38:12 -07:00
var body : some View {
2022-09-13 05:44:04 -07:00
NavigationSplitView {
List ( nodes , id : \ . self , selection : $ selection ) { node in
2021-12-15 23:53:45 -08:00
if nodes . count = = 0 {
2022-09-13 05:44:04 -07:00
Text ( " Scan for Radios " ) . font ( . largeTitle )
Text ( " No Meshtastic Nodes Found " ) . font ( . title2 )
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 iPhone, iPad or Mac. " )
. 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 showing if bluetooth is on, and if a device is connected. " )
2021-12-16 22:45:18 -08:00
. listRowSeparator ( . visible )
2022-09-13 05:44:04 -07:00
} else {
2022-10-17 20:54:41 -07:00
NavigationLink ( value : node ) {
let connected : Bool = ( bleManager . connectedPeripheral != nil && bleManager . connectedPeripheral . num = = node . num )
VStack ( alignment : . leading ) {
HStack {
2022-11-11 07:41:30 -08:00
CircleText ( text : node . user ? . shortName ? ? " ??? " , color : . blue , circleSize : 52 , fontSize : 16 ) . offset ( y : 1 ) . padding ( . trailing , 5 )
2022-10-17 20:54:41 -07:00
. offset ( x : - 15 )
2022-11-11 07:41:30 -08:00
Text ( node . user ? . longName ? ? " Unknown " ) . font ( . headline ) . offset ( x : - 15 )
2022-10-17 20:54:41 -07:00
}
. padding ( . bottom , 5 )
if connected {
HStack ( alignment : . bottom ) {
Image ( systemName : " repeat.circle.fill " ) . font ( . title2 )
. foregroundColor ( . accentColor ) . symbolRenderingMode ( . hierarchical )
2022-11-11 08:06:45 -08:00
Text ( " Currently Connected " ) . font ( . callout ) . foregroundColor ( Color . accentColor )
2021-12-16 23:08:26 -08:00
}
2022-10-17 20:54:41 -07:00
. padding ( . bottom , 2 )
}
if node . positions ? . count ? ? 0 > 0 && ( bleManager . connectedPeripheral != nil && bleManager . connectedPeripheral . num != node . num ) {
HStack ( alignment : . bottom ) {
let lastPostion = node . positions ! . reversed ( ) [ 0 ] as ! PositionEntity
let myCoord = CLLocation ( latitude : LocationHelper . currentLocation . latitude , longitude : LocationHelper . currentLocation . longitude )
if lastPostion . coordinate != nil {
let nodeCoord = CLLocation ( latitude : lastPostion . coordinate ! . latitude , longitude : lastPostion . coordinate ! . longitude )
let metersAway = nodeCoord . distance ( from : myCoord )
Image ( systemName : " lines.measurement.horizontal " ) . font ( . title3 )
. foregroundColor ( . accentColor ) . symbolRenderingMode ( . hierarchical )
2022-11-11 08:06:45 -08:00
DistanceText ( meters : metersAway ) . font ( . subheadline ) . foregroundColor ( . gray )
2022-08-19 23:26:02 -07:00
}
2022-09-03 08:50:22 -07:00
}
2022-10-17 20:54:41 -07:00
. padding ( . bottom , 2 )
}
HStack ( alignment : . bottom ) {
Image ( systemName : " clock.badge.checkmark.fill " ) . font ( . headline )
. foregroundColor ( . accentColor ) . symbolRenderingMode ( . hierarchical )
LastHeardText ( lastHeard : node . lastHeard ) . font ( . subheadline ) . foregroundColor ( . gray )
2021-12-16 22:45:18 -08:00
}
2021-12-15 23:53:45 -08:00
}
2022-10-17 20:54:41 -07:00
. padding ( [ . leading , . top , . bottom ] )
}
2022-09-13 05:44:04 -07:00
}
}
2022-10-17 20:54:41 -07:00
. tint ( Color ( UIColor . systemGray ) )
2022-10-17 21:25:15 -07:00
. navigationTitle ( " All Nodes " )
. navigationBarItems ( leading :
MeshtasticLogo ( )
)
. onAppear {
2022-10-17 21:32:14 -07:00
self . bleManager . userSettings = userSettings
self . bleManager . context = context
2022-10-17 21:25:15 -07:00
}
2022-10-14 22:18:28 -07:00
} detail : {
if let node = selection {
NodeDetail ( node : node )
} else {
Text ( " Select a node " )
2022-09-13 05:44:04 -07:00
}
2022-10-14 22:18:28 -07:00
}
2022-10-17 21:32:14 -07:00
}
2021-09-14 21:38:12 -07:00
}