-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] #520 - DateFormatter() 인스턴스 메모리 최적화 #528
Open
yungu0010
wants to merge
13
commits into
develop
Choose a base branch
from
fix/#520-dateformatter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−154
Open
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eb84af0
[Feat] #520 - DateFormatterType 열거형 추가
yungu0010 293ccab
[Fix] #520 - toDate() 함수 내 DateFormatter() 싱글톤으로 변경
yungu0010 60dc35a
[Feat] #520 - DateFormatter 싱글톤 객체 생성 및 유틸함수 생성
yungu0010 eccbd90
[Fix] #520 - transformDateFormat 함수 적용
yungu0010 6911398
[Fix] #520 - .iso, .isoWithoutMillis 타입 추가
yungu0010 106ef65
[Fix] #520 - 출석뷰에서 사용되는 DateFormatter 싱글톤 객체로 대체
yungu0010 e52cbce
[Fix] #520 - 네트워크 로거에서 사용되는 DateFormatter 싱글톤 객체로 대체
yungu0010 0cb2153
[Fix] #520 - 코드리뷰 반영
yungu0010 ffae793
[Merge] 'develop' of remote into fix/#520-dateformatter
yungu0010 6e4a67c
[Fix] #520 - 서버의 Date String 형식 변경
yungu0010 461370b
[Fix] #520 - toPresentation() 함수 viewModel에서 PresentationModel로 이동
yungu0010 35e0a09
[Del] #520 - 불필요한 데이터 변경 삭제
yungu0010 502d79d
[Fix] #520 - CalendarCardCVC의 날짜 포맷 수정 로직 PresentationModel로 이동하여 책임 분리
yungu0010 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,25 @@ | ||
// | ||
// DateFormatType.swift | ||
// Core | ||
// | ||
// Created by 강윤서 on 3/27/25. | ||
// Copyright © 2025 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum DateFormatType: String { | ||
case iso = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" | ||
case isoWithoutMillis = "yyyy-MM-dd'T'HH:mm:ss" | ||
case dateTimeDash = "yyyy-MM-dd HH:mm:ss" | ||
case dateWithDot = "yyyy.MM.dd" | ||
case dateWithDash = "yyyy-MM-dd" | ||
case dateWithSlash = "yy/MM/dd" | ||
case monthDayWeek = "M월 d일 EEEE" | ||
case monthDayWeekTime = "M월 d일 EEEE H:mm" | ||
case monthDayWeekFullTime = "M월 d일 EEEE HH:mm" | ||
case monthDayWithDot = "MM.dd" | ||
case monthDayWIthDash = "MM-dd" | ||
case time = "HH:mm" | ||
case networkLogger = "dd/MM/yyyy HH:mm:ss" | ||
} |
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
77 changes: 77 additions & 0 deletions
77
SOPT-iOS/Projects/Core/Sources/Utils/DateFormatterManager.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,77 @@ | ||
// | ||
// DateFormatterManager.swift | ||
// Core | ||
// | ||
// Created by 강윤서 on 3/27/25. | ||
// Copyright © 2025 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class DateFormatManager { | ||
public static let shared = DateFormatManager() | ||
|
||
private init() { | ||
formatter.locale = Locale(identifier: "ko_KR") | ||
formatter.timeZone = TimeZone(identifier: "Asia/Seoul") | ||
} | ||
|
||
private var formatter = DateFormatter() | ||
} | ||
|
||
public extension DateFormatManager { | ||
func setFormat(_ format: DateFormatType) { | ||
formatter.dateFormat = format.rawValue | ||
} | ||
|
||
/// 문자열을 원하는 포맷의 Date타입으로 변환 | ||
func stringToDate(_ value: String) -> Date? { | ||
return formatter.date(from: value) | ||
} | ||
|
||
/// date타입을 원하는 포맷의 문자열로 변환 | ||
func dateToString(_ date: Date) -> String { | ||
return formatter.string(from: date) | ||
} | ||
|
||
/// 문자열 날짜를 원하는 포맷의 String타입으로 변환 | ||
/// 변경할 값이 비어있다면 오늘 날짜를 반환 | ||
func transformDateFormat(_ target: String? = nil, from before: DateFormatType? = nil, to after: DateFormatType) -> String { | ||
guard let target, let before | ||
else { | ||
setFormat(after) | ||
return dateToString(Date()) | ||
} | ||
|
||
setFormat(before) | ||
guard let date = stringToDate(target) else { return "00:00" } | ||
|
||
setFormat(after) | ||
return formatter.string(from: date) | ||
} | ||
|
||
/// 서버에서 내려주는 iSO 포맷 타입을 원하는 타입으로 변환 | ||
func serverTimeToString(_ target: String, from format: DateFormatType, to serverFormat: DateFormatType = .iso) -> String { | ||
setFormat(serverFormat) | ||
guard let date = stringToDate(target) else { return "" } | ||
|
||
setFormat(format) | ||
return dateToString(date) | ||
} | ||
|
||
/// 시작 날짜와 종료 날짜 입력 받아 원하는 포맷으로 변경 후 문자열로 반환 | ||
func formatTimeInterval(start: String, end: String) -> String { | ||
setFormat(.monthDayWeekFullTime) | ||
|
||
guard let startDate = stringToDate(start), | ||
let endDate = stringToDate(end) | ||
else { return "" } | ||
|
||
let startString = dateToString(startDate) | ||
|
||
setFormat(.time) | ||
let endString = dateToString(endDate) | ||
|
||
return "\(startString) ~ \(endString)" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서버에서 받아온 날짜 관련 String을
뷰계층에서 from -> to로 바꿔주는 것이군요 :)
서버와 약속한 Date format으로 Date를 변환하는 역할은 누구에게 책임이 있다고 생각하시나요?
저는 해당 책임은 데이터 모듈 혹은 네트워크 모듈이 지녀야 한다고 생각해요.
서버의 문자열 format이 바뀌었을 때 뷰계층 코드를 변경하는 것보단 네트워크쪽 코드를 변경하는 것이 자연스럽다고 느껴지기 때문이에요
Date값이 Data계층에서 Domain계층으로 넘어갈 때 String 값을 Date값으로 변경하고,
Presentation 계층에선 Date -> String으로 변환하는 방식은 어떨까요?
Domain에는 가장 순수한 Date 타입만 존재하면 좋을 것 같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다시 생각해보니 해당 PR은 메모리 성능 최적화가 중점적이라, PR에서 작업하지 않은 내용을 리뷰한 것 같네요 ㅎㅎ;
그래도 같이 고민해보면 좋을 것 같습니다 .. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다 !! 기존 코드 수정에만 집중하다 보니 각 레이어의 역할 분리에 대해 깊이 있게 고민하지 못했던 것 같아요.
석우님 말씀처럼 서버에서 받아온
날짜(String)값
을 Data -> Domain 단계에서 Date 타입으로로 변환하면 UI 상황에 맞게 다양한 포맷으로 변경할 수 있어 더 유연한 구조가 될 것 같네요 ! 리뷰에 함께 반영하도록 하겠습니다👍👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
피드백 수용해주셔서 감사함다 :)
현 구조에선 toDomain()에서 String을 Date로 변환하는 것이 가장 편할 것 같네요.
이외에도 서버의 requestBody의 json을 Decodable 구조체로 디코딩할 때 JSONDecoder의 dateStrategyFormat값을 통해 처음부터 해당 밸류값을 Date 타입으로도 받아오는 방법도 있습니다~!