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

기록이 저장된 상태에서 기록 화면에 진입하는 경우 삭제 버튼 노출 #60

Merged
merged 1 commit into from
Apr 15, 2024
Merged
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
16 changes: 16 additions & 0 deletions PlantingMind/PlantingMind/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@
}
}
},
"delete_alert" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Deleted record cannot be recovered. Are you sure you want to delete it?"
}
},
"ko" : {
"stringUnit" : {
"state" : "translated",
"value" : "삭제된 기록은 복구할 수 없습니다. 그래도 삭제하시겠습니까?"
}
}
}
},
"done" : {
"extractionState" : "manual",
"localizations" : {
Expand Down
25 changes: 25 additions & 0 deletions PlantingMind/PlantingMind/MoodRecord/MoodRecordView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import SwiftUI
struct MoodRecordView: View {
@Environment(\.dismiss) var dismiss
@Environment(\.colorScheme) var colorScheme
@FocusState var isFocused: Bool

@ObservedObject var viewModel: MoodRecordViewModel
@State var isDialogPresent: Bool = false
@State var isDeleteAlertPresent: Bool = false

var body: some View {
NavigationStack() {
Expand Down Expand Up @@ -44,6 +46,19 @@ struct MoodRecordView: View {
limitStringView
}

if isFocused == false, viewModel.isFirstRecord == false {
Button {
isDeleteAlertPresent.toggle()
} label: {
Image(systemName: "trash.fill")
.foregroundStyle(.white)
.padding()
.padding(.horizontal, 10)
.background(.red)
.clipShape(Capsule(style: .continuous))
}
}

Spacer()
}
}
Expand Down Expand Up @@ -76,6 +91,15 @@ struct MoodRecordView: View {
}
.foregroundStyle(Color.Custom.general)
}
.alert("delete_alert", isPresented: $isDeleteAlertPresent, actions: {
Button("cancel", role: .cancel) { }
Button("ok", role: .destructive) {
viewModel.deleteRecord(completionHandler: {result in
guard result else { return }
dismiss()
})
}
})
.alert("error_description", isPresented: $viewModel.showErrorAlert) {
Button("ok", role: .cancel) { }
}
Expand Down Expand Up @@ -109,6 +133,7 @@ struct MoodRecordView: View {
.opacity(0.8)
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
.padding(.horizontal)
.focused($isFocused)
.onChange(of: viewModel.reason) { _ in
if viewModel.reason.count > 100 {
viewModel.reason.removeLast()
Expand Down
27 changes: 27 additions & 0 deletions PlantingMind/PlantingMind/MoodRecord/MoodRecordViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MoodRecordViewModel: ObservableObject {
private let originalReason: String

let date: Date
let isFirstRecord: Bool

@Published var mood: Mood
@Published var reason: String
Expand All @@ -28,6 +29,8 @@ class MoodRecordViewModel: ObservableObject {

init(context: NSManagedObjectContext, calendarModel: CalendarModel, moodRecord: MoodRecord?) {
self.context = context
self.isFirstRecord = moodRecord == nil

self.date = Calendar.current.date(from: DateComponents(year: calendarModel.year,
month: calendarModel.month,
day: calendarModel.day)) ?? Date()
Expand All @@ -47,6 +50,30 @@ class MoodRecordViewModel: ObservableObject {
return false
}

func deleteRecord(completionHandler: (Bool) -> Void) {
let fetchRequest = NSFetchRequest<MoodRecord>(entityName: "MoodRecord")
let predicate = NSPredicate(format: "timestamp == %@", date as NSDate)
fetchRequest.predicate = predicate

do {
guard let result = try self.context.fetch(fetchRequest).first else {
completionHandler(false)
return
}

context.delete(result)
try context.save()
WidgetCenter.shared.reloadAllTimelines()

self.sendFetchNotification()
completionHandler(true)
} catch {
self.showErrorAlert.toggle()
CrashlyticsLog.shared.record(error: error)
completionHandler(false)
}
}

func save() {
let fetchRequest = NSFetchRequest<MoodRecord>(entityName: "MoodRecord")
let predicate = NSPredicate(format: "timestamp == %@", date as NSDate)
Expand Down
24 changes: 24 additions & 0 deletions PlantingMind/PlantingMindTests/MoodRecordViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class MoodRecordViewModelTests: XCTestCase {

XCTAssertEqual(viewModel.mood, expectedMood)
XCTAssertEqual(viewModel.reason, expectedReason)
XCTAssertTrue(viewModel.isFirstRecord)
}

func test_기분_데이터_넘겨주는_경우() throws {
Expand Down Expand Up @@ -79,6 +80,29 @@ final class MoodRecordViewModelTests: XCTestCase {
XCTAssertEqual(record?.reason, expectedReason)
}

func test_기록_삭제_확인() throws {
let moodRecord = MoodRecord(context: coreDataStack.persistentContainer.viewContext)
moodRecord.mood = Mood.nice.rawValue
moodRecord.reason = "reason reason"

let viewModel = MoodRecordViewModel(context: coreDataStack.persistentContainer.viewContext,
calendarModel: calendarModel,
moodRecord: moodRecord)

viewModel.deleteRecord { result in
// 기록 저장안한 경우 false 리턴
XCTAssertFalse(result)
}

viewModel.save()
viewModel.deleteRecord { result in
XCTAssertTrue(result)
}

let record = try self.fetch()
XCTAssertNil(record)
}

func test_취소했을_때_변경사항_없는_경우() throws {
let moodRecord = MoodRecord(context: coreDataStack.persistentContainer.viewContext)
moodRecord.mood = Mood.nice.rawValue
Expand Down
Loading