-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingView.swift
More file actions
180 lines (168 loc) · 6.3 KB
/
Copy pathSettingView.swift
File metadata and controls
180 lines (168 loc) · 6.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// SettingView.swift
// DevLogPresentation
//
// Created by opfic on 5/6/25.
//
import SwiftUI
import DevLogDomain
struct SettingView: View {
@Environment(NavigationRouter<ProfileRoute>.self) private var router
@State var viewModel: SettingViewModel
var body: some View {
let connected = viewModel.state.isNetworkConnected
Form {
Section {
Button {
router.push(.theme)
} label: {
HStack {
Text(String(localized: "settings_theme"))
.foregroundStyle(Color.primary)
Spacer()
Text(viewModel.state.theme.localizedName)
.foregroundStyle(Color.gray)
}
}
Button {
router.push(.pushNotification)
} label: {
Text(String(localized: "settings_notifications"))
.foregroundStyle(connected ? Color.primary : Color.secondary)
}
.disabled(!connected)
let dirSize = viewModel.state.dirSize
Button {
viewModel.send(.tapRemoveCacheButton)
} label: {
HStack {
Text(String(localized: "settings_clear_temp_data"))
.foregroundStyle(dirSize == 0 ? Color.secondary : .primary)
Spacer()
Text(formatFileSize(bytes: dirSize))
.foregroundStyle(Color.secondary.opacity(dirSize == 0 ? 0 : 1))
}
}
.disabled(dirSize == 0)
}
Section {
if let appVersion = viewModel.appVersion {
HStack {
Text(String(localized: "settings_version"))
Spacer()
Text(appVersion)
}
}
if let policyString = viewModel.policyURL,
let url = URL(string: policyString) {
Link(destination: url) {
Text(String(localized: "settings_privacy_policy"))
.foregroundColor(Color.blue)
}
}
Button(action: {
if let appStoreString = viewModel.appstoreUrl,
let url = URL(string: appStoreString) {
UIApplication.shared.open(url)
}
}) {
VStack(alignment: .leading) {
Text(String(localized: "settings_join_beta"))
Text(String(localized: "settings_join_beta_subtitle"))
.foregroundStyle(Color.gray)
.font(.caption)
}
}
}
Section {
Button {
router.push(.account)
} label: {
Text(String(localized: "settings_account"))
}
.disabled(!connected)
Button(role: .destructive, action: {
viewModel.send(.setAlert(isPresented: true, type: .signOut))
}) {
Text(String(localized: "settings_sign_out"))
}
.disabled(!connected)
}
HStack {
Spacer()
Button(role: .destructive, action: {
viewModel.send(.setAlert(isPresented: true, type: .deleteAuth))
}) {
Text(String(localized: "settings_delete_account"))
.font(.headline)
}
.disabled(!connected)
Spacer()
}
}
.navigationTitle(String(localized: "nav_settings"))
.navigationBarTitleDisplayMode(.inline)
.alert(
viewModel.state.alertTitle,
isPresented: Binding(
get: { viewModel.state.showAlert },
set: { viewModel.send(.setAlert(isPresented: $0)) }
)) {
alertButtons
} message: {
Text(viewModel.state.alertMessage)
}
.overlay {
if viewModel.state.isLoading {
LoadingView()
}
}
.onAppear {
viewModel.send(.updateDirSize)
}
}
@ViewBuilder
private var alertButtons: some View {
switch viewModel.state.alertType {
case .signOut:
Button(String(localized: "common_cancel"), role: .cancel) {
viewModel.send(.setAlert(isPresented: false))
}
Button(String(localized: "common_confirm"), role: .destructive) {
viewModel.send(.tapSignOutButton)
}
case .deleteAuth:
Button(String(localized: "common_cancel"), role: .cancel) {
viewModel.send(.setAlert(isPresented: false))
}
Button(String(localized: "settings_delete_account_action"), role: .destructive) {
viewModel.send(.tapDeleteAuthButton)
}
case .removeCache:
Button(String(localized: "common_cancel"), role: .cancel) {
viewModel.send(.setAlert(isPresented: false))
}
Button(String(localized: "common_confirm"), role: .destructive) {
viewModel.send(.confirmRemoveCache)
}
case .error, .none:
Button(String(localized: "common_close"), role: .cancel) {
viewModel.send(.setAlert(isPresented: false))
}
}
}
private func formatFileSize(bytes: Int64) -> String {
let units = ["B", "KB", "MB", "GB"]
var value = Double(max(bytes, 0))
var unitIndex = 0
while 1024.0 <= value && unitIndex < units.count - 1 {
value /= 1024.0
unitIndex += 1
}
let truncated = floor(value * 100.0) / 100.0
let numberString = truncated.formatted(
.number.precision(.fractionLength(0...2))
)
return "\(numberString)\(units[unitIndex])"
}
}