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

[Feat] 다운로드 버튼 기능 업데이트 #305

Merged
merged 17 commits into from
Nov 25, 2022
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
36 changes: 29 additions & 7 deletions HappyAnding/HappyAnding/ViewModel/ShortcutsZipViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,25 @@ class ShortcutsZipViewModel: ObservableObject {

//MARK: 다운로드 수를 업데이트하는 함수

func updateNumberOfDownload(shortcut: Shortcuts) {
self.fetchUser(userID: currentUser()) { data in
var user = data
if !data.downloadedShortcuts.contains(where: { $0.id == shortcut.id }) {
/**
서버 단축어 다운로드 숫자 업데이트,
서버 유저 - downlodedShortcuts 정보 수정
뷰모델 유저 - downlodedShortcuts 정보 수정
*/
func updateNumberOfDownload(shortcut: Shortcuts, downloadlinkIndex: Int) {
if var user = self.userInfo {
if let index = user.downloadedShortcuts.firstIndex(where: { $0.id == shortcut.id }) {
//유저 정보
if downloadlinkIndex == 0 && user.downloadedShortcuts[index].downloadLink != shortcut.downloadLink[0] {
user.downloadedShortcuts[index].downloadLink = shortcut.downloadLink[0] //서버 전송용
self.userInfo?.downloadedShortcuts[index].downloadLink = shortcut.downloadLink[0] //뷰모델 변경용
self.setData(model: user)
//단축어 정보
if let shortcutListIndex = self.shortcutsUserDownloaded.firstIndex(where: {$0.id == shortcut.id}) {
shortcutsUserDownloaded[shortcutListIndex] = shortcut
}
}
} else {
self.db.collection("Shortcut").document(shortcut.id)
.updateData([
"numberOfDownload" : FieldValue.increment(Int64(1))
Expand All @@ -475,9 +490,15 @@ class ShortcutsZipViewModel: ObservableObject {
print(error.localizedDescription)
}
}
let shortcutInfo = DownloadedShortcut(id: shortcut.id, downloadLink: shortcut.downloadLink[0])
user.downloadedShortcuts.append(shortcutInfo)
//유저 정보
let shortcutInfo = DownloadedShortcut(
id: shortcut.id,
downloadLink: shortcut.downloadLink[downloadlinkIndex])
user.downloadedShortcuts.append(shortcutInfo) // 서버 전송용
self.userInfo?.downloadedShortcuts.append(shortcutInfo) //뷰모델 변경용
self.setData(model: user)
//단축어 정보
self.shortcutsUserDownloaded.insert(shortcut, at: 0)
}
}
}
Expand Down Expand Up @@ -554,6 +575,8 @@ class ShortcutsZipViewModel: ObservableObject {
self.userInfo = nil
self.shortcutsMadeByUser.removeAll()
self.curationsMadeByUser.removeAll()
self.shortcutsUserDownloaded.removeAll()
self.shortcutsUserLiked.removeAll()
Comment on lines +578 to +579
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

삭제까지 꼼꼼하게 구현해주셨네요!

}

// MARK: 현재 로그인한 아이디 리턴
Expand Down Expand Up @@ -738,7 +761,6 @@ class ShortcutsZipViewModel: ObservableObject {
print("Error fetching snapshots: \(error!)")
return
}
print(snapshot.metadata.isFromCache ? "**local cache" : "**server")
snapshot.documentChanges.forEach { diff in
let decoder = JSONDecoder()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct ShortcutCell: View {
if let url = URL(string: shortcutCell.downloadLink) {
openURL(url)
if let shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: shortcutCell.id) {
shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut)
shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut, downloadlinkIndex: 0)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import SwiftUI
struct ReadShortcutVersionView: View {

@EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel
@Environment(\.openURL) var openURL

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

Expand Down Expand Up @@ -41,9 +43,19 @@ struct ReadShortcutVersionView: View {
.foregroundColor(.Gray5)
}
if index != 0 {
let link = "[이전 버전 다운로드](\(shortcut.downloadLink[index]))"
Text(.init(link))
.tint(.Primary)
Button {
if let url = URL(string: shortcut.downloadLink[index]) {
if (shortcutsZipViewModel.userInfo?.downloadedShortcuts.firstIndex(where: { $0.id == shortcut.id })) == nil {
shortcut.numberOfDownload += 1
}
shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut, downloadlinkIndex: index)
openURL(url)
}
} label: {
Text("이전 버전 다운로드")
.Body2()
.foregroundColor(.Primary)
}
}
Divider()
.foregroundColor(.Gray1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct ReadShortcutView: View {
if !isFocused {
if let shortcut = data.shortcut {
Button {
shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut, downloadlinkIndex: 0)
if let url = URL(string: shortcut.downloadLink[0]) {
if (shortcutsZipViewModel.userInfo?.downloadedShortcuts.firstIndex(where: { $0.id == data.shortcutID })) == nil {
data.shortcut?.numberOfDownload += 1
Expand Down Expand Up @@ -127,30 +128,28 @@ struct ReadShortcutView: View {
}
.onDisappear() {
if let shortcut = data.shortcut {
let isAlreadyContained = shortcutsZipViewModel.userInfo?.downloadedShortcuts.firstIndex(where: { $0.id == self.data.shortcutID}) == nil
if isClickDownload && isAlreadyContained {
shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut)
shortcutsZipViewModel.shortcutsUserDownloaded.insert(shortcut, at: 0)

let downloadedShortcut = DownloadedShortcut(id: shortcut.id, downloadLink: shortcut.downloadLink[0])
shortcutsZipViewModel.userInfo?.downloadedShortcuts.insert(downloadedShortcut, at: 0)
}
if isMyLike != isFirstMyLike {
shortcutsZipViewModel.updateNumberOfLike(isMyLike: isMyLike, shortcut: shortcut)
}
}
}
.navigationBarTitleDisplayMode(NavigationBarItem.TitleDisplayMode.inline)
.navigationBarItems(trailing: Menu(content: {
if self.data.shortcut?.author == shortcutsZipViewModel.currentUser() {
myShortcutMenuSection
} else {
otherShortcutMenuSection
}
}, label: {
Image(systemName: "ellipsis")
.foregroundColor(.Gray4)
}))

.navigationBarItems(
leading:
btnBack
.padding(.leading, -8)
.frame(width: 30, alignment: .leading),
trailing: Menu(content: {
if self.data.shortcut?.author == shortcutsZipViewModel.currentUser() {
myShortcutMenuSection
} else {
otherShortcutMenuSection
}
}, label: {
Image(systemName: "ellipsis")
.foregroundColor(.Gray4)
}))
.alert("글 삭제", isPresented: $isTappedDeleteButton) {
Button(role: .cancel) {

Expand Down Expand Up @@ -191,7 +190,6 @@ struct ReadShortcutView: View {
for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: btnBack.padding(.horizontal, -8))
}

var btnBack : some View {
Expand Down