Skip to content

Commit

Permalink
[Feat] #473 - Add ReadShortcutViewModel to UpdateShortcutView
Browse files Browse the repository at this point in the history
  • Loading branch information
jim4020key committed Jul 24, 2023
1 parent 69a9c14 commit c39b1bd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 72 deletions.
14 changes: 14 additions & 0 deletions HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ final class ReadShortcutViewModel: ObservableObject {
@Published var isEditingComment = false
@Published var isUndoingCommentEdit = false

/// UpdateShortcutView
@Published var updatedLink = ""
@Published var updateDescription = ""
@Published var isLinkValid = false
@Published var isDescriptionValid = false

init(data: Shortcuts) {
self.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: data.id) ?? data
self.isMyLike = shortcutsZipViewModel.checkLikedShortrcut(shortcutID: data.id)
Expand Down Expand Up @@ -108,4 +114,12 @@ final class ReadShortcutViewModel: ObservableObject {
isDeletingShortcut.toggle()
isDowngradingUserLevel = shortcutsZipViewModel.isShortcutDowngrade()
}

func updateShortcut() {
shortcutsZipViewModel.updateShortcutVersion(shortcut: shortcut,
updateDescription: updateDescription,
updateLink: updatedLink)
self.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: shortcut.id) ?? shortcut
isUpdatingShortcut.toggle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,46 @@ struct ReadShortcutView: View {
ScrollViewReader { proxy in
ScrollView {
VStack(spacing: 0) {
if viewModel.shortcut != nil {

StickyHeader(height: 40)

/// 단축어 타이틀
ReadShortcutViewHeader(shortcut: $viewModel.shortcut, isMyLike: $viewModel.isMyLike)

/// 탭뷰 (기본 정보, 버전 정보, 댓글)
LazyVStack(pinnedViews: [.sectionHeaders]) {
Section(header: tabBarView) {
ZStack {
TabView(selection: $viewModel.currentTab) {
Color.clear
.tag(0)
Color.clear
.tag(1)
Color.clear
.tag(2)
}
.tabViewStyle(.page(indexDisplayMode: .never))
.frame(minHeight: UIScreen.screenHeight / 2)

switch viewModel.currentTab {
case 0:
ReadShortcutContentView(shortcut: $viewModel.shortcut)
case 1:
ReadShortcutVersionView(shortcut: $viewModel.shortcut, isUpdating: $viewModel.isUpdatingShortcut)
case 2:
ReadShortcutCommentView(isFocused: _isFocused,
newComment: $viewModel.comment,
comments: $viewModel.comments,
nestedCommentTarget: $viewModel.nestedCommentTarget,
isEditingComment: $viewModel.isEditingComment,
shortcutID: viewModel.shortcut.id)
.id(bottomID)
default:
EmptyView()
}
StickyHeader(height: 40)

/// 단축어 타이틀
ReadShortcutViewHeader(shortcut: $viewModel.shortcut, isMyLike: $viewModel.isMyLike)

/// 탭뷰 (기본 정보, 버전 정보, 댓글)
LazyVStack(pinnedViews: [.sectionHeaders]) {
Section(header: tabBarView) {
ZStack {
TabView(selection: $viewModel.currentTab) {
Color.clear
.tag(0)
Color.clear
.tag(1)
Color.clear
.tag(2)
}
.tabViewStyle(.page(indexDisplayMode: .never))
.frame(minHeight: UIScreen.screenHeight / 2)

switch viewModel.currentTab {
case 0:
ReadShortcutContentView(shortcut: $viewModel.shortcut)
case 1:
ReadShortcutVersionView(shortcut: $viewModel.shortcut, isUpdating: $viewModel.isUpdatingShortcut)
case 2:
ReadShortcutCommentView(isFocused: _isFocused,
newComment: $viewModel.comment,
comments: $viewModel.comments,
nestedCommentTarget: $viewModel.nestedCommentTarget,
isEditingComment: $viewModel.isEditingComment,
shortcutID: viewModel.shortcut.id)
.id(bottomID)
default:
EmptyView()
}
.animation(.easeInOut, value: viewModel.currentTab)
.padding(.top, 4)
.padding(.horizontal, 16)
}
.animation(.easeInOut, value: viewModel.currentTab)
.padding(.top, 4)
.padding(.horizontal, 16)
}
}
}
Expand Down Expand Up @@ -128,14 +125,6 @@ struct ReadShortcutView: View {
.onAppear {
UINavigationBar.appearance().standardAppearance.configureWithTransparentBackground()
}
// .onChange(of: viewModel.isEditingShortcut || viewModel.isUpdatingShortcut) { _ in
// if !isEditingShortcut || !isUpdatingShortcut {
// data.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: data.shortcutID)
// }
// }
// .onChange(of: viewModel.shortcutsZipViewModel.allComments) { _ in
// self.comments = shortcutsZipViewModel.fetchComment(shortcutID: data.shortcutID)
// }
.onDisappear {
viewModel.onViewDissapear()
}
Expand All @@ -159,9 +148,8 @@ struct ReadShortcutView: View {
path: $writeNavigation.navigationPath)
.environmentObject(writeNavigation)
}
//TODO: update shortcut
.fullScreenCover(isPresented: $viewModel.isUpdatingShortcut) {
UpdateShortcutView(isUpdating: $viewModel.isUpdatingShortcut, shortcut: $viewModel.shortcut)
UpdateShortcutView(viewModel: self.viewModel)
}

/// 댓글 수정할 때 뒷 배경을 어둡게 만들기 위한 뷰
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ import SwiftUI

struct UpdateShortcutView: View {

@EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel
@StateObject var viewModel: ReadShortcutViewModel

@FocusState var isDescriptionFieldFocused: Bool

@Binding var isUpdating: Bool
@Binding var shortcut: Shortcuts

@State var updatedLink = ""
@State var updateDescription = ""
@State var isLinkValid = false
@State var isDescriptionValid = false

var body: some View {
VStack {
HStack {
Button(action: {
isUpdating.toggle()
viewModel.isUpdatingShortcut.toggle()
}, label: {
Text(TextLiteral.cancel)
.foregroundColor(.gray5)
Expand All @@ -48,8 +40,8 @@ struct UpdateShortcutView: View {
placeholder: TextLiteral.updateShortcutViewLinkPlaceholder,
lengthLimit: 100,
isDownloadLinkTextField: true,
content: $updatedLink,
isValid: $isLinkValid
content: $viewModel.updatedLink,
isValid: $viewModel.isLinkValid
)
.onSubmit {
isDescriptionFieldFocused = true
Expand All @@ -64,29 +56,26 @@ struct UpdateShortcutView: View {
placeholder: TextLiteral.updateShortcutViewDescriptionPlaceholder,
lengthLimit: 50,
isDownloadLinkTextField: false,
content: $updateDescription,
isValid: $isDescriptionValid
content: $viewModel.updateDescription,
isValid: $viewModel.isDescriptionValid
)
.focused($isDescriptionFieldFocused)

Spacer()

Button(action: {
shortcutsZipViewModel.updateShortcutVersion(shortcut: shortcut,
updateDescription: updateDescription,
updateLink: updatedLink)
isUpdating.toggle()
viewModel.updateShortcut()
}, label: {
ZStack {
RoundedRectangle(cornerRadius: 12)
.foregroundColor(isLinkValid && isDescriptionValid ? .shortcutsZipPrimary : .shortcutsZipPrimary.opacity(0.13))
.foregroundColor(viewModel.isLinkValid && viewModel.isDescriptionValid ? .shortcutsZipPrimary : .shortcutsZipPrimary.opacity(0.13))
.frame(maxWidth: .infinity, maxHeight: 52)
Text(TextLiteral.update)
.foregroundColor(isLinkValid && isDescriptionValid ? .textButton : .textButtonDisable)
.foregroundColor(viewModel.isLinkValid && viewModel.isDescriptionValid ? .textButton : .textButtonDisable)
.shortcutsZipBody1()
}
})
.disabled(!isLinkValid || !isDescriptionValid)
.disabled(!viewModel.isLinkValid || !viewModel.isDescriptionValid)
.padding(.horizontal, 16)
.padding(.bottom, 24)
}
Expand Down

0 comments on commit c39b1bd

Please sign in to comment.