Skip to content

Commit

Permalink
Merge pull request #176 from makinosp/develop
Browse files Browse the repository at this point in the history
Feat: Storing Data via AppStorage
  • Loading branch information
makinosp authored Oct 27, 2024
2 parents d83ac90 + 155c710 commit 5d5a541
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"location" : "https://github.com/makinosp/vrckit",
"state" : {
"branch" : "develop",
"revision" : "94cf3114e97353a9302339cd073b3f1468c6e1ee"
"revision" : "fab9dbea46bb5dc755eb815a9a9529ff971949cb"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion harmonie/Models/MainTabViewSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension MainTabViewSegment: CustomStringConvertible {
}

extension MainTabViewSegment: Identifiable {
var id: Int { hashValue }
var id: String { rawValue }
}

extension MainTabViewSegment {
Expand Down
2 changes: 2 additions & 0 deletions harmonie/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ enum Constants {
enum Keys: String {
case isSavedOnKeyChain
case username
case userData
case tabSelection
}

enum MaxCountInFavoriteList: Int, CustomStringConvertible {
Expand Down
6 changes: 3 additions & 3 deletions harmonie/ViewModels/AppViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class AppViewModel {
var step: Step = .initializing
var isPresentedAlert = false
var vrckError: VRCKitError?
var isLoggingIn = false
var isRequesting = false
var services: APIServiceUtil
var verifyType: VerifyType?
@ObservationIgnored var client: APIClient
Expand Down Expand Up @@ -82,9 +82,9 @@ final class AppViewModel {
}

func login() async -> Either<User, VerifyType>? {
defer { isLoggingIn = false }
defer { isRequesting = false }
var result: Either<User, VerifyType>?
isLoggingIn = true
isRequesting = true
do {
result = try await services.authenticationService.loginUserInfo()
} catch {
Expand Down
49 changes: 28 additions & 21 deletions harmonie/Views/Authentication/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,46 @@ struct LoginView: View {
@State private var password = ""
@State private var isPresentedSecurityPopover = false
@State private var isPresentedSavingPasswordPopover = false
@State private var isInInitializing = true
@State private var isRequesting = false
@State private var isReady = false
private let titleFont = "Avenir Next"

var body: some View {
@Bindable var appVM = appVM
NavigationStack {
if isInInitializing {
ProgressScreen()
} else {
VStack(spacing: 32) {
title
VStack(spacing: 16) {
loginFields
keychainToggle
enterButton
}
}
.frame(maxWidth: 560)
.padding(.horizontal, 32)
.navigationDestination(item: $appVM.verifyType) { _ in
OtpView()
.navigationBarBackButtonHidden()
VStack(spacing: 32) {
title
VStack(spacing: 16) {
loginFields
keychainToggle
enterButton
}
}
.frame(maxWidth: 560)
.padding(.horizontal, 32)
.navigationDestination(item: $appVM.verifyType) { _ in
OtpView()
}
}
.ignoresSafeArea(.keyboard)
.overlay {
if !isReady {
ProgressScreen()
}
}
.task {
defer { isInInitializing = false }
guard isSavedOnKeyChain, !username.isEmpty else { return }
defer { isReady = true }
guard isSettedLocalData else { return }
guard let password = await KeychainUtil.shared.getPassword(for: username) else { return }
self.password = password
await appVM.login(credential: cledential, isSavedOnKeyChain: isSavedOnKeyChain)
}
}

private var isSettedLocalData: Bool {
isSavedOnKeyChain && !username.isEmpty
}

private var cledential: Credential {
Credential(username: username, password: password)
}
Expand Down Expand Up @@ -121,9 +126,11 @@ struct LoginView: View {

private var enterButton: some View {
AsyncButton {
defer { isRequesting = false }
isRequesting = true
await appVM.login(credential: cledential, isSavedOnKeyChain: isSavedOnKeyChain)
} label: {
if appVM.isLoggingIn {
if isRequesting {
ProgressView()
} else {
Text("Enter")
Expand All @@ -135,7 +142,7 @@ struct LoginView: View {
}

private var isDisabledEnterButton: Bool {
appVM.isLoggingIn || username.count < 4 || password.count < 8
isRequesting || username.count < 4 || password.count < 8
}
}

Expand Down
4 changes: 1 addition & 3 deletions harmonie/Views/Favorite/FavoritesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ struct FavoritesView: View {
}
}
.overlay {
if appVM.isLoggingIn {
ProgressScreen()
} else if favoriteVM.isSelectedEmpty {
if favoriteVM.isSelectedEmpty {
ContentUnavailableView {
Label("No Favorites", systemImage: IconSet.favorite.systemName)
}
Expand Down
7 changes: 3 additions & 4 deletions harmonie/Views/Friend/FriendsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
// Created by makinosp on 2024/09/16.
//

import MemberwiseInit
import SwiftUI

@MemberwiseInit
struct FriendsListView: View {
@Environment(\.isSearching) private var isSearching
@Environment(AppViewModel.self) var appVM
@Environment(FriendViewModel.self) var friendVM
@Environment(FavoriteViewModel.self) var favoriteVM
@InitWrapper(.internal, type: Binding<String?>)
@Binding private var selected: String?

init(selected: Binding<String?>) {
_selected = selected
}

var body: some View {
List(friendVM.filterResultFriends, selection: $selected) { friend in
Label {
Expand Down
4 changes: 1 addition & 3 deletions harmonie/Views/Location/LocationsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ struct LocationsView: View {
inPrivateInstance
}
.overlay {
if friendVM.isRequesting {
ProgressScreen()
} else if friendVM.friendsLocations.isEmpty {
if friendVM.friendsLocations.isEmpty {
ContentUnavailableView {
Label("No Friend Location", systemImage: IconSet.friends.systemName)
}
Expand Down
36 changes: 26 additions & 10 deletions harmonie/Views/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ extension MainTabViewSegment {
}

@available(iOS 18, *)
var tab: Tab<Never, some View, DefaultTabLabel> {
Tab(description, systemImage: icon.systemName) { content }
var tab: some TabContent {
Tab(description, systemImage: icon.systemName, value: self) { content }
}
}

Expand All @@ -34,6 +34,8 @@ struct MainTabView: View {
@Environment(AppViewModel.self) var appVM: AppViewModel
@Environment(FriendViewModel.self) var friendVM: FriendViewModel
@Environment(FavoriteViewModel.self) var favoriteVM: FavoriteViewModel
@AppStorage(Constants.Keys.tabSelection.rawValue) private var selection: MainTabViewSegment = .social
@AppStorage(Constants.Keys.userData.rawValue) private var userData = ""

var body: some View {
Group {
Expand All @@ -51,17 +53,31 @@ struct MainTabView: View {
Task { await fetchFavoritesTask() }
}
.onChange(of: scenePhase) {
if scenePhase == .active {
Task {
if await appVM.login() == nil { return }
appVM.step = .loggingIn
}
scenePhaseHandler(scenePhase)
}
}

private func scenePhaseHandler(_ scenePhase: ScenePhase) {
switch scenePhase {
case .active:
restoreUserData()
Task {
if await appVM.login() == nil { return }
}
case .background, .inactive:
guard let user = appVM.user else { return }
userData = user.rawValue
@unknown default: break
}
}

private func restoreUserData() {
guard let user = User(rawValue: userData) else { return }
appVM.user = user
}

private var tabViewLegacy: some View {
TabView {
TabView(selection: $selection) {
ForEach(MainTabViewSegment.allCases) { tabSegment in
tabSegment.content
.tag(tabSegment)
Expand All @@ -73,8 +89,8 @@ struct MainTabView: View {
}

@available(iOS 18, *) private var tabView: some View {
TabView {
ForEach(MainTabViewSegment.allCases) { $0.tab }
TabView(selection: $selection) {
return ForEach(MainTabViewSegment.allCases) { $0.tab }
}
.tabViewStyle(.sidebarAdaptable)
}
Expand Down

0 comments on commit 5d5a541

Please sign in to comment.