Skip to content

Commit

Permalink
Merge pull request #132 from Good-MoGong/Oz/#130
Browse files Browse the repository at this point in the history
Refactor, feat: gpt 응답 저장, RecordView 수정 Oz/#130
  • Loading branch information
LutherCho authored Mar 4, 2024
2 parents 1f092ab + eb9dc3f commit 6ae948e
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22222" systemVersion="23A344" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22222" systemVersion="23D60" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="DiaryEntity" representedClassName="DiaryEntity" syncable="YES">
<attribute name="action" attributeType="String"/>
<attribute name="date" attributeType="String"/>
<attribute name="emotion" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromDataTransformerName" customClassName="[String]"/>
<attribute name="event" attributeType="String"/>
<attribute name="gptAnswer" optional="YES" attributeType="String"/>
<attribute name="idea" attributeType="String"/>
</entity>
<entity name="UserEntity" representedClassName="UserEntity" syncable="YES" codeGenerationType="class">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ extension DiaryEntity {
@NSManaged public var emotion: [String]
@NSManaged public var event: String
@NSManaged public var idea: String
@NSManaged public var gptAnswer: String
}
167 changes: 105 additions & 62 deletions Projects/App/Sources/DesignSystem/Common/RecordView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import SwiftUI

enum RecordViewText {
case beforeRecord
case beforeRecordPast
case beforeRecordToday
case afterRecord
case mypageRecord(count: Int)

var stringValue: String {
switch self {
case .beforeRecord:
case .beforeRecordPast:
"감정을 기록하지 않은 날이에요.\n오늘은 시미와 감정을 기록해 볼까요?"
case .beforeRecordToday:
"오늘의 감정이 기록되지 않았어요\n시미가 당신을 기다리고 있어요!"
case .afterRecord:
"꾸준한 감정일기는 자신을 단단히\n 만들어준답니다! 내일도 와주실거죠?"
"꾸준한 감정일기는 자신을 단단히\n만들어준답니다! 내일도 와주실거죠?"
case let .mypageRecord(count):
"\(count)개의 감정기록이 담겨있네요!\n시미가 당신의 의견을 기다리고 있어요"
}
Expand All @@ -30,85 +33,125 @@ struct RecordView: View {
var isShowingMainView: Bool = true
/// 기록 전, 후를 bool로 구분
var beforeRecord: Bool?
/// 감정이 기록되지 않았을 때 (과거, 오늘) -> true면 과거에 기록이 된 것
var isRecordPast: Bool? = true
var recordCount: Int = 0

@State private var isShowingRecordView: Bool = false
@State private var isShowingOrganizeView: Bool = false
@State private var nickname: String = UserDefaultsKeys.nickname

var body: some View {
HStack {
VStack(alignment: .leading) {
if isShowingMainView {
Text(beforeRecord ?? true ? "오늘의 기록" : "오늘 일기 작성 완료!")
.font(PretendardFont.h4Bold)
.padding(.bottom, 12)
} else {
NavigationLink {
MyAccountInfo()
} label: {
HStack {
Text("\(nickname)")
.font(PretendardFont.h4Bold)
Image(systemName: "chevron.right")
}
}
.buttonStyle(PlainButtonStyle())
.padding(.bottom, 12)
}
titleTextView()

VStack(alignment: .leading) {
Text(isShowingMainView ? (beforeRecord ?? false ?
RecordViewText.beforeRecord.stringValue : RecordViewText.afterRecord.stringValue
) :
RecordViewText.mypageRecord(count: recordCount).stringValue)
}
.lineSpacing(8)
.font(PretendardFont.bodyMedium)
.fixedSize(horizontal: true, vertical: false)
.padding(.bottom, 12)
if beforeRecord ?? true {
Button {
if isShowingMainView && beforeRecord ?? true {
isShowingRecordView = true
}
} label: {
Text(isShowingMainView ? (beforeRecord ?? false ? "감정 기록하기" : "기록 보러가기") : "시미에게 의견 보내기")
.font(isShowingMainView ? PretendardFont.h4Bold : PretendardFont.h5Medium)
.padding(.vertical, isShowingMainView ? -5 : 5)
}
.buttonStyle(isShowingMainView ? CustomButtonStyle(MainButtonStyle(isButtonEnabled: true)) : CustomButtonStyle(SubPinkButtonStyle()))
.navigationDestination(isPresented: $isShowingRecordView) {
RecordStartView(isShowingOrganizeView: $isShowingRecordView)
}
}
contentTextView()

ButtonView()
}

Spacer()

// 기록 전
if beforeRecord ?? false {
Image("SimiSad")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: .symWidth * 0.3)
} else {
// 기록 후
Image("SimiSmile")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: .symWidth * 0.3)
}

Image(imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: .symWidth * 0.3)
}
.padding(.horizontal, 20)
.padding(.horizontal, 15)
.padding(.vertical, 18)
.background(
RoundedRectangle(cornerRadius: 15)
.fill(Color.bright)
)
}

@ViewBuilder
private func titleTextView() -> some View {
Group {
if isShowingMainView {
if beforeRecord == true {
if let isRecordPast = isRecordPast {
Text(isRecordPast ? "오늘의 기록" : "기록이 없어요!")
} else { }
} else {
Text("오늘 일기 작성 완료!")
}
} else {
NavigationLink {
MyAccountInfo()
} label: {
HStack {
Text("\(nickname)")
Image(systemName: "chevron.right")
.font(.medium(18))
}
}
.buttonStyle(PlainButtonStyle())
}
}
.padding(.bottom, 12)
.font(.bold(18))
}

@ViewBuilder
private func contentTextView() -> some View {
Group {
if isShowingMainView {
if beforeRecord == true {
if let isRecordPast = isRecordPast {
Text(!isRecordPast ? RecordViewText.beforeRecordPast.stringValue : RecordViewText.beforeRecordToday.stringValue)
}
} else {
Text(RecordViewText.afterRecord.stringValue)
}
} else {
Text(RecordViewText.mypageRecord(count: recordCount).stringValue)
}
}
.lineSpacing(4)
.font(.medium(14))
.fixedSize(horizontal: true, vertical: false)
.padding(.bottom, 12)
}

@ViewBuilder
private func ButtonView() -> some View {
Group {
if isRecordPast ?? true {
Button {
if isShowingMainView && beforeRecord ?? true {
isShowingRecordView = true
} else {
isShowingOrganizeView = true

}
} label: {
Text(actionButtonText)
.font(.bold(15))
.padding(.vertical, isShowingMainView ? -5 : 5)
}
.buttonStyle(isShowingMainView ? CustomButtonStyle(MainButtonStyle(isButtonEnabled: true)) : CustomButtonStyle(SubPinkButtonStyle()))
.navigationDestination(isPresented: $isShowingRecordView) {
RecordStartView(isShowingOrganizeView: $isShowingRecordView)
}
}
}
}

private var actionButtonText: String {
if isShowingMainView {
return beforeRecord ?? false ? "감정 기록하기" : "기록 보러가기"
} else {
return "시미에게 의견 보내기"
}
}

private var imageName: String {
return isShowingMainView ? (beforeRecord ?? false ? "SimiSad" : "SimiSmile") : "SimiCurious"
}
}

