2022-06-04 07:41:52 -07:00
//
// U s e r E n t i t y E x t e n s i o n . s w i f t
2023-03-26 09:08:08 -07:00
// M e s h t a s t i c
2022-06-04 07:41:52 -07:00
//
2022-10-17 18:52:49 -07: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 6 / 3 / 2 2 .
2022-06-04 07:41:52 -07:00
//
import Foundation
2024-05-26 12:15:50 -07:00
import CoreData
2024-06-07 22:09:20 -05:00
import MeshtasticProtobufs
2022-06-04 07:41:52 -07:00
extension UserEntity {
2023-03-06 10:33:18 -08:00
2022-06-04 07:41:52 -07:00
var messageList : [ MessageEntity ] {
2024-07-23 19:10:00 -07:00
let context = PersistenceController . shared . container . viewContext
let fetchRequest = MessageEntity . fetchRequest ( )
fetchRequest . predicate = NSPredicate ( format : " ((toUser == %@) OR (fromUser == %@)) AND toUser != nil AND fromUser != nil AND isEmoji == false AND admin = false AND portNum != 10 " , self , self )
return ( try ? context . fetch ( fetchRequest ) ) ? ? [ MessageEntity ] ( )
2022-06-04 07:41:52 -07:00
}
2023-03-06 10:33:18 -08:00
2023-09-09 21:23:14 -07:00
var sensorMessageList : [ MessageEntity ] {
2024-07-23 19:10:00 -07:00
let context = PersistenceController . shared . container . viewContext
let fetchRequest = MessageEntity . fetchRequest ( )
fetchRequest . predicate = NSPredicate ( format : " (fromUser == %@) AND portNum = 10 " , self )
return ( try ? context . fetch ( fetchRequest ) ) ? ? [ MessageEntity ] ( )
2023-09-09 21:23:14 -07:00
}
2024-05-29 16:40:07 -05:00
2023-08-28 21:46:04 -07:00
var unreadMessages : Int {
2024-05-29 16:40:07 -05:00
let unreadMessages = messageList . filter { ( $0 as AnyObject ) . read = = false }
2023-08-29 07:53:52 -07:00
return unreadMessages . count
2023-08-28 21:46:04 -07:00
}
2024-07-23 19:10:00 -07:00
2024-07-15 22:48:26 -05:00
var hardwareImage : String ? {
guard let hwModel else { return nil }
switch hwModel {
case " HELTECV1 " , " HELTECV3 " , " HELTECV20 " , " HELTECV21 " :
return " HELTECV3 "
case " HELTECWIRELESSPAPER " , " HELTECWIRELESSPAPERV10 " :
return " HELTECWIRELESSPAPER "
case " HELTECWIRELESSTRACKER " , " HELTECWIRELESSTRACKERV10 " :
return " HELTECWIRELESSTRACKER "
case " HELTECWSLV3 " :
return " HELTECWSLV3 "
case " LILYGOTBEAMSCORE " :
return " LILYGOTBEAMS3CORE "
case " NANOG1 " , " NANOG1EXPLORER " :
return " NANOG1 "
case " NANOG2ULTRA " :
return " NANOG2ULTRA "
case " RAK4631 " :
return " RAK4631 "
case " RAK11200 " :
return " RAK11200 "
case " SOLAR_NODE " :
return " SOLAR_NODE "
case " STATIONG1 " :
return " STATIONG1 "
case " Т В Е А М " , " TBEAMVOP7 " :
return " Т В Е А М "
case " TECHO " :
return " TECHO "
case " TLORAV1 " , " TLORAV11P3 " :
return " TLORAV1 "
case " TLORAV2 " , " TLORAT3S3 " , " TLORAV211P6 " , " TLORAV211P8 " :
return " TLORABOARD "
case " UNPHONE " :
return " UNPHONE "
default :
return " UNSET "
}
}
2022-06-04 07:41:52 -07:00
}
2024-05-26 12:15:50 -07:00
public func createUser ( num : Int64 , context : NSManagedObjectContext ) -> UserEntity {
let newUser = UserEntity ( context : context )
newUser . num = Int64 ( num )
2024-05-29 16:40:07 -05:00
let userId = String ( format : " %2X " , num )
2024-05-26 12:15:50 -07:00
newUser . userId = " ! \( userId ) "
let last4 = String ( userId . suffix ( 4 ) )
newUser . longName = " Meshtastic \( last4 ) "
newUser . shortName = last4
newUser . hwModel = " UNSET "
return newUser
}