yattee/Shared/Views/WelcomeScreen.swift

99 lines
3.4 KiB
Swift
Raw Normal View History

import Defaults
import Siesta
2021-10-17 23:06:00 +00:00
import SwiftUI
struct WelcomeScreen: View {
2021-11-28 14:37:55 +00:00
@Environment(\.presentationMode) private var presentationMode
2021-10-17 23:06:00 +00:00
@EnvironmentObject<AccountsModel> private var accounts
@State private var store = [ManifestedInstance]()
2021-10-17 23:06:00 +00:00
var body: some View {
VStack(alignment: .leading) {
2021-10-17 23:06:00 +00:00
Spacer()
2022-07-03 20:18:11 +00:00
Text("Welcome")
.frame(maxWidth: .infinity)
2021-10-17 23:06:00 +00:00
.font(.largeTitle)
.padding(.bottom, 10)
2022-07-03 20:18:11 +00:00
Text("Select location closest to you:")
.font(.subheadline)
2021-10-17 23:06:00 +00:00
ScrollView {
2022-07-03 20:18:11 +00:00
let countries = store.map(\.country).sorted().unique()
ForEach(countries, id: \.self) { country in
2021-10-17 23:06:00 +00:00
Button {
Defaults[.countryOfPublicInstances] = country
InstancesManifest.shared.setPublicAccount(country, accounts: accounts)
2021-11-28 14:37:55 +00:00
presentationMode.wrappedValue.dismiss()
2021-10-17 23:06:00 +00:00
} label: {
HStack(spacing: 10) {
if let flag = flag(country) {
Text(flag)
}
Text(country)
#if !os(tvOS)
.foregroundColor(.white)
#endif
.frame(maxWidth: .infinity, alignment: .leading)
2021-10-17 23:06:00 +00:00
}
#if os(tvOS)
.padding(8)
#else
.padding(4)
.background(RoundedRectangle(cornerRadius: 4).foregroundColor(Color.accentColor))
.padding(.bottom, 2)
#endif
}
.buttonStyle(.plain)
#if os(tvOS)
.padding(.horizontal, 10)
2021-10-17 23:06:00 +00:00
#endif
}
.padding(.horizontal, 30)
2021-10-17 23:06:00 +00:00
}
#if !os(tvOS)
OpenSettingsButton()
2022-07-03 20:18:11 +00:00
.foregroundColor(.white)
.padding(10)
.background(RoundedRectangle(cornerRadius: 4).foregroundColor(Color.accentColor))
.frame(maxWidth: .infinity)
#endif
2022-09-04 15:28:30 +00:00
Text("This information will be processed only on your device and used to connect you to the server in the specified country.".localized() + "\n" +
"It can be changed later in settings. You can use your own locations too.".localized())
2022-07-03 20:18:11 +00:00
.font(.caption)
.foregroundColor(.secondary)
}
.onAppear {
resource.load().onSuccess { response in
if let instances: [ManifestedInstance] = response.typedContent() {
store = instances
}
}
2021-10-17 23:06:00 +00:00
}
.padding()
2021-10-17 23:06:00 +00:00
#if os(macOS)
.frame(minWidth: 400, minHeight: 400)
#elseif os(tvOS)
.frame(maxWidth: 1000)
2021-10-17 23:06:00 +00:00
#endif
}
2021-11-28 14:37:55 +00:00
func flag(_ country: String) -> String? {
store.first { $0.country == country }?.flag
}
var resource: Resource {
InstancesManifest.shared.instancesList
2021-10-17 23:06:00 +00:00
}
}
struct WelcomeScreen_Previews: PreviewProvider {
static var previews: some View {
WelcomeScreen()
.injectFixtureEnvironmentObjects()
}
}