#Preview {
RecordView(beforeRecord: true)
RecordView(isShowingMainView: true,beforeRecord: true, isRecordPast: false)
.environmentObject(AuthenticationViewModel(container: DIContainer(services: Services())))
}
2 changes: 2 additions & 0 deletions Projects/App/Sources/Model/Diary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ struct Diary: Codable {
var emotions: [String]
/// 행동
var action: String
/// 시미의 공감
var gptAnswer: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ final class CalendarRepository: CalendarRepositoryProtocol {

private let coreDataManager = CoreDataManger.shared
private let fireBaseManager = FirebaseManager.shared
private var fetchDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "")

private var fetchDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "", gptAnswer: "")
private var fetchDiaryArray: [Diary] = []

/// 특정 날짜 기록 불러오기
Expand All @@ -30,10 +31,11 @@ final class CalendarRepository: CalendarRepositoryProtocol {
event: diaryEntity.event,
idea: diaryEntity.idea,
emotions: diaryEntity.emotion,
action: diaryEntity.action
)
action: diaryEntity.action,
gptAnswer: diaryEntity.gptAnswer
)
} else {
self.fetchDiary = Diary(date: "", event: "", idea: "", emotions: [], action: "")
self.fetchDiary = Diary(date: "", event: "", idea: "", emotions: [], action: "", gptAnswer: "")
}

