Skip to content

Instantly share code, notes, and snippets.

View Dimillian's full-sized avatar
📱
SwiftUI EVERYWHERE

Thomas Ricouard Dimillian

📱
SwiftUI EVERYWHERE
View GitHub Profile
var body: some View {
List {
SearchField(searchText: $viewModel.searchText)
ForEach(currentItems) { item in
NavigationLink(destination: ItemDetailView(item: item)) {
ItemRowView(displayMode: self.itemRowsDisplayMode, item: item)
}
}
}
.id(viewModel.sort)
class ItemsViewModel: ObservableObject {
@Published var items: [Item] = []
@Published var sortedItems: [Item] = []
@Published var searchItems: [Item] = []
@Published var searchText = ""
private var itemCancellable: AnyCancellable?
private var searchCancellable: AnyCancellable?
enum Sort: String, CaseIterable {
struct CategoryRowView: View {
let category: Backend.Category
var body: some View {
NavigationLink(destination: ItemsListView(viewModel: ItemsViewModel(category: category))) {
HStack {
Image(category.iconName())
Text(category.label())
}
}
struct HomeView: View {
@EnvironmentObject private var items: Items
private var categories: [(Backend.Category, [Item])] {
items.categories
.map { $0 }
.sorted(by: \.key.rawValue)
.reversed()
}
struct TabbarView: View {
private enum Tab: Int {
case dashboard, items, villagers, collection, turnips
}
@State private var selectedTab = Tab.dashboard
func tabbarItem(text: String, image: String) -> some View {
VStack {
Image(image)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = TabbarView()
.environmentObject(Items())
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
public struct ItemResponse: Codable {
let total: Int
let results: [Item]
}
public struct Item: Codable, Equatable, Identifiable {
public var id: String { name }
public let name: String
public let image: String?
public enum Category: String, CaseIterable {
case housewares, miscellaneous
case wallMounted = "wall-mounted"
case wallpapers, floors, rugs, photos, posters, fencing, tools
case tops, bottoms, dresses, headwear, accessories, socks, shoes, bags
case umbrellas, music, recipes, construction, nookmiles, other
case art, bugs, fish, fossils
}
public class Items: ObservableObject {
@Published public var categories: [Category: [Item]] = [:]
init() {
for category in Category.allCases {
_ = NookPlazaAPIService
.fetch(endpoint: category)
.replaceError(with: ItemResponse(total: 0, results: []))
.eraseToAnyPublisher()
.map{ $0.results }
import Foundation
import Combine
public struct NookPlazaAPIService {
private static let decoder = JSONDecoder()
private static let apiQueue = DispatchQueue(label: "nookazon_api",
qos: .userInitiated,
attributes: .concurrent)