-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPasswordEditViewModel.swift
More file actions
91 lines (74 loc) · 2.2 KB
/
Copy pathPasswordEditViewModel.swift
File metadata and controls
91 lines (74 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// PasswordEditViewModel.swift
// NativeAppTemplate
//
// Created by Daisuke Adachi on 2025/06/15.
//
import SwiftUI
import Observation
@Observable
@MainActor
final class PasswordEditViewModel {
var currentPassword = ""
var password = ""
var passwordConfirmation = ""
var isUpdating = false
var shouldDismiss = false
private let accountPasswordRepository: AccountPasswordRepositoryProtocol
private let messageBus: MessageBus
init(
accountPasswordRepository: AccountPasswordRepositoryProtocol,
messageBus: MessageBus
) {
self.accountPasswordRepository = accountPasswordRepository
self.messageBus = messageBus
}
var isBusy: Bool {
isUpdating
}
var hasInvalidData: Bool {
if Utility.isBlank(currentPassword) ||
Utility.isBlank(password) ||
Utility.isBlank(passwordConfirmation) {
return true
}
if hasInvalidDataPassword {
return true
}
return false
}
var hasInvalidDataPassword: Bool {
if Utility.isBlank(password) {
return true
}
if password.count < .minimumPasswordLength {
return true
}
return false
}
var minimumPasswordLength: Int {
.minimumPasswordLength
}
func updatePassword() {
let whitespacesAndNewlines = CharacterSet.whitespacesAndNewlines
let theCurrentPassword = currentPassword.trimmingCharacters(in: whitespacesAndNewlines)
let thePassword = password.trimmingCharacters(in: whitespacesAndNewlines)
let thePasswordConfirmation = passwordConfirmation.trimmingCharacters(in: whitespacesAndNewlines)
Task {
isUpdating = true
do {
let updatePassword = UpdatePassword(
currentPassword: theCurrentPassword,
password: thePassword,
passwordConfirmation: thePasswordConfirmation
)
try await accountPasswordRepository.update(updatePassword: updatePassword)
messageBus.post(message: Message(level: .success, message: .passwordUpdated))
shouldDismiss = true
} catch {
messageBus.post(message: Message(level: .error, message: error.localizedDescription, autoDismiss: false))
}
isUpdating = false
}
}
}