completion(fetchDiary, isFetchSuccess)
Expand All @@ -43,7 +45,7 @@ final class CalendarRepository: CalendarRepositoryProtocol {
func fetchWholeRecord(completion: @escaping ([Diary]) -> Void) {
let diaryEntitys = coreDataManager.retrieve(type: DiaryEntity.self)
for diary in diaryEntitys {
let fetchDiary = Diary(date: diary.date, event: diary.event, idea: diary.idea, emotions: diary.emotion, action: diary.action)
let fetchDiary = Diary(date: diary.date, event: diary.event, idea: diary.idea, emotions: diary.emotion, action: diary.action, gptAnswer: diary.gptAnswer)
self.fetchDiaryArray.append(fetchDiary)
}
completion(fetchDiaryArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class CalendarViewModel: RecordConditionFetch {
@Published var checkingDate: Date = Date()
@Published var popupDate: Bool = false

@Published var recordDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "")
@Published var recordDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "", gptAnswer: "")
@Published var recordDiaryArray: [Diary] = []
@Published var completeRecord: Bool = true
@Published var isShowingRecordView = false
Expand Down
3 changes: 2 additions & 1 deletion Projects/App/Sources/Presentation/MyPage/MypageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct MypageView: View {

var body: some View {
NavigationStack {
RecordView(isShowingMainView: false)
RecordView(isShowingMainView: false, recordCount: CoreDataManger.shared.getDiaryCount())

.padding(.vertical, 20)
CustomerSupport()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class RecordRepository: RecordRepositoryProtocal {
private let chatGPTManager = ChatGPTManager.shared
private let fireBaseManager = FirebaseManager.shared
private let userID = ""
private var fetchDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "")
private var fetchDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "", gptAnswer: "")
private var cancellables = Set<AnyCancellable>()

func saveRecord(userID: String, diary: Diary) async -> Bool {
Expand Down Expand Up @@ -68,6 +68,7 @@ final class RecordRepository: RecordRepositoryProtocal {
diaryInfo.idea = diary.idea
diaryInfo.emotion = diary.emotions
diaryInfo.action = diary.action
diaryInfo.gptAnswer = diary.gptAnswer
}
}
print("CoreData 저장 성공")
Expand Down Expand Up @@ -96,9 +97,9 @@ final class RecordRepository: RecordRepositoryProtocal {
let diaryEntitys = coreDataManager.retrieve(type: DiaryEntity.self, column: \.date, comparision: .equal, value: date)
let isFetchSuccess = diaryEntitys.isEmpty ? false : true
if let diaryEntity = diaryEntitys.first {
self.fetchDiary = Diary(date: diaryEntity.date , event: diaryEntity.event , idea: diaryEntity.idea, emotions: diaryEntity.emotion , action: diaryEntity.action)
self.fetchDiary = Diary(date: diaryEntity.date , event: diaryEntity.event , idea: diaryEntity.idea, emotions: diaryEntity.emotion , action: diaryEntity.action, gptAnswer: diaryEntity.gptAnswer)
} else {
self.fetchDiary = Diary(date: "", event: "", idea: "", emotions: [], action: "")
self.fetchDiary = Diary(date: "", event: "", idea: "", emotions: [], action: "", gptAnswer: "")
}

completion(fetchDiary,isFetchSuccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ final class RecordViewModel: RecordConditionFetch {
let recordUseCase: RecordUseCase
var userID: String = ""
@Published var recordOrder: RecordOrder = .event
@Published var recordDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "")
@Published var recordDiary: Diary = .init(date: "", event: "", idea: "", emotions: [], action: "", gptAnswer: "")
@Published var currentText: String = ""
@Published var selectedEmotion: EmotionType = .joy
@Published var selectedDatailEmotion: [String] = []
@Published var gptAnswerText: String = ""

@Published var isShowingOutPopUp: Bool = false
@Published var isShowingGuidePopUp: Bool = false
Expand All @@ -41,9 +40,10 @@ final class RecordViewModel: RecordConditionFetch {
}
else if recordOrder == .action && direction == .next {
recordDiary.date = Date().formatToString()
Task {
self.isShowingCompletionView = await recordUseCase.saveRecord(userID: userID, diary: recordDiary)
}
makeGPTRequest()
// Task {
// self.isShowingCompletionView = await recordUseCase.saveRecord(userID: userID, diary: recordDiary)
// }
} else {
withAnimation {
let indexOffset = direction == .previous ? -1 : 1
Expand Down Expand Up @@ -74,9 +74,14 @@ final class RecordViewModel: RecordConditionFetch {

func makeGPTRequest() {
isGPTLoading = true
recordUseCase.makeGPTRequest(diary: recordDiary) { gptAnswer in
self.gptAnswerText = gptAnswer
isShowingCompletionView = true
recordUseCase.makeGPTRequest(diary: recordDiary) { [self] gptAnswer in
recordDiary.gptAnswer = gptAnswer

self.isGPTLoading = false
Task {
await recordUseCase.saveRecord(userID: self.userID, diary: recordDiary)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ struct RecordCompletionView: View {

VStack(alignment: .leading) {
if recordViewModel.isGPTLoading {
ProgressView()
ProgressView()
} else {
ChatBubble(message: recordViewModel.gptAnswerText, animatedMessage: $animatedMessage)
ChatBubble(message: recordViewModel.recordDiary.gptAnswer, animatedMessage: $animatedMessage)
}
Spacer()
}

HStack {
Button("") {
isShowingOrganizeView = false
Expand All @@ -82,9 +82,6 @@ struct RecordCompletionView: View {
RecordOrganizeView(organizeViewModel: recordViewModel, isShowingOrganizeView: $isShowingOrganizeView)
}
.navigationBarBackButtonHidden()
.onAppear(perform: {
recordViewModel.makeGPTRequest()
})
}
}

Expand Down
Loading

0 comments on commit 6ae948e

Please sign in to comment.