2022-11-07 18:31:12 -08:00
|
|
|
//
|
|
|
|
|
// ChannelEntityExtension.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 11/7/22.
|
|
|
|
|
//
|
|
|
|
|
import Foundation
|
2024-06-07 22:09:20 -05:00
|
|
|
import CoreData
|
|
|
|
|
import MeshtasticProtobufs
|
2022-11-07 18:31:12 -08:00
|
|
|
|
|
|
|
|
extension ChannelEntity {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-11-07 18:31:12 -08:00
|
|
|
var allPrivateMessages: [MessageEntity] {
|
2024-07-23 19:10:00 -07:00
|
|
|
let context = PersistenceController.shared.container.viewContext
|
|
|
|
|
let fetchRequest = MessageEntity.fetchRequest()
|
2024-08-03 07:02:51 -07:00
|
|
|
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "messageTimestamp", ascending: true)]
|
2024-08-08 19:17:05 -07:00
|
|
|
fetchRequest.predicate = NSPredicate(format: "channel == %ld AND toUser == nil AND isEmoji == false", self.index)
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2024-07-23 19:10:00 -07:00
|
|
|
return (try? context.fetch(fetchRequest)) ?? [MessageEntity]()
|
2022-11-07 18:31:12 -08:00
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-08-29 18:51:57 -07:00
|
|
|
var unreadMessages: Int {
|
2024-05-29 16:40:07 -05:00
|
|
|
|
|
|
|
|
let unreadMessages = allPrivateMessages.filter { ($0 as AnyObject).read == false }
|
2023-08-29 18:51:57 -07:00
|
|
|
return unreadMessages.count
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-04-30 11:09:04 -07:00
|
|
|
var protoBuf: Channel {
|
|
|
|
|
var channel = Channel()
|
|
|
|
|
channel.index = self.index
|
|
|
|
|
channel.settings.name = self.name ?? ""
|
|
|
|
|
channel.settings.psk = self.psk ?? Data()
|
|
|
|
|
channel.role = Channel.Role(rawValue: Int(self.role)) ?? Channel.Role.secondary
|
|
|
|
|
channel.settings.moduleSettings.positionPrecision = UInt32(self.positionPrecision)
|
|
|
|
|
return channel
|
|
|
|
|
}
|
2022-11-07 18:31:12 -08:00
|
|
|
}
|