Skip to content

Commit

Permalink
Merge pull request #1824 from nextcloud/user-status-message-setting-f…
Browse files Browse the repository at this point in the history
…ixes

User status message setting fixes
  • Loading branch information
SystemKeeper authored Oct 9, 2024
2 parents b790cec + 1e746e0 commit 34a8ab4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
8 changes: 4 additions & 4 deletions NextcloudTalk/EmojiTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import UIKit
import SwiftUI
import Dynamic

struct EmojiTextFieldWrapper: UIViewRepresentable {
struct SingleEmojiTextFieldWrapper: UIViewRepresentable {
@State var placeholder: String
@Binding var text: String

Expand All @@ -22,14 +22,14 @@ struct EmojiTextFieldWrapper: UIViewRepresentable {
uiView.placeholder = placeholder
}

func makeCoordinator() -> EmojiTextFieldWrapper.Coordinator {
func makeCoordinator() -> SingleEmojiTextFieldWrapper.Coordinator {
Coordinator(parent: self)
}

class Coordinator: NSObject, UITextFieldDelegate {
var parent: EmojiTextFieldWrapper
var parent: SingleEmojiTextFieldWrapper

init(parent: EmojiTextFieldWrapper) {
init(parent: SingleEmojiTextFieldWrapper) {
self.parent = parent
}

Expand Down
64 changes: 28 additions & 36 deletions NextcloudTalk/UserStatusMessageSwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ struct UserStatusMessageSwiftUIView: View {
@Binding var changed: Bool
@State var showClearAtAlert: Bool = false

@State private var changedStatusFromPredefined: Bool = false
@State private var customStatusSelected: Bool = false
@State private var selectedPredifinedStatus: NKUserStatus?
@State private var statusPredefinedStatuses: [NKUserStatus] = []
@State private var selectedPredefinedMessageId: String?
@State private var predefinedStatuses: [NKUserStatus] = []

@State private var selectedIcon: String = ""
@State private var selectedMessage: String = ""
@State private var selectedClearAt: Double = 0
@State private var selectedClearAtString: String = ""
@State private var changedSmth: Bool = false

@State private var userHasStatusSet: Bool = false

@State private var isLoading: Bool = true
@FocusState private var textFieldIsFocused: Bool
Expand All @@ -47,39 +46,30 @@ struct UserStatusMessageSwiftUIView: View {
List {
Section(header: Text(NSLocalizedString("What is your status?", comment: ""))) {
HStack(spacing: 10) {
EmojiTextFieldWrapper(placeholder: "😀", text: $selectedIcon)
SingleEmojiTextFieldWrapper(placeholder: "😀", text: $selectedIcon)
.frame(maxWidth: 23)
.opacity(selectedIcon.isEmpty ? 0.5 : 1.0)
.onChange(of: selectedIcon) { newString in
changedSmth = true
customStatusSelected = !changedStatusFromPredefined
changedStatusFromPredefined = false
if newString.count > 1 {
selectedIcon = String(newString.first!)
}
.onChange(of: selectedIcon) { _ in
selectedPredefinedMessageId = nil
}
.tint(.primary)
.focused($textFieldIsFocused)
Divider()
TextField(NSLocalizedString("What is your status?", comment: ""), text: $selectedMessage)
.onChange(of: selectedMessage) { _ in
changedSmth = true
customStatusSelected = !changedStatusFromPredefined
changedStatusFromPredefined = false
selectedPredefinedMessageId = nil
}
.tint(.primary)
.focused($textFieldIsFocused)
}
}
Section {
ForEach(statusPredefinedStatuses, id: \.id) { status in
ForEach(predefinedStatuses, id: \.id) { status in
Button(action: {
changedStatusFromPredefined = true
selectedPredifinedStatus = status
customStatusSelected = false
selectedIcon = selectedPredifinedStatus!.icon ?? "Empty"
selectedMessage = selectedPredifinedStatus!.message ?? "Empty"
selectedClearAt = selectedPredifinedStatus!.clearAt?.timeIntervalSince1970 ?? 0
selectedPredefinedMessageId = status.id
selectedIcon = status.icon ?? ""
selectedMessage = status.message ?? ""
selectedClearAt = status.clearAt?.timeIntervalSince1970 ?? 0
selectedClearAtString = getPredefinedClearStatusText(clearAt: status.clearAt, clearAtTime: status.clearAtTime, clearAtType: status.clearAtType)
setClearAt(clearAt: selectedClearAtString)
}) {

Check warning on line 75 in NextcloudTalk/UserStatusMessageSwiftUIView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Multiple Closures with Trailing Closure Violation: Trailing closure syntax should not be used when passing more than one closure argument (multiple_closures_with_trailing_closure)
Expand Down Expand Up @@ -119,11 +109,11 @@ struct UserStatusMessageSwiftUIView: View {
comment: ""),
action: clearActiveUserStatus,
style: .tertiary, height: 40,
disabled: Binding.constant(selectedMessage.isEmpty && selectedIcon.isEmpty))
disabled: Binding.constant(!userHasStatusSet))
NCButtonSwiftUI(title: NSLocalizedString("Set status message", comment: ""),
action: setActiveUserStatus,
style: .primary, height: 40,
disabled: Binding.constant(selectedMessage.isEmpty || changedSmth == false))
disabled: Binding.constant(selectedMessage.isEmpty && selectedIcon.isEmpty))
.padding(.bottom, 16)
}
} else {
Expand All @@ -133,12 +123,12 @@ struct UserStatusMessageSwiftUIView: View {
comment: ""),
action: clearActiveUserStatus,
style: .tertiary, height: 40,
disabled: Binding.constant(selectedMessage.isEmpty && selectedIcon.isEmpty))
disabled: Binding.constant(!userHasStatusSet))
.padding(.bottom, 16)
NCButtonSwiftUI(title: NSLocalizedString("Set status message", comment: ""),
action: setActiveUserStatus,
style: .primary, height: 40,
disabled: Binding.constant(selectedMessage.isEmpty || changedSmth == false))
disabled: Binding.constant(selectedMessage.isEmpty && selectedIcon.isEmpty))
.padding(.bottom, 16)
Spacer()
}
Expand Down Expand Up @@ -192,17 +182,19 @@ struct UserStatusMessageSwiftUIView: View {
func getStatus() {
isLoading = true
NCAPIController.sharedInstance().setupNCCommunication(for: NCDatabaseManager.sharedInstance().activeAccount())
NextcloudKit.shared.getUserStatus { _, clearAt, icon, message, _, _, _, _, _, _, error in
NextcloudKit.shared.getUserStatus { _, clearAt, icon, message, messageId, _, _, _, _, _, error in
if error.errorCode == 0 {
selectedIcon = icon ?? "😀"
userHasStatusSet = !(icon?.isEmpty ?? true) || !(message?.isEmpty ?? true)
selectedIcon = icon ?? ""
selectedMessage = message ?? ""
selectedPredefinedMessageId = messageId
selectedClearAt = clearAt?.timeIntervalSince1970 ?? 0
selectedClearAtString = getPredefinedClearStatusText(clearAt: clearAt, clearAtTime: nil, clearAtType: nil)
}
}
NextcloudKit.shared.getUserStatusPredefinedStatuses { _, userStatuses, _, error in
if error.errorCode == 0 {
statusPredefinedStatuses = userStatuses!
predefinedStatuses = userStatuses ?? []
withAnimation {
isLoading = false
}
Expand All @@ -211,8 +203,8 @@ struct UserStatusMessageSwiftUIView: View {
}

func setActiveUserStatus() {
if !customStatusSelected, let selectedPredifinedStatus {
NextcloudKit.shared.setCustomMessagePredefined(messageId: selectedPredifinedStatus.id!, clearAt: selectedClearAt) { _, error in
if let selectedPredefinedMessageId {
NextcloudKit.shared.setCustomMessagePredefined(messageId: selectedPredefinedMessageId, clearAt: selectedClearAt) { _, error in
if error.errorCode == 0 {
dismiss()
changed.toggle()
Expand All @@ -222,7 +214,8 @@ struct UserStatusMessageSwiftUIView: View {
}
}
} else {
NextcloudKit.shared.setCustomMessageUserDefined(statusIcon: selectedIcon.isEmpty ? "😀" : selectedIcon, message: selectedMessage, clearAt: selectedClearAt) { _, error in
let statusIcon = selectedIcon.isEmpty ? nil : selectedIcon
NextcloudKit.shared.setCustomMessageUserDefined(statusIcon: statusIcon, message: selectedMessage, clearAt: selectedClearAt) { _, error in
if error.errorCode == 0 {
dismiss()
changed.toggle()
Expand All @@ -246,8 +239,8 @@ struct UserStatusMessageSwiftUIView: View {
}

func triggerErrorAlert(title: String, message: String) {
errorAlertTitle = NSLocalizedString("Could not clear status message", comment: "")
errorAlertMessage = NSLocalizedString("Could not clear status message", comment: "")
errorAlertTitle = title
errorAlertMessage = message
showErrorAlert.toggle()
}

Expand Down Expand Up @@ -285,7 +278,6 @@ struct UserStatusMessageSwiftUIView: View {
func setClearAt(clearAt: String) {
selectedClearAt = getClearAt(clearAt)
selectedClearAtString = clearAt
customStatusSelected = true
}

func getPredefinedClearStatusText(clearAt: NSDate?, clearAtTime: String?, clearAtType: String?) -> String {

Check warning on line 283 in NextcloudTalk/UserStatusMessageSwiftUIView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 18 (cyclomatic_complexity)
Expand Down

0 comments on commit 34a8ab4

Please sign in to comment.