Skip to content

Commit

Permalink
feat: 앱업데이트 버튼 추가 및 앱스토어 연결 (#68)
Browse files Browse the repository at this point in the history
feat: 앱업데이트 버튼 추가 및 앱스토어 연결 (#68)
  • Loading branch information
hryeong66 committed Aug 19, 2024
1 parent 255e844 commit e6318d3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
30 changes: 28 additions & 2 deletions Projects/Features/Setting/Sources/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public struct SettingView: View {
.padding(.top, 50)

memeLogoImage
appNameAndVersion
.padding(.bottom, 50)
appDescriptionView

Divider()
.padding(.horizontal, 10)
Expand Down Expand Up @@ -69,6 +68,19 @@ public struct SettingView: View {
.padding(.top, 50)
}

var appDescriptionView: some View {
VStack {
if viewModel.state.needUpdate {
appNameAndVersion
.padding(.bottom, 16)
appUpdateButton
} else {
appNameAndVersion
}
}
.padding(.bottom, 50)
}

var appNameAndVersion: some View {
VStack {
Text("파밈")
Expand All @@ -81,6 +93,20 @@ public struct SettingView: View {
}
}

var appUpdateButton: some View {
Link(destination: viewModel.appStoreUrl) {
Text("앱 업데이트하기")
.font(Font.Body.Large.semiBold)
.foregroundStyle(Color.Text.inverse)
.padding(.vertical, 12)
.padding(.horizontal, 16)
.background {
RoundedCorners(radius: 10, corners: .allCorners)
.foregroundStyle(Color.Background.primary)
}
}
}

var settingListView: some View {
ForEach(viewModel.state.settingList) { settingType in
SettingListItemView(
Expand Down
61 changes: 38 additions & 23 deletions Projects/Features/Setting/Sources/SettingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final public class SettingViewModel: ViewModelType, ObservableObject {

public enum Action {
case naviBackButtonTapped
case checkNeedUpdate
}

public struct State {
Expand All @@ -28,14 +29,20 @@ final public class SettingViewModel: ViewModelType, ObservableObject {
// MARK: - Properties
weak var router: SettingRouting?
@Published public var state: State
private let appId: String = "6532618484"
var appStoreUrl: URL {
return URL(string: "itms-apps://itunes.apple.com/app/\(appId)")!
}

// MARK: - Initializers
public init(router: SettingRouting? = nil) {
self.router = router
self.state = State()
self.state.needUpdate = self.checkNeedUpdate()
self.state.currnetAppVersion = self.getCurrentAppVersion()

self.state.currnetAppVersion = "v." + self.getCurrentAppVersion()
self.initSettingList()

self.dispatch(type: .checkNeedUpdate)
}

private func initSettingList() {
Expand All @@ -50,11 +57,14 @@ final public class SettingViewModel: ViewModelType, ObservableObject {
}

// MARK: - Methods
@MainActor
public func dispatch(type: Action) {
switch type {
case .naviBackButtonTapped:
router?.popView()
Task { @MainActor in
switch type {
case .naviBackButtonTapped:
router?.popView()
case .checkNeedUpdate:
self.state.needUpdate = await checkNeedUpdate()
}
}
}

Expand All @@ -63,30 +73,35 @@ final public class SettingViewModel: ViewModelType, ObservableObject {
return currentVersion ?? ""
}

private func checkNeedUpdate() -> Bool {
guard let appStoreVersion = self.getLatestVersion() else { return false }
let currentVersion = getCurrentAppVersion()
print("Setting | currentVersion = \(currentVersion)")
print("Setting | appStoreVersion = \(appStoreVersion)")

private func checkNeedUpdate() async -> Bool {
guard let appStoreVersion = await self.getLatestVersion() else { return false }
let currentVersion = self.getCurrentAppVersion()
return currentVersion != appStoreVersion
}

private func getLatestVersion() -> String? {
// guard
// let bundleIdentifier = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String,
// let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleIdentifier)"),
// let data = try? Data(contentsOf: url),
// let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
// let results = json["results"] as? [[String: Any]], !results.isEmpty,
// let appStoreVersion = results[0]["version"] as? String else {
// return nil
// }
// return appStoreVersion
private func getLatestVersion() async -> String? {
guard let bundleIdentifier = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String,
let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleIdentifier)&country=kr") else {
return nil
}

do {
let (data, _) = try await URLSession.shared.data(from: url)
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let results = json["results"] as? [[String: Any]], !results.isEmpty,
let appStoreVersion = results[0]["version"] as? String {
return appStoreVersion
}
} catch {
print("Failed to fetch latest version: \(error)")
}

return nil
}

}


public struct SettingType: Identifiable {
public let id = UUID()
public let url: String
Expand Down

0 comments on commit e6318d3

Please sign in to comment.