Skip to content

Commit

Permalink
[Feat] #473 - Add functions to ShowProfileViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
jim4020key committed Jul 24, 2023
1 parent ff46b6e commit aedd754
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
22 changes: 9 additions & 13 deletions HappyAnding/HappyAnding/ViewModel/ShowProfileViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@ import SwiftUI

final class ShowProfileViewModel: ObservableObject {

var shortcutsZipViewModel = ShortcutsZipViewModel.share
private let shortcutsZipViewModel = ShortcutsZipViewModel.share

@Published var user: User
@Published private(set) var shortcuts: [Shortcuts] = []
@Published private(set) var curations: [Curation] = []
@Published private(set) var userGrade = Image(systemName: "person.crop.circle.fill")
@Published var currentTab: Int = 0
@Published var animationAmount = 0.0
@Published private(set) var animationAmount = 0.0

init(data: User) {
self.user = data
fetchUserGrade()
fetchShortcuts()
fetchCurations()
}

private func fetchUserGrade() {
self.userGrade = shortcutsZipViewModel.fetchShortcutGradeImage(isBig: true, shortcutGrade: shortcutsZipViewModel.checkShortcutGrade(userID: user.id))
self.userGrade = shortcutsZipViewModel.fetchShortcutGradeImage(isBig: true, shortcutGrade: shortcutsZipViewModel.checkShortcutGrade(userID: data.id))
self.shortcuts = shortcutsZipViewModel.allShortcuts.filter { $0.author == data.id }
self.curations = shortcutsZipViewModel.fetchCurationByAuthor(author: data.id)
}

private func fetchShortcuts() {
self.shortcuts = shortcutsZipViewModel.allShortcuts.filter { $0.author == user.id }
func profileDidTap() {
self.animationAmount += 360
}

private func fetchCurations() {
self.curations = shortcutsZipViewModel.fetchCurationByAuthor(author: user.id)
func moveTab(to tab: Int) {
self.currentTab = tab
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ShowProfileView: View {
@StateObject var viewModel: ShowProfileViewModel

@Namespace var namespace

private let tabItems = [TextLiteral.showProfileViewShortcutTabTitle, TextLiteral.showProfileViewCurationTabTitle]

var body: some View {
Expand All @@ -24,7 +24,7 @@ struct ShowProfileView: View {
VStack(spacing: 8) {
Button {
withAnimation(.interpolatingSpring(stiffness: 10, damping: 3)) {
viewModel.animationAmount += 360
viewModel.profileDidTap()
}
} label: {
if viewModel.shortcuts.isEmpty {
Expand Down Expand Up @@ -67,7 +67,7 @@ struct ShowProfileView: View {
.toolbar(.visible, for: .tabBar)
.onAppear {
withAnimation(.interpolatingSpring(stiffness: 10, damping: 3)) {
viewModel.animationAmount += 360
viewModel.profileDidTap()
}
}
}
Expand All @@ -76,29 +76,29 @@ struct ShowProfileView: View {
var tabBarView: some View {
HStack(spacing: 20) {
ForEach(Array(zip(self.tabItems.indices, self.tabItems)), id: \.0) { index, name in
tabBarItem(string: name, tab: index)
tabBarItem(title: name, tabID: index)
}
}
.padding(.horizontal, 16)
.frame(height: 36)
.background(Color.shortcutsZipWhite)
}

private func tabBarItem(string: String, tab: Int) -> some View {
private func tabBarItem(title: String, tabID: Int) -> some View {
Button {
viewModel.currentTab = tab
viewModel.moveTab(to: tabID)
} label: {
VStack {
if viewModel.currentTab == tab {
Text(string)
if viewModel.currentTab == tabID {
Text(title)
.shortcutsZipHeadline()
.foregroundColor(.gray5)
Color.gray5
.frame(height: 2)
.matchedGeometryEffect(id: "underline", in: namespace, properties: .frame)

} else {
Text(string)
Text(title)
.shortcutsZipBody1()
.foregroundColor(.gray3)
Color.clear
Expand Down

0 comments on commit aedd754

Please sign in to comment.