From 78a38cb0806ef4f37359ac3f6bd9e4749d3cfe7c Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:37:49 +0400 Subject: [PATCH] ios: group welcome message byte limit (#3751) * ios: group welcome message character limit * confirmation dialogue key * use chatJsonLength * text * change footer --------- Co-authored-by: Evgeny Poberezkin --- .../Views/Chat/Group/GroupWelcomeView.swift | 24 ++++++++++++++----- apps/ios/SimpleXChat/API.swift | 5 ++++ apps/ios/SimpleXChat/SimpleX.h | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/Group/GroupWelcomeView.swift b/apps/ios/Shared/Views/Chat/Group/GroupWelcomeView.swift index c69cc526d..d6dbf06ef 100644 --- a/apps/ios/Shared/Views/Chat/Group/GroupWelcomeView.swift +++ b/apps/ios/Shared/Views/Chat/Group/GroupWelcomeView.swift @@ -18,6 +18,8 @@ struct GroupWelcomeView: View { @FocusState private var keyboardVisible: Bool @State private var showSaveDialog = false + let maxByteCount = 1200 + var body: some View { VStack { if groupInfo.canEdit { @@ -29,9 +31,12 @@ struct GroupWelcomeView: View { showSaveDialog = true } }) - .confirmationDialog("Save welcome message?", isPresented: $showSaveDialog) { - Button("Save and update group profile") { - save() + .confirmationDialog( + welcomeTextFitsLimit() ? "Save welcome message?" : "Welcome message is too long", + isPresented: $showSaveDialog + ) { + if welcomeTextFitsLimit() { + Button("Save and update group profile") { save() } } Button("Exit without saving") { dismiss() } } @@ -53,7 +58,7 @@ struct GroupWelcomeView: View { private func textPreview() -> some View { messageText(welcomeText, parseSimpleXMarkdown(welcomeText), nil, showSecrets: false) - .frame(minHeight: 140, alignment: .topLeading) + .frame(minHeight: 130, alignment: .topLeading) .frame(maxWidth: .infinity, alignment: .leading) } @@ -73,7 +78,7 @@ struct GroupWelcomeView: View { } .padding(.horizontal, -5) .padding(.top, -8) - .frame(height: 140, alignment: .topLeading) + .frame(height: 130, alignment: .topLeading) .frame(maxWidth: .infinity, alignment: .leading) } } else { @@ -92,6 +97,9 @@ struct GroupWelcomeView: View { } .disabled(welcomeText.isEmpty) copyButton() + } footer: { + Text(!welcomeTextFitsLimit() ? "Message too large" : "") + .foregroundColor(.red) } Section { @@ -112,13 +120,17 @@ struct GroupWelcomeView: View { Button("Save and update group profile") { save() } - .disabled(welcomeTextUnchanged()) + .disabled(welcomeTextUnchanged() || !welcomeTextFitsLimit()) } private func welcomeTextUnchanged() -> Bool { welcomeText == groupInfo.groupProfile.description || (welcomeText == "" && groupInfo.groupProfile.description == nil) } + private func welcomeTextFitsLimit() -> Bool { + chatJsonLength(welcomeText) <= maxByteCount + } + private func save() { Task { do { diff --git a/apps/ios/SimpleXChat/API.swift b/apps/ios/SimpleXChat/API.swift index ab069f24c..c0bb29892 100644 --- a/apps/ios/SimpleXChat/API.swift +++ b/apps/ios/SimpleXChat/API.swift @@ -105,6 +105,11 @@ public func parseSimpleXMarkdown(_ s: String) -> [FormattedText]? { return nil } +public func chatJsonLength(_ s: String) -> Int { + var c = s.cString(using: .utf8)! + return Int(chat_json_length(&c)) +} + struct ParsedMarkdown: Decodable { var formattedText: [FormattedText]? } diff --git a/apps/ios/SimpleXChat/SimpleX.h b/apps/ios/SimpleXChat/SimpleX.h index c49d10451..153365424 100644 --- a/apps/ios/SimpleXChat/SimpleX.h +++ b/apps/ios/SimpleXChat/SimpleX.h @@ -25,6 +25,7 @@ extern char *chat_parse_markdown(char *str); extern char *chat_parse_server(char *str); extern char *chat_password_hash(char *pwd, char *salt); extern char *chat_valid_name(char *name); +extern int chat_json_length(char *str); extern char *chat_encrypt_media(chat_ctrl ctl, char *key, char *frame, int len); extern char *chat_decrypt_media(char *key, char *frame, int len);