Skip to content

Commit

Permalink
[Feat] #524 - MyPageViewModel 적용
Browse files Browse the repository at this point in the history
- 섹션 접힘 유무 UserDefaults 저장
- 섹션 최대 노출 셀 수 변경
  • Loading branch information
JMM00 committed Apr 20, 2024
1 parent 947638a commit 72a7d9a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 49 deletions.
12 changes: 12 additions & 0 deletions HappyAnding/HappyAnding.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
F9A86DA62A0B54ED00405E12 /* UserNameCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9A86DA52A0B54ED00405E12 /* UserNameCell.swift */; };
F9AC2BB62935201C00165820 /* CheckUpdateVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9AC2BB52935201C00165820 /* CheckUpdateVersion.swift */; };
F9AFF6E32A59153B00FFFFAD /* WriteShortcutViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9AFF6E22A59153B00FFFFAD /* WriteShortcutViewModel.swift */; };
F9B99F262BCD73A10094F47B /* MyPageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9B99F252BCD73A10094F47B /* MyPageViewModel.swift */; };
F9BA11D329A389EA00176807 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = F9BA11D229A389EA00176807 /* GoogleService-Info.plist */; };
F9BA11D429A389EA00176807 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = F9BA11D229A389EA00176807 /* GoogleService-Info.plist */; };
F9CAEF832914855900224B0A /* Date+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9CAEF822914855900224B0A /* Date+String.swift */; };
Expand Down Expand Up @@ -355,6 +356,7 @@
F9AC2BB52935201C00165820 /* CheckUpdateVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckUpdateVersion.swift; sourceTree = "<group>"; };
F9AC2BB92935D34C00165820 /* Image+View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+View.swift"; sourceTree = "<group>"; };
F9AFF6E22A59153B00FFFFAD /* WriteShortcutViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteShortcutViewModel.swift; sourceTree = "<group>"; };
F9B99F252BCD73A10094F47B /* MyPageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageViewModel.swift; sourceTree = "<group>"; };
F9BA11D229A389EA00176807 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
F9CAEF822914855900224B0A /* Date+String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+String.swift"; sourceTree = "<group>"; };
F9DB8EB82939853D00516CE1 /* ShareExtensionValidationTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareExtensionValidationTextField.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -706,6 +708,7 @@
A0F822AA2910B8B900AF4448 /* ViewModel */ = {
isa = PBXGroup;
children = (
F9B99F242BCD738B0094F47B /* MyPageViewModels */,
8788E1A12A484518007C3852 /* ExploreShortcutViewModels */,
8788E1A32A484533007C3852 /* ReadShortcutViewModels */,
8788E19E2A483FDF007C3852 /* ExploreCurationViewModels */,
Expand Down Expand Up @@ -801,6 +804,14 @@
path = SettingViews;
sourceTree = "<group>";
};
F9B99F242BCD738B0094F47B /* MyPageViewModels */ = {
isa = PBXGroup;
children = (
F9B99F252BCD73A10094F47B /* MyPageViewModel.swift */,
);
path = MyPageViewModels;
sourceTree = "<group>";
};
F9DB8ECB293B30EC00516CE1 /* Recovered References */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1084,6 +1095,7 @@
F9136EB6293612310034AAB2 /* ShortcutsZipView.swift in Sources */,
87E99CB128FFF273009B691F /* WriteCurationSetView.swift in Sources */,
4D61A767291E1EE8000EF531 /* NavigationViewModel.swift in Sources */,
F9B99F262BCD73A10094F47B /* MyPageViewModel.swift in Sources */,
4D93D0752A61D0D10042CBA8 /* ReadShortcutViewModel.swift in Sources */,
F96D45BD29816578000C2441 /* StickyHeader.swift in Sources */,
87E99CEE29080D33009B691F /* User.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// MyPageViewModel.swift
// HappyAnding
//
// Created by JeonJimin on 4/15/24.
//

import Foundation
import Combine

final class MyPageViewModel: ObservableObject {
private let shortcutsZipViewModel = ShortcutsZipViewModel.share
private var cancellables = Set<AnyCancellable>()

@Published var myShortcuts: [Shortcuts] = []
@Published var myDownloadShortcuts: [Shortcuts] = []
@Published var myLovingShortcuts: [Shortcuts] = []

@Published var isMyDownloadShortcutFolded = false {
didSet {
UserDefaults.standard.set(isMyDownloadShortcutFolded, forKey: "isMyDownloadShortcutFolded")
}
}
@Published var isMyLovingShortcutFolded = false {
didSet {
UserDefaults.standard.set(isMyLovingShortcutFolded, forKey: "isMyLovingShortcutFolded")
}
}

init() {
shortcutsZipViewModel.$shortcutsMadeByUser
.sink { [weak self] in self?.myShortcuts = $0 }
.store(in: &cancellables)

shortcutsZipViewModel.$shortcutsUserDownloaded
.sink { [weak self] in self?.myDownloadShortcuts = $0 }
.store(in: &cancellables)

shortcutsZipViewModel.$shortcutsUserLiked
.sink { [weak self] in self?.myLovingShortcuts = $0 }
.store(in: &cancellables)

self.isMyDownloadShortcutFolded = UserDefaults.standard.bool(forKey: "isMyDownloadShortcutFolded")
self.isMyLovingShortcutFolded = UserDefaults.standard.bool(forKey: "isMyLovingShortcutFolded")
}

func fetchUserInfo() -> String {
shortcutsZipViewModel.userInfo?.nickname ?? TextLiteral.defaultUser
}
}
78 changes: 30 additions & 48 deletions HappyAnding/HappyAnding/Views/MyPageViews/MyPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,29 @@
import SwiftUI

struct MyPageView: View {

@EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel

@AppStorage("useWithoutSignIn") var useWithoutSignIn: Bool = false

@State var isTappedUserGradeButton = false

@StateObject var viewModel: MyPageViewModel

var body: some View {
ScrollView {
ScrollView(showsIndicators: false) {
VStack(spacing: 12) {

//MARK: - 사용자 프로필
Button {
//프로필 설정 페이지 연걸
} label: {
HStack {
//TODO: 프로필 이미지 - 등급 시스템과 동일 추후 이미지 연결
Image("profile_ex")
.resizable()
.scaledToFit()
.frame(width: 96, height: 96)

VStack(spacing: 9) {
HStack {
//뱃지 영역 ExploreShortcutView 머지 후 Seal 이용하기
Image(systemName: "seal")
.resizable()
.scaledToFit()
.frame(width: 30)
Image(systemName: "seal")
.resizable()
.scaledToFit()
.frame(width: 30)
Image(systemName: "seal")
.resizable()
.scaledToFit()
.frame(width: 30)
Image(systemName: "seal")
.resizable()
.scaledToFit()
.frame(width: 30)
//TODO: 추가 예정 이미지 확정되면 넣기
}
.frame(maxWidth: .infinity, alignment: .leading)
Divider()
HStack {
Text(shortcutsZipViewModel.userInfo?.nickname ?? TextLiteral.defaultUser)
Text(viewModel.fetchUserInfo())
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundStyle(SCZColor.Basic)
Image(systemName: "slider.horizontal.3")
Expand All @@ -69,18 +47,23 @@ struct MyPageView: View {
}
.padding(.horizontal, 16)

MyPageSection(type: .myShortcut, shortcuts: $shortcutsZipViewModel.shortcutsMadeByUser)
Divider()
.padding(.horizontal, 16)
.foregroundStyle(SCZColor.CharcoalGray.opacity08)
MyPageSection(type: .myDownloadShortcut, shortcuts: $shortcutsZipViewModel.shortcutsUserDownloaded)
Divider()
.padding(.horizontal, 16)
.foregroundStyle(SCZColor.CharcoalGray.opacity08)
MyPageSection(type: .myLovingShortcut, shortcuts: $shortcutsZipViewModel.shortcutsUserLiked)
Divider()
.padding(.horizontal, 16)
.foregroundStyle(SCZColor.CharcoalGray.opacity08)
MyPageSection(type: .myShortcut, shortcuts: $viewModel.myShortcuts, isFolded: .constant(false))

MyPageSection(type: .myDownloadShortcut, shortcuts: $viewModel.myDownloadShortcuts, isFolded: $viewModel.isMyDownloadShortcutFolded)

if viewModel.isMyDownloadShortcutFolded {
Divider()
.padding(.horizontal, 16)
.foregroundStyle(SCZColor.CharcoalGray.opacity08)
}

MyPageSection(type: .myLovingShortcut, shortcuts: $viewModel.myLovingShortcuts, isFolded: $viewModel.isMyLovingShortcutFolded)

if viewModel.isMyLovingShortcutFolded {
Divider()
.padding(.horizontal, 16)
.foregroundStyle(SCZColor.CharcoalGray.opacity08)
}
}
.padding(.bottom, 30)
.padding(.top, 12)
Expand Down Expand Up @@ -138,8 +121,7 @@ struct MyPageView: View {
struct MyPageSection: View {
let type: SectionType
@Binding var shortcuts: [Shortcuts]
@State var isFolded = false

@Binding var isFolded: Bool
var body: some View {

VStack(alignment: .leading, spacing: 0) {
Expand Down Expand Up @@ -171,9 +153,9 @@ struct MyPageSection: View {
}
}
.padding(.horizontal, 16)

let maxNum = shortcuts.count > 3 ? 2 : 3
if type == .myShortcut || !isFolded {
ScrollView(.horizontal, showsIndicators: false ) {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 8) {
Rectangle()
.frame(width: 16)
Expand All @@ -189,17 +171,17 @@ struct MyPageSection: View {
.roundedBorder(cornerRadius: 16, color: .white, isNormalBlend: true, opacity: 0.12)
}

ForEach(shortcuts.prefix(5), id: \.self) { shortcut in
ForEach(shortcuts.prefix(maxNum), id: \.self) { shortcut in
OrderedCell(type: .myShortcut, index: 0, shortcut: shortcut)
}
if shortcuts.count > 5 {
if shortcuts.count > maxNum {
ExpandedCell(type: type, shortcuts: shortcuts)
}
} else {
ForEach(shortcuts.prefix(4), id: \.self) { shortcut in
ForEach(shortcuts.prefix(maxNum), id: \.self) { shortcut in
UnorderedCell(shortcut: shortcut)
}
if shortcuts.count > 4 {
if shortcuts.count > maxNum {
ExpandedCell(type: type, shortcuts: shortcuts)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct ShortcutTabView: View {

@ViewBuilder
private func thirdTab() -> some View {
MyPageView()
MyPageView(viewModel: MyPageViewModel())
.modifierNavigation()
.navigationBarBackground ({ Color.shortcutsZipBackground })
.id(thirdTabID)
Expand Down

0 comments on commit 72a7d9a

Please sign in to comment.