Skip to content
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
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions SOPT-iOS/Projects/Core/Sources/Enum/DateFormatType.swift
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public extension String {

/// 서버에서 들어온 Date String을 Date 타입으로 반환하는 메서드
func toDate() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.timeZone = TimeZone(identifier: "KST")
if let date = dateFormatter.date(from: self) {
DateFormatManager.shared.setFormat(.iso)
if let date = DateFormatManager.shared.stringToDate(self) {
return date
} else {
print("toDate() convert error")
Expand All @@ -38,8 +36,7 @@ public extension String {

/// 서버에서 들어온 Date String을 UI에 적용 가능한 String 타입으로 반환하는 메서드
func serverTimeToString(forUse: TimeStringCase) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yy/MM/dd"
DateFormatManager.shared.setFormat(.dateWithSlash)

let currentTime = Int(Date().timeIntervalSince1970)

Expand All @@ -61,7 +58,7 @@ public extension String {
return "방금"
}
case .forDefault:
return dateFormatter.string(from: self.toDate())
return DateFormatManager.shared.dateToString(self.toDate())
}
}

Expand Down
77 changes: 77 additions & 0 deletions SOPT-iOS/Projects/Core/Sources/Utils/DateFormatterManager.swift
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)"
}
}
40 changes: 0 additions & 40 deletions SOPT-iOS/Projects/Core/Sources/Utils/setDateFormat.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ extension TodayAttendance {

public func toDomain() -> TodayAttendanceModel {
return .init(status: self.status,
attendedAt: setDateFormat(
date: self.attendedAt,
from: "yyyy-MM-dd'T'HH:mm:ss",
to: "HH:mm"))
attendedAt: DateFormatManager.shared.transformDateFormat(self.attendedAt,
from: .dateTimeDash,
to: .time))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ extension ListDetailEntity {
return ListDetailModel(
image: self.images.first ?? "",
content: self.contents,
date: changeDateformat(self.updatedAt ?? self.createdAt),
date: self.updatedAt ?? self.createdAt,
stampId: self.id,
activityDate: self.activityDate
)
}

private func changeDateformat(_ date: String) -> String {
return date.split(separator: "-").joined(separator: ".")
}
}

extension StampEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ extension ShowAttendanceVC {
private func setScheduledData(_ model: AttendanceScheduleModel) {

if self.sceneType == .scheduledDay {
let date = viewModel.formatTimeInterval(startDate: model.startDate,
endDate: model.endDate)
let date = DateFormatManager.shared.formatTimeInterval(start: model.startDate, end: model.endDate)
headerScheduleView.setData(date: date,
place: model.location,
todaySchedule: model.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ extension ShowAttendanceViewModel {
if model.type != SessionType.noSession.rawValue {
self.sceneType = .scheduledDay

let convertedStartDate = self.convertDateString(model.startDate)
let convertedEndDate = self.convertDateString(model.endDate)

let convertedStartDate = DateFormatManager.shared.serverTimeToString(model.startDate,
from: .isoWithoutMillis,
to: .monthDayWeekTime)
let convertedEndDate = DateFormatManager.shared.serverTimeToString(model.endDate,
from: .isoWithoutMillis,
to: .monthDayWeekTime)
let newModel = AttendanceScheduleModel(type: model.type,
id: model.id,
location: model.location,
Expand Down Expand Up @@ -154,33 +157,4 @@ extension ShowAttendanceViewModel {
}
.store(in: self.cancelBag)
}

private func convertDateString(_ dateString: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "ko_KR")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Seoul")

guard let date = dateFormatter.date(from: dateString) else { return "" }

dateFormatter.dateFormat = "M월 d일 EEEE H:mm"
return dateFormatter.string(from: date)
}

func formatTimeInterval(startDate: String, endDate: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "M월 d일 EEEE HH:mm"
dateFormatter.locale = Locale(identifier: "ko_KR")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Seoul")

guard let startDateObject = dateFormatter.date(from: startDate),
let endDateObject = dateFormatter.date(from: endDate) else { return "" }

let formattedStartDate = dateFormatter.string(from: startDateObject)

dateFormatter.dateFormat = "HH:mm"
let formattedEndDate = dateFormatter.string(from: endDateObject)

return "\(formattedStartDate) ~ \(formattedEndDate)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class DailySoptuneMainVC: UIViewController, DailySoptuneMainViewCon
private let dateLabel = UILabel().then {
$0.textColor = DSKitAsset.Colors.gray100.color
$0.font = DSKitFontFamily.Suit.medium.font(size: 16)
$0.text = setDateFormat(to: "M월 d일 EEEE")
$0.text = DateFormatManager.shared.transformDateFormat(to: .monthDayWeek)
}

private let recieveFortune = UILabel().then {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension DailySoptuneMainViewModel {
input.receiveTodayFortuneButtonTap
.withUnretained(self)
.sink { owner, _ in
owner.useCase.getDailySoptuneResult(date: setDateFormat(to: "yyyy-MM-dd"))
owner.useCase.getDailySoptuneResult(date: DateFormatManager.shared.transformDateFormat(to: .dateWithDash))
AmplitudeInstance.shared.track(eventType: .clickCheckTodaySoptune)
}.store(in: cancelBag)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension DailySoptuneResultContentView {

extension DailySoptuneResultContentView {
public func setData(model: DailySoptuneResultModel) {
self.dateLabel.text = setDateFormat(to: "MM월 d일 EEEE")
self.dateLabel.text = DateFormatManager.shared.transformDateFormat(to: .monthDayWeek)
self.contentLabel.text = "\(model.userName)님,\n\(model.title.setLineBreakAtMiddle())"
self.contentLabel.setLineSpacing(lineSpacing: 5)
self.contentLabel.textAlignment = .center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ extension CalendarCardCVC {
extension CalendarCardCVC {
func configureCell(model: HomePresentationModel.RecentSchedule,
userType: UserType) {
// TODO: 서버 - 날짜 포맷 변경 후 반영
self.dateLabel.text = setDateFormat(date: model.date, to: "MM.dd")
self.dateLabel.text = model.date
self.scheduleTitleLabel.text = model.title
if let tagType = CalenderCategoryTagType(rawValue: model.type) {
self.scheduleCategoryTagView.setData(title: tagType.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,15 @@ extension HomeDescriptionModel {
extension HomeRecentScheduleModel {
func toPresentation() -> HomePresentationModel.RecentSchedule {
return HomePresentationModel.RecentSchedule(
date: self.date,
date: changeFormat(self.date),
type: self.type,
title: self.title
)
}

private func changeFormat(_ dateString: String) -> String {
return dateString.split(separator: "-").joined(separator: ".")
}
}

extension HomeAppServicesModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import Foundation

import Core
import Domain

struct SoptlogPresentationModel {
let profile: Profile
let introduce: Introduce
Expand Down Expand Up @@ -35,3 +38,46 @@ struct SoptlogPresentationModel {
let todayFortuneText: String
}
}

extension SoptlogModel {
func toPresentation() -> SoptlogPresentationModel {
var appService: [SoptlogPresentationModel.AppService] = []
appService.append(SoptlogPresentationModel.AppService(
serviceName: I18N.Soptlog.soptlevel,
serviceImageURL: self.icons[0],
serviceValue: self.soptLevel))
appService.append(SoptlogPresentationModel.AppService(
serviceName: I18N.Soptlog.poke,
serviceImageURL: self.icons[1],
serviceValue: self.pokeCount))

if self.isActive {
appService.append(SoptlogPresentationModel.AppService(
serviceName: I18N.Soptlog.soptamp,
serviceImageURL: self.icons[2],
serviceValue: self.soptampRank))
} else {
appService.append(SoptlogPresentationModel.AppService(
serviceName: I18N.Soptlog.withSopt,
serviceImageURL: self.icons[2],
serviceValue: self.during))
}


return SoptlogPresentationModel(
profile: SoptlogPresentationModel.Profile(
userName: self.userName,
profileImage: self.profileImage,
part: self.part
),
introduce: SoptlogPresentationModel.Introduce(
profileMessage: self.profileMessage
),
appService: appService,
alarm: SoptlogPresentationModel.Alarm(
isFortuneChecked: self.isFortuneChecked,
todayFortuneText: self.todayFortuneText
)
)
}
}
Loading