2022-12-03 10:45:54 +01:00
|
|
|
//
|
|
|
|
// ProfileView.swift
|
|
|
|
// BeReal
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 03/12/2022.
|
|
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2022-12-12 02:52:25 +01:00
|
|
|
import DataModels
|
2022-12-20 03:33:03 +01:00
|
|
|
import KeychainStorage
|
2022-12-03 10:45:54 +01:00
|
|
|
import SwiftUI
|
|
|
|
|
2022-12-03 11:37:42 +01:00
|
|
|
public struct ProfileView: View {
|
|
|
|
|
2022-12-12 21:30:16 +01:00
|
|
|
// MARK: Environments
|
|
|
|
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
2022-12-20 03:33:03 +01:00
|
|
|
// MARK: Storages
|
|
|
|
|
|
|
|
@KeychainStorage(key: .KeychainStorage.account) var account: Account?
|
|
|
|
|
2022-12-03 11:45:54 +01:00
|
|
|
// MARK: Properties
|
|
|
|
|
2022-12-12 02:52:25 +01:00
|
|
|
private let user: User?
|
|
|
|
private let logout: ActionClosure
|
|
|
|
|
|
|
|
private let stringAdapter = StringAdapter()
|
|
|
|
private let dateAdapter = DateAdapter()
|
2022-12-03 11:45:54 +01:00
|
|
|
|
2022-12-03 11:37:42 +01:00
|
|
|
// MARK: Initialisers
|
|
|
|
|
2022-12-12 02:52:25 +01:00
|
|
|
public init(
|
|
|
|
user: User?,
|
|
|
|
logout: @escaping ActionClosure
|
|
|
|
) {
|
|
|
|
self.user = user
|
|
|
|
self.logout = logout
|
2022-12-03 11:45:54 +01:00
|
|
|
}
|
2022-12-03 11:37:42 +01:00
|
|
|
|
|
|
|
// MARK: Body
|
|
|
|
|
|
|
|
public var body: some View {
|
2022-12-12 21:43:24 +01:00
|
|
|
DismissableView {
|
2022-12-12 21:30:16 +01:00
|
|
|
ClearBackgroundList {
|
|
|
|
Section {
|
|
|
|
Image.photo
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
|
|
|
.frame(width: 160)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
}
|
|
|
|
.listRowBackground(Color.clear)
|
|
|
|
|
2022-12-15 02:29:59 +01:00
|
|
|
Group {
|
|
|
|
ProfileSection(
|
|
|
|
header: "profile.sections.names.header.text",
|
|
|
|
items: [
|
|
|
|
.init(
|
|
|
|
key: "profile.sections.names.label.first_name.text",
|
|
|
|
value: stringAdapter(value: user?.profile.firstName)
|
|
|
|
),
|
|
|
|
.init(
|
|
|
|
key: "profile.sections.names.label.last_name.text",
|
|
|
|
value: stringAdapter(value: user?.profile.lastName)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
ProfileSection(
|
|
|
|
header: "profile.sections.root_info.header.text",
|
|
|
|
items: [
|
|
|
|
.init(
|
|
|
|
key: "profile.sections.root_info.label.identifier.text",
|
|
|
|
value: stringAdapter(value: user?.rootFolder.id)
|
|
|
|
),
|
|
|
|
.init(
|
|
|
|
key: "profile.sections.root_info.label.name.text",
|
|
|
|
value: stringAdapter(value: user?.rootFolder.name)
|
|
|
|
),
|
|
|
|
.init(
|
|
|
|
key: "profile.sections.root_info.label.last_modified.text",
|
|
|
|
value: dateAdapter(value: user?.rootFolder.lastModifiedAt)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.listRowBackground(Color.secondary.colorInvert())
|
2022-12-12 21:30:16 +01:00
|
|
|
|
|
|
|
Section {
|
|
|
|
Button {
|
2022-12-20 03:33:03 +01:00
|
|
|
Task { await logUserOut() }
|
2022-12-12 21:30:16 +01:00
|
|
|
} label: {
|
|
|
|
Text(
|
|
|
|
"profile.button.log_out.text",
|
|
|
|
bundle: .module
|
|
|
|
)
|
2022-12-15 02:46:01 +01:00
|
|
|
.font(.body)
|
2022-12-12 21:30:16 +01:00
|
|
|
.fontWeight(.semibold)
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
}
|
2022-12-15 02:29:59 +01:00
|
|
|
.tint(.red)
|
|
|
|
.controlSize(.large)
|
2022-12-12 21:30:16 +01:00
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
.buttonBorderShape(.roundedRectangle(radius: 8))
|
|
|
|
}
|
|
|
|
.listRowBackground(Color.clear)
|
2022-12-03 10:45:54 +01:00
|
|
|
}
|
2022-12-15 02:29:59 +01:00
|
|
|
.background(Color.primary.colorInvert())
|
2022-12-03 10:45:54 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 02:52:25 +01:00
|
|
|
|
2022-12-03 10:45:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-20 03:33:03 +01:00
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension ProfileView {
|
|
|
|
func logUserOut() async {
|
|
|
|
account = nil
|
|
|
|
|
|
|
|
logout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:45:54 +01:00
|
|
|
// MARK: - Images+Constants
|
|
|
|
|
|
|
|
private extension Image {
|
|
|
|
static let photo = Image(systemName: "person.crop.circle.fill")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
struct ProfileView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-12-12 02:52:25 +01:00
|
|
|
ProfileView(user: .init(
|
|
|
|
profile: .init(
|
|
|
|
firstName: "Some first name...",
|
|
|
|
lastName: "Some last name..."
|
|
|
|
),
|
|
|
|
rootFolder: .init(
|
|
|
|
id: "1234567890",
|
|
|
|
name: "Some folder name...",
|
|
|
|
lastModifiedAt: .now
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// closure for logout action.
|
2022-12-03 11:45:54 +01:00
|
|
|
}
|
2022-12-03 10:45:54 +01:00
|
|
|
}
|
|
|
|
}
|