-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
213 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// AppFavorite.swift | ||
// iAppStore | ||
// | ||
// Created by iHTCboy on 2023/6/24. | ||
// Copyright © 2023 37 Mobile Games. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct AppFavorite: Codable { | ||
var appId: String | ||
var regionName: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// AppFavoritesModel.swift | ||
// iAppStore | ||
// | ||
// Created by iHTCboy on 2023/6/24. | ||
// Copyright © 2023 37 Mobile Games. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class AppFavoritesModel: ObservableObject { | ||
|
||
public static let shared = AppFavoritesModel() | ||
|
||
func search(_ appId: String) -> AppFavorite? { | ||
let favorites = appFavorites() | ||
return favorites.first(where: { $0.appId == appId }) | ||
} | ||
|
||
func add(_ app: AppFavorite) { | ||
var favorites = appFavorites() | ||
if let index = favorites.firstIndex(where: { $0.appId == app.appId }) { | ||
favorites[index] = app | ||
} else { | ||
favorites.append(app) | ||
} | ||
saveFavorites(favorites) | ||
} | ||
|
||
/// 删除 | ||
/// - Parameter appId: app id | ||
/// - Returns: 1:删除成功,0:删除失败,-1:未找到删除元素 | ||
@discardableResult | ||
func remove(appId: String) -> Int { | ||
var favorites = appFavorites() | ||
if let index = favorites.firstIndex(where: { $0.appId == appId }) { | ||
favorites.remove(at: index) | ||
saveFavorites(favorites) | ||
return 1 | ||
} | ||
return -1 | ||
} | ||
|
||
func appFavorites() -> [AppFavorite] { | ||
let userDefaults = UserDefaults.standard | ||
if let data = userDefaults.data(forKey: "AppFavorites") { | ||
if let decodedData = try? JSONDecoder().decode([AppFavorite].self, from: data) { | ||
return decodedData | ||
} | ||
} | ||
return [] | ||
} | ||
|
||
func saveFavorites(_ favorites: [AppFavorite]) { | ||
let userDefaults = UserDefaults.standard | ||
if let encodedData = try? JSONEncoder().encode(favorites) { | ||
userDefaults.set(encodedData, forKey: "AppFavorites") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// AppFavoritesView.swift | ||
// iAppStore | ||
// | ||
// Created by iHTCboy on 2023/6/24. | ||
// Copyright © 2023 37 Mobile Games. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct AppFavoritesView: View { | ||
|
||
@StateObject private var appModel = AppDetailModel() | ||
|
||
var body: some View { | ||
VStack { | ||
if appModel.results.count == 0 { | ||
EmptyStateView() | ||
} else { | ||
List { | ||
ForEach(appModel.results, id: \.trackId) { item in | ||
let index = appModel.results.firstIndex { $0.trackId == item.trackId } | ||
NavigationLink(destination: AppDetailView(appId: String(item.trackId), regionName: "中国", item: nil)) { | ||
SearchCellView(index: index ?? 0, item: item).frame(height: 110) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.navigationBarTitle("App 收藏夹") | ||
.navigationBarTitleDisplayMode(.automatic) | ||
.onAppear { | ||
let favorites = AppFavoritesModel.shared.appFavorites() | ||
if appModel.results.count == 0, !favorites.isEmpty { | ||
favorites.forEach { app in | ||
appModel.lookupAppId(app.appId, app.regionName) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct EmptyStateView: View { | ||
var body: some View { | ||
Spacer() | ||
Image(systemName: "list.star") | ||
.font(Font.system(size: 60)) | ||
.foregroundColor(Color.tsmg_tertiaryLabel) | ||
Spacer() | ||
} | ||
} | ||
|
||
|
||
struct AppFavoritesView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
NavigationView { | ||
AppFavoritesView() | ||
} | ||
.onAppear { | ||
AppFavoritesModel.shared.add(AppFavorite(appId: "1669437212", regionName: "中国")) | ||
AppFavoritesModel.shared.add(AppFavorite(appId: "989673964", regionName: "中国")) | ||
AppFavoritesModel.shared.add(AppFavorite(appId: "1142110895", regionName: "中国")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters