기존의 기본적인 타이머 앱에 CoreData를 통합하고 UX를 개선하는 과정을 정리하였다. 데이터 영속성 구현과 사용자 경험 향상에 중점을 두었다.
TimerCoreDataManager
싱글톤 패턴 적용타이머명 입력 필드의 가시성을 개선하였다:
labelTextField.attributedPlaceholder = NSAttributedString(
string: "타이머명",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray]
)
리셋 기능을 강화하였다:
@objc private func resetButtonTapped() {
stopTimer()
hours = 0
minutes = 0
seconds = 0
updateTimerLabel()
labelTextField.text = "" // 텍스트필드 초기화 추가
playPauseButton.setImage(UIImage(systemName: "play.fill"), for: .normal)
}
CoreData 매니저 클래스를 구현하여 데이터 관리를 체계화하였다:
class TimerCoreDataManager {
static let shared = TimerCoreDataManager()
// CRUD Operations
func saveTimer(name: String, hours: Int, minutes: Int, seconds: Int) {
let timer = TimerItem(context: context)
timer.name = name
timer.hours = Int16(hours)
timer.minutes = Int16(minutes)
timer.seconds = Int16(seconds)
timer.createdAt = Date()
saveContext()
}
func fetchTimers() -> [TimerItem] {
let request: NSFetchRequest<TimerItem> = TimerItem.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false)]
do {
return try context.fetch(request)
} catch {
print("타이머 가져오기 실패: \(error)")
return []
}
}
}
테이블뷰를 활용하여 저장된 타이머 목록을 구현하였다:
extension TimerView: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TimerCell", for: indexPath)
cell.backgroundColor = .clear
cell.textLabel?.textColor = .white
let timerItem = timerItems[indexPath.row]
let duration = String(format: "%02d:%02d:%02d",
timerItem.hours,
timerItem.minutes,
timerItem.seconds)
cell.textLabel?.text = "\(timerItem.name ?? "Unknown") \(duration)"
return cell
}
}
기능 추가
UI/UX 개선