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
|
/**
* TrackerMap
* Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import SwiftUI
import shared
class SessionState: ObservableObject {
@Published var loggedIn = false
}
struct RootView: View {
@StateObject private var rootViewModel = RootViewModel()
@State private var username = ""
@State private var password = ""
@State private var server: String
init() {
server = UserDefaults.standard.string(forKey: "server-url")
?? NSLocalizedString("app-server-url", comment: "")
}
var body: some View {
Group {
VStack {
if (rootViewModel.networkAvailable != true) {
OfflineBanner()
}
if rootViewModel.showLoadingView {
LoadingView()
.frame(minHeight: 0, maxHeight: .infinity)
} else {
switch rootViewModel.loginState {
case is SessionController.LoginStateSuccess:
UnitsView()
case is SessionController.LoginStateLoading:
LoadingView()
.frame(minHeight: 0, maxHeight: .infinity)
default:
LoginContentView(username: $username,
password: $password,
server: $server,
onLogin: rootViewModel.login)
}
}
}
}.environmentObject(rootViewModel)
}
}
struct LoginContentView: View {
@Binding var username: String
@Binding var password: String
@Binding var server: String
let onLogin: (String, String, String) -> Void
var body: some View {
VStack {
Spacer()
LoginTitleView()
Spacer()
LoginForm(username: $username,
password: $password,
server: $server)
Button(action: {
self.onLogin(username, password, server)
}) {
Text("login")
.font(.system(size: 18))
.bold()
}
.padding([.top, .bottom], 30)
Spacer()
}
.padding()
}
}
// Source: https://github.com/niochat/nio
struct LoginTitleView: View {
var body: some View {
return VStack {
Text("app-name")
.font(.title)
.bold()
}
}
}
struct LoginForm: View {
@Binding var username: String
@Binding var password: String
@Binding var server: String
var body: some View {
VStack {
FormTextField(title: "username", text: $username)
FormTextField(title: "password", text: $password, isSecure: true)
#if os(macOS)
FormTextField(title: "server-url", text: $server)
#else
FormTextField(title: "server-url", text: $server, keyboardType: .URL)
#endif
}
}
}
private struct FormTextField: View {
@Environment(\.colorScheme) private var colorScheme
let title: LocalizedStringKey
@Binding var text: String
var onEditingChanged: ((Bool) -> Void)?
#if os(macOS)
#else
var keyboardType: UIKeyboardType = .default
#endif
var isSecure = false
var body: some View {
ZStack {
Capsule(style: .continuous)
.foregroundColor(colorScheme == .light
? Color(#colorLiteral(red: 0.9395676295, green: 0.9395676295, blue: 0.9395676295, alpha: 1))
: Color(#colorLiteral(red: 0.2293992357, green: 0.2293992357, blue: 0.2293992357, alpha: 1)))
.frame(height: 50)
if isSecure {
SecureField(title, text: $text)
.padding()
.textContentType(.password)
} else {
#if os(macOS)
TextField(title, text: $text, onEditingChanged: onEditingChanged ?? { _ in })
.padding()
.autocapitalization(.none)
.disableAutocorrection(true)
#else
TextField(title, text: $text, onEditingChanged: onEditingChanged ?? { _ in })
.padding()
.autocapitalization(.none)
.disableAutocorrection(true)
.keyboardType(keyboardType)
#endif
}
}
.padding(.horizontal)
.frame(maxWidth: 400)
}
}
|