Skip to content

Commit

Permalink
Merge pull request #7 from innovationacademy-kr/feat/calendarVM
Browse files Browse the repository at this point in the history
[Calendar] ViewModel 나누기, 테스트 코드 변경 및 추가에 대한 작업
  • Loading branch information
ittzggd authored Apr 1, 2024
2 parents f7e2391 + 097d4b6 commit ad63f1a
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 150 deletions.
5 changes: 4 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
identifier_name:
min_length: 0
min_length: 0

disabled_rules:
- line_length
12 changes: 9 additions & 3 deletions HANE24.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
9AF730F8299E2F5000AF2E53 /* CalendarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF730F7299E2F5000AF2E53 /* CalendarView.swift */; };
9AF730FB299F53C700AF2E53 /* HaneVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF730FA299F53C700AF2E53 /* HaneVM.swift */; };
9AF730FD299F602D00AF2E53 /* JSONs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF730FC299F602D00AF2E53 /* JSONs.swift */; };
D6185AE12BB3C71800E6944A /* CalendarVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6185AE02BB3C71800E6944A /* CalendarVM.swift */; };
D646BD0C2B95DCD500ED631E /* HANE24Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646BD0B2B95DCD500ED631E /* HANE24Test.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -130,9 +131,9 @@
0EFDFD46299F5AF500A911F7 /* DarkMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DarkMode.swift; sourceTree = "<group>"; };
2B1326102B2821E600301A8B /* ThisMonthAccTimeCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThisMonthAccTimeCardView.swift; sourceTree = "<group>"; };
2B1326122B285B1D00301A8B /* NoticeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoticeView.swift; sourceTree = "<group>"; };
38BC7E64D66913E5A6C0834C /* Pods-24HANE.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-24HANE.debug.xcconfig"; path = "Target Support Files/Pods-24HANE/Pods-24HANE.debug.xcconfig"; sourceTree = "<group>"; };
5F9D5EBD040CA1B419BF545D /* Pods_24HANE.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_24HANE.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9A1A4BED299ADE650076D650 /* TodayAccTimeCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodayAccTimeCardView.swift; sourceTree = "<group>"; };
9AF0C045D5FEBE82760C04A3 /* Pods-24HANE.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-24HANE.debug.xcconfig"; path = "Target Support Files/Pods-24HANE/Pods-24HANE.debug.xcconfig"; sourceTree = "<group>"; };
9AF730EC299CC54F00AF2E53 /* DateExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtensions.swift; sourceTree = "<group>"; };
9AF730EE299D3A4900AF2E53 /* CalendarGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarGridView.swift; sourceTree = "<group>"; };
9AF730F0299D58F100AF2E53 /* AccTimeCardForCalendarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccTimeCardForCalendarView.swift; sourceTree = "<group>"; };
Expand All @@ -142,6 +143,7 @@
9AF730FA299F53C700AF2E53 /* HaneVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HaneVM.swift; sourceTree = "<group>"; };
9AF730FC299F602D00AF2E53 /* JSONs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONs.swift; sourceTree = "<group>"; };
A021B0070CB31045F069E3A2 /* Pods-24HANE.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-24HANE.release.xcconfig"; path = "Target Support Files/Pods-24HANE/Pods-24HANE.release.xcconfig"; sourceTree = "<group>"; };
D6185AE02BB3C71800E6944A /* CalendarVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarVM.swift; sourceTree = "<group>"; };
D646BD092B95DCD500ED631E /* HANE24Test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HANE24Test.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D646BD0B2B95DCD500ED631E /* HANE24Test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HANE24Test.swift; sourceTree = "<group>"; };
D680AC322BB17CA00083025D /* env.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = env.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -316,6 +318,7 @@
isa = PBXGroup;
children = (
9AF730FA299F53C700AF2E53 /* HaneVM.swift */,
D6185AE02BB3C71800E6944A /* CalendarVM.swift */,
0E3ED8A72BB13727001B0BAE /* NetworkManager.swift */,
0E6B608E29AC850D009D8BC4 /* NetworkMonitoringManager.swift */,
);
Expand All @@ -335,8 +338,8 @@
CC7009C835DF6CA6ECA500B0 /* Pods */ = {
isa = PBXGroup;
children = (
9AF0C045D5FEBE82760C04A3 /* Pods-24HANE.debug.xcconfig */,
A021B0070CB31045F069E3A2 /* Pods-24HANE.release.xcconfig */,
38BC7E64D66913E5A6C0834C /* Pods-24HANE.debug.xcconfig */,
);
path = Pods;
sourceTree = "<group>";
Expand Down Expand Up @@ -541,6 +544,7 @@
0E6B608F29AC850D009D8BC4 /* NetworkMonitoringManager.swift in Sources */,
2B1326132B285B1D00301A8B /* NoticeView.swift in Sources */,
0E1654F5299A285B001E5EED /* ContentView.swift in Sources */,
D6185AE12BB3C71800E6944A /* CalendarVM.swift in Sources */,
9AF730EF299D3A4900AF2E53 /* CalendarGridView.swift in Sources */,
0E3ED8A82BB13727001B0BAE /* NetworkManager.swift in Sources */,
0E9868252B2B6C0B00E127DC /* CalendarHeaderView.swift in Sources */,
Expand Down Expand Up @@ -603,6 +607,7 @@
/* Begin XCBuildConfiguration section */
0E165500299A285E001E5EED /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = D680AC322BB17CA00083025D /* env.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -663,6 +668,7 @@
};
0E165501299A285E001E5EED /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = D680AC322BB17CA00083025D /* env.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -717,7 +723,7 @@
};
0E165503299A285E001E5EED /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 9AF0C045D5FEBE82760C04A3 /* Pods-24HANE.debug.xcconfig */;
baseConfigurationReference = 38BC7E64D66913E5A6C0834C /* Pods-24HANE.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
Expand Down
2 changes: 1 addition & 1 deletion HANE24/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ContentView: View {
}

do {
try hane.isSignIn = await hane.isLogin() ? true : false
try hane.isSignIn = await hane.isLogin() ? true : false
self.signInChecked = true
} catch {
print("Invalid URL")
Expand Down
2 changes: 2 additions & 0 deletions HANE24/HANE24App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import SwiftUI
struct HANE24App: App {

var hane = Hane()
var calendar = CalendarVM()

var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(hane)
.environmentObject(calendar)
}
}
}
9 changes: 5 additions & 4 deletions HANE24/View/Calendar/CalendarBodyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct CalendarBodyView: View {
@EnvironmentObject var hane: Hane
@EnvironmentObject var calendarVM: CalendarVM
@State var picker = false
@State var datePickerSelection: Date

Expand Down Expand Up @@ -42,15 +42,16 @@ struct CalendarBodyView: View {
}
}
}
.onChange(of: picker) {pickerState in
.onChange(of: picker) { pickerState in
if !pickerState {
hane.selectedDate = datePickerSelection
calendarVM.calendarModel.selectedDate = datePickerSelection
}
}
}
}

#Preview {
CalendarBodyView(datePickerSelection: Date())
.environmentObject(Hane())
.environmentObject(CalendarVM())
// .environmentObject(Hane())
}
28 changes: 15 additions & 13 deletions HANE24/View/Calendar/CalendarGridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import SwiftUI
struct CalendarGridView: View {
@Binding var picker: Bool

@EnvironmentObject var hane: Hane
@EnvironmentObject var calendarVM: CalendarVM
// @EnvironmentObject var hane: Hane

let weekdays = ["", "", "", "", "", "", ""]
let cols: [GridItem] = Array(repeating: GridItem(.flexible(), spacing: 20), count: 7)

var body: some View {
ZStack {
LoadingAnimation()
.isHidden(!hane.loading)
.isHidden(!calendarVM.loading)
VStack {
// day of week
LazyVGrid(columns: cols, spacing: 12) {
Expand All @@ -35,17 +36,17 @@ struct CalendarGridView: View {
// is today ? border only
// default
LazyVGrid(columns: cols, spacing: 12) {
ForEach(hane.selectedDate.daysOfMonth.indices, id: \.self) { d in
if let day = hane.selectedDate.daysOfMonth[d] {
ForEach(calendarVM.calendarModel.selectedDate.daysOfMonth.indices, id: \.self) { d in
if let day = calendarVM.calendarModel.selectedDate.daysOfMonth[d] {
Button {
hane.selectedDate = day
calendarVM.calendarModel.selectedDate = day
} label: {
ZStack {
RoundedRectangle(cornerRadius: day.isSameDate(with: hane.selectedDate) ? 20 : 10)
RoundedRectangle(cornerRadius: day.isSameDate(with: calendarVM.calendarModel.selectedDate) ? 20 : 10)
.foregroundColor(getGridColor(day))
.overlay {
if day.isSameDate(with: Date())
&& !day.isSameDate(with: hane.selectedDate) {
&& !day.isSameDate(with: calendarVM.calendarModel.selectedDate) {
RoundedRectangle(cornerRadius: 10)
.stroke(Color.dateToday, lineWidth: 1)
}
Expand All @@ -54,7 +55,7 @@ struct CalendarGridView: View {

Text("\(day.dayToInt)")
.foregroundColor(getTextColor(day))
.font(.system(size: 14, weight: day.isSameDate(with: hane.selectedDate) ? .bold : .regular))
.font(.system(size: 14, weight: day.isSameDate(with: calendarVM.calendarModel.selectedDate) ? .bold : .regular))
}
}
.frame(width: 30, height: 30)
Expand All @@ -64,27 +65,27 @@ struct CalendarGridView: View {
}
}
}
.isHidden(hane.loading)
.isHidden(calendarVM.loading)
}
}
}

func getGridColor(_ day: Date) -> Color {
switch day {
case day where day.isSameDate(with: hane.selectedDate):
case day where day.isSameDate(with: calendarVM.calendarModel.selectedDate):
return .dateSelected
case day where day.isSameDate(with: Date.now):
return .backgroundCalendar
default:
return calculateLogColor(accumulationTime: hane.dailyTotalTimesInAMonth[day.dayToInt])
return calculateLogColor(accumulationTime: calendarVM.calendarModel.dailyTotalTimesInAMonth[day.dayToInt])
}
}

func getTextColor(_ day: Date) -> Color {
switch day {
case day where day > Date.now:
return .fontDisabled
case day where day.isSameDate(with: hane.selectedDate):
case day where day.isSameDate(with: calendarVM.calendarModel.selectedDate):
return .fontWhite
case day where day.isSameDate(with: Date.now):
return .dateToday
Expand Down Expand Up @@ -112,5 +113,6 @@ struct CalendarGridView: View {

#Preview {
CalendarGridView(picker: .constant(false))
.environmentObject(Hane())
.environmentObject(CalendarVM())
// .environmentObject(Hane())
}
14 changes: 8 additions & 6 deletions HANE24/View/Calendar/CalendarHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ import SwiftUI

struct CalendarHeaderView: View {
@Binding var picker: Bool
@EnvironmentObject var hane: Hane
// @EnvironmentObject var hane: Hane
@EnvironmentObject var calendarVM: CalendarVM

var body: some View {
HStack {
changeMonthButton(isForward: false)
.disabled(hane.selectedDate.toString("yyyy.MM") <= "2022.08" || hane.loading)
.disabled(calendarVM.calendarModel.selectedDate.toString("yyyy.MM") <= "2022.08" || calendarVM.loading)
Spacer()
Text("\(hane.selectedDate.yearToString).\(hane.selectedDate.monthToString)")
Text("\(calendarVM.calendarModel.selectedDate.yearToString).\(calendarVM.calendarModel.selectedDate.monthToString)")
.foregroundColor(picker ? .gradientPurple : .fontDefault )
.onTapGesture {
picker.toggle()
}
Spacer()
changeMonthButton(isForward: true)
.disabled(hane.selectedDate.toString("yyyy.MM") >= Date().toString("yyyy.MM") || hane.loading)
.disabled(calendarVM.calendarModel.selectedDate.toString("yyyy.MM") >= Date().toString("yyyy.MM") || calendarVM.loading)
}
.font(.system(size: 20, weight: .semibold))
}

@ViewBuilder func changeMonthButton(isForward: Bool) -> some View {
Button {
hane.selectedDate = Calendar.current.date(byAdding: .month, value: isForward ? 1 : -1, to: hane.selectedDate)!
calendarVM.calendarModel.selectedDate = Calendar.current.date(byAdding: .month, value: isForward ? 1 : -1, to: calendarVM.calendarModel.selectedDate)!
} label: {
ZStack {
Image(systemName: isForward ? "chevron.right" : "chevron.left")
Expand All @@ -44,5 +45,6 @@ struct CalendarHeaderView: View {

#Preview {
CalendarHeaderView(picker: .constant(false))
.environmentObject(Hane())
.environmentObject(CalendarVM())
// .environmentObject(Hane())
}
42 changes: 11 additions & 31 deletions HANE24/View/Calendar/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import SwiftUI
/// selectedDate: Date = 선택 날짜
struct CalendarView: View {
@Environment(\.colorScheme) var colorScheme
@EnvironmentObject var hane: Hane
// @EnvironmentObject var hane: Hane
@EnvironmentObject var calendarVM: CalendarVM

var body: some View {
ZStack {
Expand All @@ -19,16 +20,17 @@ struct CalendarView: View {
ScrollView {
PullToRefresh(coordinateSpaceName: "pullToRefresh") {
Task {
try await hane.refresh()
try await calendarVM.updateMonthlyLogs(date: calendarVM.calendarModel.selectedDate)
}
}
VStack(spacing: 16) {
CalendarBodyView(datePickerSelection: hane.selectedDate)
CalendarBodyView(datePickerSelection: calendarVM.calendarModel.selectedDate)
.padding(.horizontal, 5)
AccTimeCardForCalendarView(totalAccTime: hane.monthlyTotalAccumulationTime,
validAccTime: hane.monthlyAcceptedAccumulationTime )
AccTimeCardForCalendarView(
totalAccTime: calendarVM.calendarModel.monthlyTotalAccumulationTime,
validAccTime: calendarVM.calendarModel.monthlyAcceptedAccumulationTime)
.padding(.vertical, 10)
TagLogView(logList: convert(hane.monthlyLogs[hane.selectedDate.toString("yyyy.MM.dd")] ?? []))
TagLogView(logList: calendarVM.convertedSelectedMonthlyLog)
.padding(.top, 10)
Spacer()
}
Expand All @@ -37,39 +39,17 @@ struct CalendarView: View {
.coordinateSpace(name: "pullToRefresh")
}
.coordinateSpace(name: "pullToRefresh")
.onChange(of: hane.selectedDate) {[oldDate = hane.selectedDate] newDate in
.onChange(of: calendarVM.calendarModel.selectedDate) {[oldDate = calendarVM.calendarModel.selectedDate] newDate in
if oldDate.monthToInt != newDate.monthToInt || oldDate.yearToInt != newDate.yearToInt {
Task {
try await hane.updateMonthlyLogs(date: newDate)
try await calendarVM.updateMonthlyLogs(date: newDate)
}
}
}
}

func convert(_ from: [InOutLog]) -> [Log] {
guard !from.isEmpty else { return [] }
var logArray = from.map {
var inTime: String?
var outTime: String?
var logTime: String? = "누락"
if let intime = $0.inTimeStamp {
inTime = Date(milliseconds: intime).toString("HH:mm:ss")
}
if let outtime = $0.outTimeStamp {
outTime = Date(milliseconds: outtime).toString("HH:mm:ss")
}
if var logtime = $0.durationSecond {
logtime -= 9 * 3600
logTime = Date(milliseconds: logtime).toString("HH:mm:ss")
}
return Log(inTime: inTime, outTime: outTime, logTime: logTime)
}
logArray[0].logTime = (logArray[0].logTime == "누락" && hane.selectedDate.toString("yyyy.MM.dd") == Date().toString("yyyy.MM.dd")) ? "-" : logArray[0].logTime
return logArray.reversed()
}
}

#Preview {
CalendarView()
.environmentObject(Hane())
.environmentObject(CalendarVM())
}
10 changes: 6 additions & 4 deletions HANE24/View/Calendar/TagLogView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import SwiftUI

struct TagLogView: View {
@EnvironmentObject var hane: Hane
// @EnvironmentObject var hane: Hane
@EnvironmentObject var calendarVM: CalendarVM
@Environment(\.colorScheme) var colorScheme

var logList: [Log]

var body: some View {
let dailyTotalTime = hane.dailyTotalTimesInAMonth[hane.selectedDate.dayToInt]
let dailyTotalTime = calendarVM.calendarModel.dailyTotalTimesInAMonth[calendarVM.calendarModel.selectedDate.dayToInt]
VStack(alignment: .center, spacing: 4) {
HStack {
Text("\(hane.selectedDate.monthToInt).\(hane.selectedDate.dayToInt) \(hane.selectedDate.toString("E"))요일")
Text("\(calendarVM.calendarModel.selectedDate.monthToInt).\(calendarVM.calendarModel.selectedDate.dayToInt) \(calendarVM.calendarModel.selectedDate.toString("E"))요일")
.padding(.leading, 5)
Spacer()
Text("\(dailyTotalTime / 3600)시간 \((dailyTotalTime % 3600) / 60)")
Expand Down Expand Up @@ -92,5 +93,6 @@ struct TagLogView: View {

#Preview {
TagLogView(logList: [])
.environmentObject(Hane())
.environmentObject(CalendarVM())
// .environmentObject(Hane())
}
3 changes: 3 additions & 0 deletions HANE24/View/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SwiftUI

struct MainView: View {
@EnvironmentObject var hane: Hane
@EnvironmentObject var calendar: CalendarVM

@State var selection = 1
@Environment(\.colorScheme) var colorScheme

Expand Down Expand Up @@ -37,6 +39,7 @@ struct MainView: View {
.task {
do {
try await hane.refresh()
try await calendar.updateMonthlyLogs(date: .now)
} catch {
print("error on MainView \(error.localizedDescription)")
}
Expand Down
Loading

0 comments on commit ad63f1a

Please sign in to comment.