-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] #201 - Mypage ViewModel 파일 생생 및 서버와 연관된 코드 이동
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Winey/Winey/Source/Scenes/MyPage/ViewModels/MypageViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// MypageViewModel.swift | ||
// Winey | ||
// | ||
// Created by 고영민 on 10/26/23. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
|
||
class MypageViewModel { | ||
|
||
|
||
// MARK: - Properties | ||
|
||
var userService = UserService() | ||
var collectionView: UICollectionView? | ||
var nickname: String? | ||
var userLevel: UserLevel? | ||
var duringGoalAmount: Int? | ||
var duringGoalCount: Int? | ||
var targetMoney: Int? | ||
var dday: Int? | ||
var isOver: Bool = false | ||
|
||
// 데이터 업데이트를 알리기 위한 클로저 | ||
var dataUpdated: (() -> Void)? | ||
|
||
// MARK: - Server | ||
func getTotalUser() { | ||
userService.getTotalUser() { [weak self] response in | ||
guard let response = response, let data = response.data else { return } | ||
guard let self else { return } | ||
let userData = data.userResponseUserDto | ||
self.userLevel = self.judgeUserLevel(userData.userLevel) | ||
self.nickname = userData.nickname | ||
|
||
let goal = data.userResponseGoalDto | ||
self.duringGoalCount = goal?.duringGoalCount | ||
self.duringGoalAmount = goal?.duringGoalAmount | ||
self.dday = goal?.dday | ||
self.targetMoney = goal?.targetMoney | ||
self.isOver = data.userResponseGoalDto?.isOver ?? false | ||
|
||
let hasGoal = data.userResponseGoalDto != nil | ||
UserSingleton.saveGoal(hasGoal) | ||
|
||
self.collectionView?.reloadData() | ||
self.dataUpdated?() | ||
} | ||
} | ||
private func judgeUserLevel(_ userLevel: String) -> UserLevel? { | ||
return UserLevel(rawValue: userLevel) | ||
} | ||
} |