jsdw_ios/QuickLocation/Section/Group/GroupMemberList/GroupMemberListVM.swift

123 lines
4.2 KiB
Swift

//
// GroupMemberListVM.swift
// QuickLocation
//
// Created by on 2026/6/29.
//
import RxSwift
import RxRelay
import RxDataSources
import ObjectMapper
struct DrivingEventItem: IdentifiableType, Equatable {
typealias Identity = String
let identity: String
let title: String
let iconName: String
let count: Int
init(title: String, iconName: String, count: Int = 0) {
self.identity = title
self.title = title
self.iconName = iconName
self.count = count
}
}
typealias DrivingEventSection = SectionModel<String, DrivingEventItem>
typealias ScheduleRecordSection = SectionModel<String, ScheduleRecordModel>
class GroupMemberListVM {
let groupKey: String
var groupModel: GroupInfoModel?
struct Output {
var sectionedItems: Observable<[GroupMemberListSectionModel]>
var drivingSectionedItems: Observable<[DrivingEventSection]>
var scheduleSectionedItems: Observable<[ScheduleRecordSection]>
}
let output: Output
var memberId: String = ""
private let sectionedItems = PublishSubject<[GroupMemberListSectionModel]>()
private let drivingItemsRelay = BehaviorRelay<[DrivingEventItem]>(value: GroupMemberListVM.defaultDrivingEvents)
private let scheduleItemsRelay = BehaviorRelay<[ScheduleRecordModel]>(value: [])
private var memberList: [GroupMemberModel] = []
private static let defaultDrivingEvents: [DrivingEventItem] = [
DrivingEventItem(title: "急加速", iconName: "GroupMemberList/1"),
DrivingEventItem(title: "急转向", iconName: "GroupMemberList/3"),
DrivingEventItem(title: "急刹", iconName: "GroupMemberList/5"),
DrivingEventItem(title: "手机干扰", iconName: "GroupMemberList/7"),
DrivingEventItem(title: "超速", iconName: "GroupMemberList/2"),
DrivingEventItem(title: "低速", iconName: "GroupMemberList/4"),
DrivingEventItem(title: "频繁变道", iconName: "GroupMemberList/6"),
DrivingEventItem(title: "长时间驾驶", iconName: "GroupMemberList/8")
]
//
func isCurrentUser(id: String) -> Bool {
id == AppContextManager.shared.userId
}
//
func isGroupOwn(id: String) -> Bool {
guard let model = groupModel else { return false }
return model.group_key.contains(id)
}
func loadData(_ list: [GroupMemberModel]) {
var tempmemberList = list
tempmemberList.moveToFirst { $0.is_online == true }
tempmemberList.moveToFirst { $0.user_id == AppContextManager.shared.userId }
tempmemberList.moveToFirst { isGroupOwn(id: $0.user_id) }
memberList = tempmemberList
memberId = memberList.first?.user_id ?? ""
sectionedItems.onNext(memberList.mapSection())
}
///
func rowOf(userId: String) -> Int? {
memberList.firstIndex(where: { $0.user_id == userId })
}
///
func loadScheduleRecords(_ list: [ScheduleRecordModel]) {
scheduleItemsRelay.accept(list)
}
///
func updateDrivingStats(_ stats: DrivingStatsModel) {
let items = Self.defaultDrivingEvents.enumerated().map { i, item in
let count: Int
switch i {
case 0: count = stats.hard_acceleration
case 1: count = stats.sharp_turn
case 2: count = stats.hard_braking
case 3: count = stats.signal_loss
case 4: count = stats.speeding
case 5: count = stats.low_speeding
case 6: count = stats.frequent_lane_change
case 7: count = stats.long_driving
default: count = 0
}
return DrivingEventItem(title: item.title, iconName: item.iconName, count: count)
}
drivingItemsRelay.accept(items)
}
init(groupKey: String) {
self.groupKey = groupKey
output = Output(
sectionedItems: sectionedItems.asObservable(),
drivingSectionedItems: drivingItemsRelay.map { $0.mapSection() }.asObservable(),
scheduleSectionedItems: scheduleItemsRelay.map { $0.mapSection() }.asObservable()
)
}
}