2022-12-22 21:01:29 +00:00
|
|
|
//
|
|
|
|
// CIFeaturePreferenceView.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by Evgeny on 21/12/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
struct CIFeaturePreferenceView: View {
|
2023-10-31 09:44:57 +00:00
|
|
|
@ObservedObject var chat: Chat
|
2024-07-03 22:42:13 +01:00
|
|
|
@EnvironmentObject var theme: AppTheme
|
2022-12-22 21:01:29 +00:00
|
|
|
var chatItem: ChatItem
|
|
|
|
var feature: ChatFeature
|
|
|
|
var allowed: FeatureAllowed
|
|
|
|
var param: Int?
|
|
|
|
|
|
|
|
var body: some View {
|
2022-12-23 13:10:00 +00:00
|
|
|
HStack(alignment: .center, spacing: 4) {
|
2022-12-22 21:01:29 +00:00
|
|
|
Image(systemName: feature.icon)
|
2024-07-03 22:42:13 +01:00
|
|
|
.foregroundColor(theme.colors.secondary)
|
2022-12-22 21:01:29 +00:00
|
|
|
.scaleEffect(feature.iconScale)
|
|
|
|
if let ct = chat.chatInfo.contact,
|
|
|
|
allowed != .no && ct.allowsFeature(feature) && !ct.userAllowsFeature(feature) {
|
2022-12-27 19:24:33 +04:00
|
|
|
let setParam = feature == .timedMessages && ct.mergedPreferences.timedMessages.userPreference.preference.ttl == nil
|
|
|
|
featurePreferenceView(acceptText: setParam ? "Set 1 day" : "Accept")
|
2022-12-23 13:10:00 +00:00
|
|
|
.onTapGesture {
|
2022-12-27 19:24:33 +04:00
|
|
|
allowFeatureToContact(ct, feature, param: setParam ? 86400 : nil)
|
2022-12-23 13:10:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
featurePreferenceView()
|
2022-12-22 21:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.leading, 6)
|
|
|
|
.padding(.bottom, 6)
|
|
|
|
.textSelection(.disabled)
|
|
|
|
}
|
2022-12-23 13:10:00 +00:00
|
|
|
|
2022-12-27 19:24:33 +04:00
|
|
|
private func featurePreferenceView(acceptText: LocalizedStringKey? = nil) -> some View {
|
2022-12-23 13:10:00 +00:00
|
|
|
var r = Text(CIContent.preferenceText(feature, allowed, param) + " ")
|
2022-12-27 19:24:33 +04:00
|
|
|
.fontWeight(.light)
|
2024-07-03 22:42:13 +01:00
|
|
|
.foregroundColor(theme.colors.secondary)
|
2022-12-27 19:24:33 +04:00
|
|
|
if let acceptText {
|
|
|
|
r = r
|
|
|
|
+ Text(acceptText)
|
2022-12-23 13:10:00 +00:00
|
|
|
.fontWeight(.medium)
|
2024-07-03 22:42:13 +01:00
|
|
|
.foregroundColor(theme.colors.primary)
|
2024-11-27 19:01:16 +00:00
|
|
|
+ Text(verbatim: " ")
|
2022-12-23 13:10:00 +00:00
|
|
|
}
|
|
|
|
r = r + chatItem.timestampText
|
2022-12-27 19:24:33 +04:00
|
|
|
.fontWeight(.light)
|
2024-07-03 22:42:13 +01:00
|
|
|
.foregroundColor(theme.colors.secondary)
|
2022-12-23 13:10:00 +00:00
|
|
|
return r.font(.caption)
|
|
|
|
}
|
2022-12-22 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 19:24:33 +04:00
|
|
|
func allowFeatureToContact(_ contact: Contact, _ feature: ChatFeature, param: Int? = nil) {
|
2022-12-22 21:01:29 +00:00
|
|
|
Task {
|
|
|
|
do {
|
2022-12-27 19:24:33 +04:00
|
|
|
let prefs = contactUserPreferencesToPreferences(contact.mergedPreferences).setAllowed(feature, param: param)
|
2022-12-22 21:01:29 +00:00
|
|
|
if let toContact = try await apiSetContactPrefs(contactId: contact.contactId, preferences: prefs) {
|
|
|
|
await MainActor.run {
|
|
|
|
ChatModel.shared.updateContact(toContact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logger.error("allowFeatureToContact apiSetContactPrefs error: \(responseError(error))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CIFeaturePreferenceView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
let content = CIContent.rcvChatPreference(feature: .timedMessages, allowed: .yes, param: 30)
|
|
|
|
let chatItem = ChatItem(
|
|
|
|
chatDir: .directRcv,
|
2023-02-09 15:10:35 +04:00
|
|
|
meta: CIMeta.getSample(1, .now, content.text, .rcvRead),
|
2022-12-22 21:01:29 +00:00
|
|
|
content: content,
|
|
|
|
quotedItem: nil,
|
|
|
|
file: nil
|
|
|
|
)
|
2023-10-31 09:44:57 +00:00
|
|
|
CIFeaturePreferenceView(chat: Chat.sampleData, chatItem: chatItem, feature: ChatFeature.timedMessages, allowed: .yes, param: 30)
|
2022-12-22 21:01:29 +00:00
|
|
|
}
|
|
|
|
}
|