2022-11-22 12:50:56 +00:00
|
|
|
//
|
|
|
|
// CIChatFeatureView.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by Evgeny on 21/11/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
struct CIChatFeatureView: View {
|
2023-10-31 09:44:57 +00:00
|
|
|
@EnvironmentObject var m: ChatModel
|
2024-10-15 10:58:54 +03:00
|
|
|
@Environment(\.revealed) var revealed: Bool
|
2024-07-29 23:17:14 +03:00
|
|
|
@ObservedObject var im = ItemsModel.shared
|
2024-04-12 11:29:58 +01:00
|
|
|
@ObservedObject var chat: Chat
|
2024-07-03 22:42:13 +01:00
|
|
|
@EnvironmentObject var theme: AppTheme
|
2022-11-22 12:50:56 +00:00
|
|
|
var chatItem: ChatItem
|
|
|
|
var feature: Feature
|
2022-12-22 21:01:29 +00:00
|
|
|
var icon: String? = nil
|
2022-11-23 11:04:08 +00:00
|
|
|
var iconColor: Color
|
2022-11-22 12:50:56 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2024-04-12 11:29:58 +01:00
|
|
|
if !revealed, let fs = mergedFeatures() {
|
2023-10-31 09:44:57 +00:00
|
|
|
HStack {
|
|
|
|
ForEach(fs, content: featureIconView)
|
|
|
|
}
|
|
|
|
.padding(.horizontal, 6)
|
|
|
|
.padding(.vertical, 6)
|
|
|
|
} else {
|
|
|
|
fullFeatureView
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct FeatureInfo: Identifiable {
|
|
|
|
var icon: String
|
|
|
|
var scale: CGFloat
|
|
|
|
var color: Color
|
|
|
|
var param: String?
|
|
|
|
|
|
|
|
init(_ f: Feature, _ color: Color, _ param: Int?) {
|
|
|
|
self.icon = f.iconFilled
|
|
|
|
self.scale = f.iconScale
|
|
|
|
self.color = color
|
|
|
|
self.param = f.hasParam && param != nil ? timeText(param) : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var id: String {
|
|
|
|
"\(icon) \(color) \(param ?? "")"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 11:29:58 +01:00
|
|
|
private func mergedFeatures() -> [FeatureInfo]? {
|
2023-10-31 09:44:57 +00:00
|
|
|
var fs: [FeatureInfo] = []
|
|
|
|
var icons: Set<String> = []
|
|
|
|
if var i = m.getChatItemIndex(chatItem) {
|
2024-07-29 23:17:14 +03:00
|
|
|
while i < im.reversedChatItems.count,
|
|
|
|
let f = featureInfo(im.reversedChatItems[i]) {
|
2023-10-31 09:44:57 +00:00
|
|
|
if !icons.contains(f.icon) {
|
|
|
|
fs.insert(f, at: 0)
|
|
|
|
icons.insert(f.icon)
|
|
|
|
}
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fs.count > 1 ? fs : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
private func featureInfo(_ ci: ChatItem) -> FeatureInfo? {
|
|
|
|
switch ci.content {
|
2024-07-03 22:42:13 +01:00
|
|
|
case let .rcvChatFeature(feature, enabled, param): FeatureInfo(feature, enabled.iconColor(theme.colors.secondary), param)
|
|
|
|
case let .sndChatFeature(feature, enabled, param): FeatureInfo(feature, enabled.iconColor(theme.colors.secondary), param)
|
|
|
|
case let .rcvGroupFeature(feature, preference, param, role): FeatureInfo(feature, preference.enabled(role, for: chat.chatInfo.groupInfo?.membership).iconColor(theme.colors.secondary), param)
|
|
|
|
case let .sndGroupFeature(feature, preference, param, role): FeatureInfo(feature, preference.enabled(role, for: chat.chatInfo.groupInfo?.membership).iconColor(theme.colors.secondary), param)
|
2023-10-31 09:44:57 +00:00
|
|
|
default: nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder private func featureIconView(_ f: FeatureInfo) -> some View {
|
|
|
|
let i = Image(systemName: f.icon)
|
|
|
|
.foregroundColor(f.color)
|
|
|
|
.scaleEffect(f.scale)
|
|
|
|
if let param = f.param {
|
|
|
|
HStack {
|
|
|
|
i
|
2024-07-03 22:42:13 +01:00
|
|
|
chatEventText(Text(param), theme.colors.secondary).lineLimit(1)
|
2023-10-31 09:44:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var fullFeatureView: some View {
|
2022-11-28 20:03:39 +04:00
|
|
|
HStack(alignment: .bottom, spacing: 4) {
|
2022-12-22 21:01:29 +00:00
|
|
|
Image(systemName: icon ?? feature.iconFilled)
|
2022-11-22 12:50:56 +00:00
|
|
|
.foregroundColor(iconColor)
|
2022-12-22 21:01:29 +00:00
|
|
|
.scaleEffect(feature.iconScale)
|
2024-07-03 22:42:13 +01:00
|
|
|
chatEventText(chatItem, theme.colors.secondary)
|
2022-11-22 12:50:56 +00:00
|
|
|
}
|
2023-10-31 09:44:57 +00:00
|
|
|
.padding(.horizontal, 6)
|
|
|
|
.padding(.vertical, 4)
|
2022-11-22 12:50:56 +00:00
|
|
|
.textSelection(.disabled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CIChatFeatureView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
let enabled = FeatureEnabled(forUser: false, forContact: false)
|
2024-10-15 10:58:54 +03:00
|
|
|
CIChatFeatureView(
|
|
|
|
chat: Chat.sampleData,
|
|
|
|
chatItem: ChatItem.getChatFeatureSample(.fullDelete, enabled), feature: ChatFeature.fullDelete, iconColor: enabled.iconColor(.secondary)
|
|
|
|
).environment(\.revealed, true)
|
2022-11-22 12:50:56 +00:00
|
|
|
}
|
|
|
|
}
|