feat: ios mobil arayüz tasarımı

This commit is contained in:
2026-02-11 18:06:35 +03:00
parent 69884db0ab
commit 261b2f58cc
42 changed files with 2501 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
import Foundation
import SwiftUI
@MainActor
final class AppRouter: ObservableObject {
enum Route: Hashable {
case addBooks
case category(name: String)
case detail(BookRemote)
}
@Published var isAuthenticated = false
@Published var path: [Route] = []
func resetToHome() {
path.removeAll()
}
}
struct AppDependencies {
let apiClient: APIClientProtocol
let authService: AuthServiceProtocol
let booksService: BooksServiceProtocol
let keychain: KeychainStoreProtocol
let imageCache: ImageCacheProtocol
static func live() -> AppDependencies {
let config = URLSessionConfiguration.default
config.waitsForConnectivity = false
config.timeoutIntervalForRequest = 8
config.timeoutIntervalForResource = 15
let session = URLSession(configuration: config)
let client = APIClient(baseURL: Bundle.main.apiBaseURL, session: session)
return AppDependencies(
apiClient: client,
authService: AuthService(client: client),
booksService: BooksService(client: client),
keychain: KeychainStore(),
imageCache: ImageCache.shared
)
}
}
private struct DependenciesKey: EnvironmentKey {
static let defaultValue = AppDependencies.live()
}
extension EnvironmentValues {
var dependencies: AppDependencies {
get { self[DependenciesKey.self] }
set { self[DependenciesKey.self] = newValue }
}
}

View File

@@ -0,0 +1,72 @@
import SwiftUI
import SwiftData
@main
struct BookibraApp: App {
@StateObject private var router = AppRouter()
private let dependencies = AppDependencies.live()
private let container: ModelContainer
init() {
do {
container = try ModelContainer(for: LibraryBook.self)
} catch {
fatalError("SwiftData container oluşturulamadı: \(error)")
}
}
var body: some Scene {
WindowGroup {
RootView()
.environmentObject(router)
.environment(\.dependencies, dependencies)
.modelContainer(container)
}
}
}
private struct RootView: View {
@EnvironmentObject private var router: AppRouter
@Environment(\.dependencies) private var dependencies
var body: some View {
NavigationStack(path: $router.path) {
Group {
if router.isAuthenticated {
HomeView(viewModel: HomeViewModel())
} else {
AuthView(viewModel: AuthViewModel(authService: dependencies.authService, keychain: dependencies.keychain))
}
}
.navigationDestination(for: AppRouter.Route.self) { route in
switch route {
case .addBooks:
AddBooksView(viewModel: AddBooksViewModel(booksService: dependencies.booksService))
case .category(let name):
CategoryListView(viewModel: CategoryViewModel(categoryName: name))
case .detail(let book):
BookDetailView(viewModel: BookDetailViewModel(book: book))
}
}
}
.task {
await bootstrapSession()
}
}
@MainActor
private func bootstrapSession() async {
let token = dependencies.keychain.read(for: AuthViewModel.tokenKey)
guard let token, !token.isEmpty else {
router.isAuthenticated = false
return
}
do {
_ = try await dependencies.authService.profile(token: token)
router.isAuthenticated = true
} catch {
router.isAuthenticated = false
}
}
}

View File

@@ -0,0 +1,17 @@
import SwiftUI
struct BlurFogOverlay: View {
var body: some View {
Rectangle()
.fill(
LinearGradient(
colors: [Color.clear, Theme.background.opacity(0.82), Theme.background],
startPoint: .top,
endPoint: .bottom
)
)
.background(.ultraThinMaterial)
.ignoresSafeArea(edges: .bottom)
.allowsHitTesting(false)
}
}

View File

@@ -0,0 +1,57 @@
import SwiftUI
struct BookCoverCard: View {
let book: BookRemote
let imageCache: ImageCacheProtocol
var body: some View {
VStack(spacing: 6) {
RemoteImageView(url: book.coverImageUrl, imageCache: imageCache)
.frame(width: 98, height: 145)
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: 10)
.stroke(Color.black.opacity(0.08), lineWidth: 1)
}
.shadow(color: .black.opacity(0.16), radius: 6, y: 4)
Text(book.title)
.font(.caption)
.lineLimit(1)
.foregroundStyle(.primary)
}
.accessibilityElement(children: .combine)
.accessibilityLabel("\(book.title), \(book.authors.joined(separator: ", "))")
}
}
private struct RemoteImageView: View {
let url: URL?
let imageCache: ImageCacheProtocol
@State private var image: UIImage?
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 10)
.fill(LinearGradient(colors: [Color.gray.opacity(0.22), Color.gray.opacity(0.35)], startPoint: .top, endPoint: .bottom))
if let image {
Image(uiImage: image)
.resizable()
.scaledToFill()
} else {
Image(systemName: "book.closed")
.font(.title3)
.foregroundStyle(Color.black.opacity(0.35))
}
}
.task(id: url) {
guard let url else { return }
do {
image = try await imageCache.image(for: url)
} catch {
image = nil
}
}
}
}

View File

@@ -0,0 +1,21 @@
import SwiftUI
struct NetworkErrorView: View {
let message: String
let retryAction: () -> Void
var body: some View {
VStack(spacing: 12) {
Image(systemName: "wifi.exclamationmark")
.font(.title2)
Text(message)
.font(.subheadline)
.multilineTextAlignment(.center)
Button(String(localized: "common.retry"), action: retryAction)
.buttonStyle(.borderedProminent)
}
.padding()
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 16))
.padding(.horizontal)
}
}

View File

@@ -0,0 +1,21 @@
import SwiftUI
struct PrimaryPillButton: View {
let title: String
let action: () -> Void
var body: some View {
Button(action: action) {
Text(title)
.font(.headline)
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
.contentShape(Rectangle())
}
.background(Color.black)
.clipShape(Capsule())
.shadow(color: .black.opacity(0.28), radius: 18, y: 8)
.accessibilityLabel(title)
}
}

View File

@@ -0,0 +1,14 @@
import SwiftUI
struct ScrewView: View {
var body: some View {
ZStack {
Circle()
.fill(Color.black.opacity(0.25))
Circle()
.stroke(Color.white.opacity(0.5), lineWidth: 1)
.padding(1)
}
.frame(width: 9, height: 9)
}
}

View File

@@ -0,0 +1,85 @@
import SwiftUI
struct ShelfSectionView: View {
let title: String
let books: [BookRemote]
let gradient: LinearGradient
let imageCache: ImageCacheProtocol
let onTapCategory: () -> Void
let onTapBook: (BookRemote) -> Void
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Button(action: onTapCategory) {
Text(title)
.font(.title3.weight(.bold))
.foregroundStyle(Theme.textPrimary)
}
Spacer()
Text("\(books.count) books")
.font(.caption)
.foregroundStyle(Theme.textSecondary)
Image(systemName: "chevron.left")
.font(.caption2)
.foregroundStyle(.gray)
Image(systemName: "chevron.right")
.font(.caption2)
.foregroundStyle(.gray)
}
ZStack(alignment: .bottom) {
RoundedRectangle(cornerRadius: 16)
.fill(gradient)
.frame(height: 74)
.shadow(color: .black.opacity(0.12), radius: 10, y: 8)
.overlay {
HStack {
ScrewView()
Spacer()
ScrewView()
}
.padding(.horizontal, 12)
}
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 12) {
if books.isEmpty {
ghostCovers
} else {
ForEach(books) { book in
Button {
onTapBook(book)
} label: {
BookCoverCard(book: book, imageCache: imageCache)
}
}
}
}
.padding(.horizontal, 16)
.padding(.bottom, 18)
}
.frame(height: 190)
}
}
.padding(.horizontal, 20)
}
private var ghostCovers: some View {
HStack(spacing: 10) {
ForEach(0..<3, id: \.self) { _ in
RoundedRectangle(cornerRadius: 10)
.fill(Color.white.opacity(0.5))
.frame(width: 92, height: 140)
.overlay {
Image(systemName: "book")
.foregroundStyle(.white.opacity(0.8))
}
}
Text(String(localized: "home.noBooks"))
.font(.caption)
.foregroundStyle(.secondary)
.frame(width: 130, alignment: .leading)
}
}
}

View File

@@ -0,0 +1,29 @@
import SwiftUI
enum Theme {
static let background = Color(red: 0.97, green: 0.96, blue: 0.94)
static let textPrimary = Color.black
static let textSecondary = Color.gray
static let designShelf = LinearGradient(
colors: [Color(red: 0.87, green: 0.66, blue: 0.45), Color(red: 0.79, green: 0.55, blue: 0.34)],
startPoint: .top,
endPoint: .bottom
)
static let psychologyShelf = LinearGradient(
colors: [Color(red: 0.58, green: 0.67, blue: 0.78), Color(red: 0.45, green: 0.55, blue: 0.66)],
startPoint: .top,
endPoint: .bottom
)
static let novelsShelf = LinearGradient(
colors: [Color.white, Color(red: 0.9, green: 0.9, blue: 0.9)],
startPoint: .top,
endPoint: .bottom
)
static func headerSerif(size: CGFloat) -> Font {
.custom("NewYork-Regular", size: size, relativeTo: .largeTitle)
}
}

View File

@@ -0,0 +1,135 @@
import Foundation
struct BookRemote: Codable, Identifiable, Hashable {
var id: String { isbn13 ?? isbn10 ?? title + (authors.first ?? "") }
let remoteId: String?
let title: String
let authors: [String]
let publishedYear: Int?
let isbn10: String?
let isbn13: String?
let coverImageUrl: URL?
let language: String?
let description: String?
let pageCount: Int?
let categories: [String]
let publisher: String?
let sourceLocale: String?
init(
remoteId: String? = nil,
title: String,
authors: [String] = [],
publishedYear: Int? = nil,
isbn10: String? = nil,
isbn13: String? = nil,
coverImageUrl: URL? = nil,
language: String? = nil,
description: String? = nil,
pageCount: Int? = nil,
categories: [String] = [],
publisher: String? = nil,
sourceLocale: String? = nil
) {
self.remoteId = remoteId
self.title = title
self.authors = authors
self.publishedYear = publishedYear
self.isbn10 = isbn10
self.isbn13 = isbn13
self.coverImageUrl = coverImageUrl
self.language = language
self.description = description
self.pageCount = pageCount
self.categories = categories
self.publisher = publisher
self.sourceLocale = sourceLocale
}
}
struct BookSearchResponse: Decodable {
let items: [BookRemote]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Endpoint 1: /isbn returns { data: { tr: {...}, en: {...} } }
if let localeMap = try? container.decode([String: RawBook].self, forKey: .data) {
self.items = localeMap.map { locale, raw in raw.toBook(locale: locale) }
return
}
// Endpoint 2/3: /title and /filter returns { data: [{ locale, items: [...] }] }
if let groups = try? container.decode([LocaleGroup].self, forKey: .data) {
self.items = groups.flatMap { group in
group.items.map { $0.toBook(locale: group.locale) }
}
return
}
self.items = []
}
private enum CodingKeys: String, CodingKey {
case data
}
}
private struct LocaleGroup: Decodable {
let locale: String
let items: [RawBook]
}
private struct RawBook: Decodable {
let asin: String?
let title: String?
let authorName: String?
let author: String?
let isbn: String?
let thumbImage: String?
let image: String?
let date: String?
let publisher: String?
let page: Int?
let description: String?
let categories: [String]?
let locale: String?
func toBook(locale: String?) -> BookRemote {
let coverRaw = thumbImage ?? image
let authorsText = authorName ?? author ?? ""
let authors = authorsText
.replacingOccurrences(of: "[Yazar]", with: "")
.split(separator: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
let isbnClean = isbn?.filter { $0.isNumber || $0.uppercased() == "X" }
let isbn10 = isbnClean?.count == 10 ? isbnClean : nil
let isbn13 = isbnClean?.count == 13 ? isbnClean : nil
return BookRemote(
remoteId: asin,
title: title ?? "Untitled",
authors: authors,
publishedYear: Self.extractYear(from: date),
isbn10: isbn10,
isbn13: isbn13,
coverImageUrl: coverRaw.flatMap(URL.init(string:)),
language: locale,
description: description,
pageCount: page,
categories: categories ?? [],
publisher: publisher,
sourceLocale: locale
)
}
private static func extractYear(from value: String?) -> Int? {
guard let value else { return nil }
let digits = value.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
guard digits.count >= 4 else { return nil }
return Int(String(digits.prefix(4)))
}
}

View File

@@ -0,0 +1,63 @@
import Foundation
import SwiftData
@Model
final class LibraryBook {
@Attribute(.unique) var localId: UUID
var title: String
var authorsString: String
var coverUrlString: String?
var isbn10: String?
var isbn13: String?
var publishedYear: Int?
var categoriesString: String
var summary: String?
var dateAdded: Date
var language: String?
var sourceLocale: String?
var remotePayloadJson: String?
init(
localId: UUID = UUID(),
title: String,
authorsString: String,
coverUrlString: String? = nil,
isbn10: String? = nil,
isbn13: String? = nil,
publishedYear: Int? = nil,
categoriesString: String = "",
summary: String? = nil,
dateAdded: Date = .now,
language: String? = nil,
sourceLocale: String? = nil,
remotePayloadJson: String? = nil
) {
self.localId = localId
self.title = title
self.authorsString = authorsString
self.coverUrlString = coverUrlString
self.isbn10 = isbn10
self.isbn13 = isbn13
self.publishedYear = publishedYear
self.categoriesString = categoriesString
self.summary = summary
self.dateAdded = dateAdded
self.language = language
self.sourceLocale = sourceLocale
self.remotePayloadJson = remotePayloadJson
}
var authors: [String] {
authorsString
.split(separator: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
}
var categories: [String] {
categoriesString
.split(separator: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
}
}

View File

@@ -0,0 +1,10 @@
import Foundation
struct UserProfile: Codable, Equatable {
struct User: Codable, Equatable {
let id: String
let email: String
}
let user: User
}

View File

@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.580",
"green" : "0.424",
"red" : "0.294"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,53 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1 @@
API_BASE_URL = http://192.168.1.124:8080

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
</dict>
<key>UILaunchScreen</key>
<dict/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
<key>API_BASE_URL</key>
<string>http://192.168.1.124:8080</string>
<key>NSCameraUsageDescription</key>
<string>ISBN barkodu taramak için kameraya erişim gerekiyor.</string>
</dict>
</plist>

View File

@@ -0,0 +1 @@
API_BASE_URL = http://localhost:8080

View File

@@ -0,0 +1,18 @@
"auth.login" = "Login";
"auth.register" = "Register";
"auth.email" = "Email";
"auth.password" = "Password";
"auth.continue" = "Continue";
"home.myFavourite" = "My Favourite";
"home.books" = "BOOKS";
"home.addBooks" = "Add Books";
"home.noBooks" = "No books yet. Tap Add Books.";
"add.title" = "Add Books";
"add.searchByTitle" = "Search by Title";
"add.scanBarcode" = "Scan Barcode";
"add.filter" = "Filter";
"add.searchPlaceholder" = "Type book title";
"detail.add" = "Add to My Books";
"detail.remove" = "Remove from My Books";
"common.retry" = "Retry";
"common.empty" = "No results";

View File

@@ -0,0 +1,21 @@
{
"data": [
{
"locale": "tr",
"items": [
{
"asin": "6053757810",
"title": "Fahrenheit 451",
"authorName": "Ray Bradbury",
"isbn": "9786053757818",
"thumbImage": "https://example.com/cover.jpg",
"date": "2018",
"publisher": "Ithaki",
"page": 280,
"description": "Distopik klasik.",
"categories": ["Novels", "Science Fiction"]
}
]
}
]
}

View File

@@ -0,0 +1,18 @@
"auth.login" = "Giriş Yap";
"auth.register" = "Kayıt Ol";
"auth.email" = "E-posta";
"auth.password" = "Şifre";
"auth.continue" = "Devam Et";
"home.myFavourite" = "My Favourite";
"home.books" = "BOOKS";
"home.addBooks" = "Kitap Ekle";
"home.noBooks" = "Henüz kitap yok. Kitap Ekle'ye dokunun.";
"add.title" = "Kitap Ekle";
"add.searchByTitle" = "Başlığa Göre";
"add.scanBarcode" = "Barkod Tara";
"add.filter" = "Filtre";
"add.searchPlaceholder" = "Kitap adı yazın";
"detail.add" = "Kitaplığıma Ekle";
"detail.remove" = "Kitaplığımdan Kaldır";
"common.retry" = "Tekrar Dene";
"common.empty" = "Sonuç bulunamadı";

View File

@@ -0,0 +1,129 @@
import Foundation
protocol APIClientProtocol {
func get<T: Decodable>(path: String, queryItems: [URLQueryItem], token: String?) async throws -> T
func post<T: Decodable, Body: Encodable>(path: String, body: Body, token: String?) async throws -> T
}
enum APIError: LocalizedError {
case invalidURL
case invalidResponse
case unauthorized
case server(status: Int, message: String)
case decoding(Error)
case transport(Error)
var errorDescription: String? {
switch self {
case .invalidURL: return "Geçersiz URL"
case .invalidResponse: return "Geçersiz sunucu yanıtı"
case .unauthorized: return "Oturum süresi doldu"
case .server(_, let message): return message
case .decoding: return "Sunucu verisi çözümlenemedi"
case .transport(let error): return error.localizedDescription
}
}
}
final class APIClient: APIClientProtocol {
private let baseURL: URL
private let session: URLSession
private let decoder: JSONDecoder
private let encoder: JSONEncoder
init(baseURL: URL, session: URLSession = .shared) {
self.baseURL = baseURL
self.session = session
self.decoder = JSONDecoder()
self.encoder = JSONEncoder()
}
func get<T: Decodable>(path: String, queryItems: [URLQueryItem] = [], token: String? = nil) async throws -> T {
var request = try buildRequest(path: path, method: "GET", queryItems: queryItems, token: token)
request.httpBody = nil
return try await perform(request)
}
func post<T: Decodable, Body: Encodable>(path: String, body: Body, token: String? = nil) async throws -> T {
var request = try buildRequest(path: path, method: "POST", queryItems: [], token: token)
request.httpBody = try encoder.encode(body)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
return try await perform(request)
}
private func buildRequest(path: String, method: String, queryItems: [URLQueryItem], token: String?) throws -> URLRequest {
guard var components = URLComponents(url: baseURL.appendingPathComponent(path), resolvingAgainstBaseURL: false) else {
throw APIError.invalidURL
}
if !queryItems.isEmpty {
components.queryItems = queryItems
}
guard let url = components.url else { throw APIError.invalidURL }
var request = URLRequest(url: url)
request.httpMethod = method
request.timeoutInterval = 20
if let token {
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
return request
}
private func perform<T: Decodable>(_ request: URLRequest) async throws -> T {
#if DEBUG
print("[API] \(request.httpMethod ?? "") \(request.url?.absoluteString ?? "")")
#endif
do {
let (data, response) = try await session.data(for: request)
guard let http = response as? HTTPURLResponse else { throw APIError.invalidResponse }
if http.statusCode == 401 { throw APIError.unauthorized }
guard (200...299).contains(http.statusCode) else {
let message = (try? JSONSerialization.jsonObject(with: data) as? [String: Any])?["message"] as? String ?? "Sunucu hatası"
throw APIError.server(status: http.statusCode, message: message)
}
do {
return try decoder.decode(T.self, from: data)
} catch {
throw APIError.decoding(error)
}
} catch let error as APIError {
throw error
} catch {
throw APIError.transport(error)
}
}
}
extension Bundle {
var apiBaseURL: URL {
let raw = (object(forInfoDictionaryKey: "API_BASE_URL") as? String)?
.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
// 1) Normal case: full URL in Info.plist / xcconfig.
if let url = URL(string: raw), let host = url.host, !host.isEmpty {
return url
}
// 2) If scheme is missing (e.g. "192.168.1.124:8080"), prepend http://.
if !raw.isEmpty, !raw.contains("://"),
let url = URL(string: "http://\(raw)"),
let host = url.host, !host.isEmpty {
return url
}
// 3) Device-local fallback for current dev network.
if let fallback = URL(string: "http://192.168.1.124:8080") {
#if DEBUG
print("[API] Invalid API_BASE_URL='\(raw)'. Falling back to \(fallback.absoluteString)")
#endif
return fallback
}
// 4) Last resort.
return URL(string: "http://localhost:8080")!
}
}

View File

@@ -0,0 +1,42 @@
import Foundation
struct AuthResponse: Codable {
struct User: Codable {
let id: String
let email: String
}
let token: String
let user: User
}
protocol AuthServiceProtocol {
func login(email: String, password: String) async throws -> AuthResponse
func register(email: String, password: String) async throws -> AuthResponse
func profile(token: String) async throws -> UserProfile
}
final class AuthService: AuthServiceProtocol {
private let client: APIClientProtocol
init(client: APIClientProtocol) {
self.client = client
}
func login(email: String, password: String) async throws -> AuthResponse {
try await client.post(path: "api/auth/login", body: Credentials(email: email, password: password), token: nil)
}
func register(email: String, password: String) async throws -> AuthResponse {
try await client.post(path: "api/auth/register", body: Credentials(email: email, password: password), token: nil)
}
func profile(token: String) async throws -> UserProfile {
try await client.get(path: "api/auth/profile", queryItems: [], token: token)
}
}
private struct Credentials: Codable {
let email: String
let password: String
}

View File

@@ -0,0 +1,52 @@
import Foundation
protocol BooksServiceProtocol {
func searchByTitle(_ title: String, locales: String) async throws -> [BookRemote]
func searchByISBN(_ isbn: String, locales: String) async throws -> [BookRemote]
func filter(title: String, year: String, locales: String) async throws -> [BookRemote]
}
final class BooksService: BooksServiceProtocol {
private let client: APIClientProtocol
init(client: APIClientProtocol) {
self.client = client
}
func searchByTitle(_ title: String, locales: String = "tr,en") async throws -> [BookRemote] {
let response: BookSearchResponse = try await client.get(
path: "api/books/title",
queryItems: [
.init(name: "title", value: title),
.init(name: "locales", value: locales)
],
token: nil
)
return response.items
}
func searchByISBN(_ isbn: String, locales: String = "tr,en") async throws -> [BookRemote] {
let response: BookSearchResponse = try await client.get(
path: "api/books/isbn/\(isbn)",
queryItems: [
.init(name: "locales", value: locales),
.init(name: "withGemini", value: "false")
],
token: nil
)
return response.items
}
func filter(title: String, year: String, locales: String = "tr,en") async throws -> [BookRemote] {
let response: BookSearchResponse = try await client.get(
path: "api/books/filter",
queryItems: [
.init(name: "title", value: title),
.init(name: "published", value: year),
.init(name: "locales", value: locales)
],
token: nil
)
return response.items
}
}

View File

@@ -0,0 +1,34 @@
import Foundation
import UIKit
protocol ImageCacheProtocol {
func image(for url: URL) async throws -> UIImage
}
final class ImageCache: ImageCacheProtocol {
static let shared = ImageCache()
private let cache = NSCache<NSURL, UIImage>()
private let session: URLSession
init(session: URLSession = .shared) {
self.session = session
cache.countLimit = 200
}
func image(for url: URL) async throws -> UIImage {
if let existing = cache.object(forKey: url as NSURL) {
return existing
}
let request = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 20)
let (data, _) = try await session.data(for: request)
guard let image = UIImage(data: data) else {
throw APIError.invalidResponse
}
cache.setObject(image, forKey: url as NSURL)
return image
}
}

View File

@@ -0,0 +1,47 @@
import Foundation
import Security
protocol KeychainStoreProtocol {
func save(_ value: String, for key: String) -> Bool
func read(for key: String) -> String?
func delete(for key: String) -> Bool
}
final class KeychainStore: KeychainStoreProtocol {
func save(_ value: String, for key: String) -> Bool {
let data = Data(value.utf8)
let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: key,
kSecValueData: data
]
SecItemDelete(query as CFDictionary)
return SecItemAdd(query as CFDictionary, nil) == errSecSuccess
}
func read(for key: String) -> String? {
let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: key,
kSecReturnData: true,
kSecMatchLimit: kSecMatchLimitOne
]
var item: CFTypeRef?
guard SecItemCopyMatching(query as CFDictionary, &item) == errSecSuccess,
let data = item as? Data else {
return nil
}
return String(data: data, encoding: .utf8)
}
func delete(for key: String) -> Bool {
let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: key
]
return SecItemDelete(query as CFDictionary) == errSecSuccess
}
}

View File

@@ -0,0 +1,88 @@
import Foundation
@MainActor
final class AddBooksViewModel: ObservableObject {
enum Mode: String, CaseIterable {
case title
case scan
case filter
var title: String {
switch self {
case .title: return String(localized: "add.searchByTitle")
case .scan: return String(localized: "add.scanBarcode")
case .filter: return String(localized: "add.filter")
}
}
}
@Published var mode: Mode = .title
@Published var titleQuery = ""
@Published var filterTitle = ""
@Published var filterYear = ""
@Published var results: [BookRemote] = []
@Published var isLoading = false
@Published var errorMessage: String?
private var debounceTask: Task<Void, Never>?
private let booksService: BooksServiceProtocol
init(booksService: BooksServiceProtocol) {
self.booksService = booksService
}
func titleChanged() {
debounceTask?.cancel()
let query = titleQuery.trimmingCharacters(in: .whitespacesAndNewlines)
guard query.count >= 2 else {
results = []
return
}
debounceTask = Task {
try? await Task.sleep(for: .milliseconds(400))
await searchByTitle(query)
}
}
func searchByTitle(_ query: String? = nil) async {
let value = query ?? titleQuery
guard !value.isEmpty else { return }
isLoading = true
defer { isLoading = false }
do {
results = try await booksService.searchByTitle(value, locales: "tr,en")
errorMessage = nil
} catch {
errorMessage = error.localizedDescription
}
}
func searchByISBN(_ isbn: String) async {
guard !isbn.isEmpty else { return }
isLoading = true
defer { isLoading = false }
do {
results = try await booksService.searchByISBN(isbn, locales: "tr,en")
errorMessage = nil
} catch {
errorMessage = error.localizedDescription
}
}
func applyFilter() async {
guard !filterTitle.isEmpty, !filterYear.isEmpty else { return }
isLoading = true
defer { isLoading = false }
do {
results = try await booksService.filter(title: filterTitle, year: filterYear, locales: "tr,en")
errorMessage = nil
} catch {
errorMessage = error.localizedDescription
}
}
}

View File

@@ -0,0 +1,53 @@
import Foundation
@MainActor
final class AuthViewModel: ObservableObject {
enum Mode: String, CaseIterable {
case login
case register
}
static let tokenKey = "bookibra.jwt"
static let emailKey = "bookibra.email"
@Published var mode: Mode = .login
@Published var email = ""
@Published var password = ""
@Published var isLoading = false
@Published var errorMessage: String?
private let authService: AuthServiceProtocol
private let keychain: KeychainStoreProtocol
init(authService: AuthServiceProtocol, keychain: KeychainStoreProtocol) {
self.authService = authService
self.keychain = keychain
}
func submit(onSuccess: @escaping () -> Void) async {
guard !email.isEmpty, !password.isEmpty else {
errorMessage = "E-posta ve şifre gerekli"
return
}
isLoading = true
defer { isLoading = false }
do {
let response: AuthResponse
switch mode {
case .login:
response = try await authService.login(email: email, password: password)
case .register:
response = try await authService.register(email: email, password: password)
}
_ = try await authService.profile(token: response.token)
_ = keychain.save(response.token, for: Self.tokenKey)
_ = keychain.save(response.user.email, for: Self.emailKey)
onSuccess()
} catch {
errorMessage = error.localizedDescription
}
}
}

View File

@@ -0,0 +1,55 @@
import Foundation
import SwiftData
import UIKit
@MainActor
final class BookDetailViewModel: ObservableObject {
@Published var book: BookRemote
@Published var isInLibrary = false
init(book: BookRemote) {
self.book = book
}
func refresh(context: ModelContext) {
let all = (try? context.fetch(FetchDescriptor<LibraryBook>())) ?? []
isInLibrary = all.contains(where: { local in
match(local: local, remote: book)
})
}
func toggleLibrary(context: ModelContext) {
let all = (try? context.fetch(FetchDescriptor<LibraryBook>())) ?? []
if let existing = all.first(where: { match(local: $0, remote: book) }) {
context.delete(existing)
isInLibrary = false
UINotificationFeedbackGenerator().notificationOccurred(.warning)
} else {
let local = LibraryBook(
title: book.title,
authorsString: book.authors.joined(separator: ", "),
coverUrlString: book.coverImageUrl?.absoluteString,
isbn10: book.isbn10,
isbn13: book.isbn13,
publishedYear: book.publishedYear,
categoriesString: book.categories.joined(separator: ", "),
summary: book.description,
language: book.language,
sourceLocale: book.sourceLocale,
remotePayloadJson: nil
)
context.insert(local)
isInLibrary = true
UINotificationFeedbackGenerator().notificationOccurred(.success)
}
try? context.save()
}
private func match(local: LibraryBook, remote: BookRemote) -> Bool {
if let lhs = local.isbn13, let rhs = remote.isbn13 { return lhs == rhs }
if let lhs = local.isbn10, let rhs = remote.isbn10 { return lhs == rhs }
return local.title == remote.title
}
}

View File

@@ -0,0 +1,39 @@
import Foundation
@MainActor
final class CategoryViewModel: ObservableObject {
enum SortOption: String, CaseIterable {
case recentlyAdded = "Recently Added"
case titleAZ = "Title A-Z"
case author = "Author"
}
let categoryName: String
@Published var searchText = ""
@Published var sortOption: SortOption = .recentlyAdded
init(categoryName: String) {
self.categoryName = categoryName
}
func books(from allBooks: [LibraryBook]) -> [LibraryBook] {
var filtered = allBooks.filter { $0.categories.contains(categoryName) || (categoryName == "Design" && $0.categories.isEmpty) }
if !searchText.isEmpty {
filtered = filtered.filter {
$0.title.localizedCaseInsensitiveContains(searchText)
|| $0.authorsString.localizedCaseInsensitiveContains(searchText)
}
}
switch sortOption {
case .recentlyAdded:
filtered.sort { $0.dateAdded > $1.dateAdded }
case .titleAZ:
filtered.sort { $0.title.localizedCaseInsensitiveCompare($1.title) == .orderedAscending }
case .author:
filtered.sort { $0.authorsString.localizedCaseInsensitiveCompare($1.authorsString) == .orderedAscending }
}
return filtered
}
}

View File

@@ -0,0 +1,61 @@
import Foundation
import SwiftUI
struct ShelfCategory: Identifiable {
let id: String
let name: String
let books: [BookRemote]
}
@MainActor
final class HomeViewModel: ObservableObject {
@Published var categories: [ShelfCategory] = []
func refresh(from localBooks: [LibraryBook]) {
var map: [String: [BookRemote]] = [:]
for local in localBooks {
let targets = local.categories.isEmpty ? ["Design"] : local.categories
let remote = Self.makeRemote(from: local)
for name in targets {
map[name, default: []].append(remote)
}
}
let preferred = ["Design", "Psychology", "Novels"]
var built = preferred.map { name in
ShelfCategory(id: name, name: name, books: map[name] ?? [])
}
let extras = map.keys
.filter { !preferred.contains($0) }
.sorted()
.map { ShelfCategory(id: $0, name: $0, books: map[$0] ?? []) }
built.append(contentsOf: extras)
categories = built
}
func gradient(for name: String) -> LinearGradient {
switch name.lowercased() {
case "design": return Theme.designShelf
case "psychology": return Theme.psychologyShelf
case "novels": return Theme.novelsShelf
default: return Theme.novelsShelf
}
}
private static func makeRemote(from local: LibraryBook) -> BookRemote {
BookRemote(
title: local.title,
authors: local.authors,
publishedYear: local.publishedYear,
isbn10: local.isbn10,
isbn13: local.isbn13,
coverImageUrl: local.coverUrlString.flatMap(URL.init(string:)),
language: local.language,
description: local.summary,
categories: local.categories,
sourceLocale: local.sourceLocale
)
}
}

View File

@@ -0,0 +1,115 @@
import SwiftUI
struct AddBooksView: View {
@EnvironmentObject private var router: AppRouter
@ObservedObject var viewModel: AddBooksViewModel
var body: some View {
VStack(spacing: 12) {
Picker("Mode", selection: $viewModel.mode) {
ForEach(AddBooksViewModel.Mode.allCases, id: \.self) { mode in
Text(mode.title).tag(mode)
}
}
.pickerStyle(.segmented)
Group {
switch viewModel.mode {
case .title:
titleSearch
case .scan:
scanSearch
case .filter:
filterSearch
}
}
if let error = viewModel.errorMessage {
NetworkErrorView(message: error) {
Task { await viewModel.searchByTitle() }
}
}
List(viewModel.results, id: \.id) { book in
Button {
router.path.append(.detail(book))
} label: {
HStack(spacing: 12) {
AsyncImage(url: book.coverImageUrl) { phase in
if let image = phase.image {
image.resizable().scaledToFill()
} else {
RoundedRectangle(cornerRadius: 8).fill(.gray.opacity(0.2))
}
}
.frame(width: 48, height: 72)
.clipShape(RoundedRectangle(cornerRadius: 8))
VStack(alignment: .leading, spacing: 4) {
Text(book.title)
.font(.headline)
.lineLimit(2)
Text(book.authors.joined(separator: ", "))
.font(.subheadline)
.foregroundStyle(.secondary)
.lineLimit(1)
if let year = book.publishedYear {
Text("\(year)")
.font(.caption)
.foregroundStyle(.secondary)
}
}
}
}
}
.listStyle(.plain)
.overlay {
if viewModel.results.isEmpty, !viewModel.isLoading {
Text(String(localized: "common.empty"))
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
}
.padding(.horizontal, 16)
.navigationTitle(String(localized: "add.title"))
.navigationBarTitleDisplayMode(.inline)
.overlay {
if viewModel.isLoading {
ProgressView()
.controlSize(.large)
}
}
}
private var titleSearch: some View {
TextField(String(localized: "add.searchPlaceholder"), text: $viewModel.titleQuery)
.textFieldStyle(.roundedBorder)
.onChange(of: viewModel.titleQuery) { _, _ in
viewModel.titleChanged()
}
}
private var scanSearch: some View {
BarcodeScannerView { isbn in
Task { await viewModel.searchByISBN(isbn) }
}
.frame(height: 260)
.clipShape(RoundedRectangle(cornerRadius: 16))
}
private var filterSearch: some View {
VStack(spacing: 8) {
TextField("Title", text: $viewModel.filterTitle)
.textFieldStyle(.roundedBorder)
TextField("YYYY", text: $viewModel.filterYear)
.textFieldStyle(.roundedBorder)
.keyboardType(.numberPad)
Button("Apply") {
Task { await viewModel.applyFilter() }
}
.buttonStyle(.borderedProminent)
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
}

View File

@@ -0,0 +1,154 @@
import SwiftUI
import AVFoundation
import VisionKit
struct BarcodeScannerView: View {
let onScanned: (String) -> Void
var body: some View {
Group {
if DataScannerViewController.isSupported, DataScannerViewController.isAvailable {
DataScannerRepresentable(onScanned: onScanned)
} else {
AVScannerRepresentable(onScanned: onScanned)
}
}
.overlay {
RoundedRectangle(cornerRadius: 16)
.stroke(Color.white.opacity(0.75), lineWidth: 2)
}
.accessibilityLabel("ISBN barkod tarayıcı")
}
}
@available(iOS 16.0, *)
private struct DataScannerRepresentable: UIViewControllerRepresentable {
let onScanned: (String) -> Void
func makeCoordinator() -> Coordinator { Coordinator(onScanned: onScanned) }
func makeUIViewController(context: Context) -> DataScannerViewController {
let vc = DataScannerViewController(
recognizedDataTypes: [.barcode(symbologies: [.ean8, .ean13, .upce, .code128])],
qualityLevel: .balanced,
recognizesMultipleItems: false,
isHighFrameRateTrackingEnabled: true,
isPinchToZoomEnabled: true,
isGuidanceEnabled: true,
isHighlightingEnabled: true
)
vc.delegate = context.coordinator
try? vc.startScanning()
return vc
}
func updateUIViewController(_ uiViewController: DataScannerViewController, context: Context) {}
final class Coordinator: NSObject, DataScannerViewControllerDelegate {
let onScanned: (String) -> Void
private var lastISBN: String?
private var lastEmitAt: Date = .distantPast
init(onScanned: @escaping (String) -> Void) {
self.onScanned = onScanned
}
func dataScanner(_ dataScanner: DataScannerViewController, didTapOn item: RecognizedItem) {
guard case .barcode(let code) = item,
let payload = code.payloadStringValue,
let normalized = ISBNNormalizer.normalize(payload) else { return }
emitIfNeeded(normalized)
}
func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) {
guard let first = addedItems.first,
case .barcode(let code) = first,
let payload = code.payloadStringValue,
let normalized = ISBNNormalizer.normalize(payload) else { return }
emitIfNeeded(normalized)
}
private func emitIfNeeded(_ isbn: String) {
let now = Date()
if lastISBN == isbn, now.timeIntervalSince(lastEmitAt) < 1.5 {
return
}
lastISBN = isbn
lastEmitAt = now
onScanned(isbn)
}
}
}
private struct AVScannerRepresentable: UIViewRepresentable {
let onScanned: (String) -> Void
func makeUIView(context: Context) -> ScannerPreviewView {
let view = ScannerPreviewView()
context.coordinator.configure(preview: view)
return view
}
func updateUIView(_ uiView: ScannerPreviewView, context: Context) {}
func makeCoordinator() -> Coordinator { Coordinator(onScanned: onScanned) }
final class Coordinator: NSObject, AVCaptureMetadataOutputObjectsDelegate {
private let session = AVCaptureSession()
private let sessionQueue = DispatchQueue(label: "bookibra.av.capture.session")
private let onScanned: (String) -> Void
private var lastISBN: String?
private var lastEmitAt: Date = .distantPast
init(onScanned: @escaping (String) -> Void) {
self.onScanned = onScanned
}
func configure(preview: ScannerPreviewView) {
guard let device = AVCaptureDevice.default(for: .video),
let input = try? AVCaptureDeviceInput(device: device) else { return }
if session.canAddInput(input) { session.addInput(input) }
let output = AVCaptureMetadataOutput()
if session.canAddOutput(output) {
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: .main)
output.metadataObjectTypes = [.ean8, .ean13, .upce, .code128]
}
preview.previewLayer.session = session
preview.previewLayer.videoGravity = .resizeAspectFill
sessionQueue.async { [session] in
session.startRunning()
}
}
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
guard let code = metadataObjects.compactMap({ $0 as? AVMetadataMachineReadableCodeObject }).first,
let value = code.stringValue,
let normalized = ISBNNormalizer.normalize(value) else { return }
let now = Date()
if lastISBN == normalized, now.timeIntervalSince(lastEmitAt) < 1.5 {
return
}
lastISBN = normalized
lastEmitAt = now
onScanned(normalized)
}
}
}
private final class ScannerPreviewView: UIView {
override class var layerClass: AnyClass { AVCaptureVideoPreviewLayer.self }
var previewLayer: AVCaptureVideoPreviewLayer { layer as! AVCaptureVideoPreviewLayer }
}
enum ISBNNormalizer {
static func normalize(_ value: String) -> String? {
let cleaned = value.uppercased().filter { $0.isNumber || $0 == "X" }
if cleaned.count == 13 { return cleaned }
if cleaned.count == 10 { return cleaned }
return nil
}
}

View File

@@ -0,0 +1,71 @@
import SwiftUI
struct AuthView: View {
@EnvironmentObject private var router: AppRouter
@ObservedObject var viewModel: AuthViewModel
var body: some View {
VStack(spacing: 24) {
Spacer()
Text("Bookibra")
.font(Theme.headerSerif(size: 48))
.foregroundStyle(.black)
Picker("Mode", selection: $viewModel.mode) {
Text(String(localized: "auth.login")).tag(AuthViewModel.Mode.login)
Text(String(localized: "auth.register")).tag(AuthViewModel.Mode.register)
}
.pickerStyle(.segmented)
.padding(.horizontal, 24)
VStack(spacing: 14) {
TextField(String(localized: "auth.email"), text: $viewModel.email)
.textInputAutocapitalization(.never)
.keyboardType(.emailAddress)
.padding()
.background(Color.white, in: RoundedRectangle(cornerRadius: 12))
SecureField(String(localized: "auth.password"), text: $viewModel.password)
.padding()
.background(Color.white, in: RoundedRectangle(cornerRadius: 12))
}
.padding(.horizontal, 24)
if let error = viewModel.errorMessage {
Text(error)
.font(.footnote)
.foregroundStyle(.red)
.padding(.horizontal, 24)
.multilineTextAlignment(.center)
}
Button {
Task {
await viewModel.submit {
router.isAuthenticated = true
}
}
} label: {
if viewModel.isLoading {
ProgressView()
.tint(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
} else {
Text(String(localized: "auth.continue"))
.font(.headline)
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
}
}
.background(Color.black)
.clipShape(Capsule())
.padding(.horizontal, 24)
Spacer()
}
.background(Theme.background.ignoresSafeArea())
}
}

View File

@@ -0,0 +1,71 @@
import SwiftUI
import SwiftData
struct CategoryListView: View {
@EnvironmentObject private var router: AppRouter
@Query(sort: \LibraryBook.dateAdded, order: .reverse) private var allBooks: [LibraryBook]
@ObservedObject var viewModel: CategoryViewModel
var body: some View {
let books = viewModel.books(from: allBooks)
VStack {
HStack {
TextField("Search", text: $viewModel.searchText)
.textFieldStyle(.roundedBorder)
Menu {
ForEach(CategoryViewModel.SortOption.allCases, id: \.self) { option in
Button(option.rawValue) { viewModel.sortOption = option }
}
} label: {
Image(systemName: "arrow.up.arrow.down.circle")
.font(.title3)
}
}
.padding(.horizontal)
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 104), spacing: 12)], spacing: 16) {
ForEach(books, id: \.localId) { book in
let remote = BookRemote(
title: book.title,
authors: book.authors,
publishedYear: book.publishedYear,
isbn10: book.isbn10,
isbn13: book.isbn13,
coverImageUrl: book.coverUrlString.flatMap(URL.init(string:)),
language: book.language,
description: book.summary,
categories: book.categories
)
Button {
router.path.append(.detail(remote))
} label: {
VStack(alignment: .leading, spacing: 6) {
AsyncImage(url: remote.coverImageUrl) { phase in
if let image = phase.image {
image.resizable().scaledToFill()
} else {
RoundedRectangle(cornerRadius: 10).fill(Color.gray.opacity(0.2))
}
}
.frame(height: 154)
.clipShape(RoundedRectangle(cornerRadius: 10))
Text(book.title)
.font(.caption)
.lineLimit(2)
.foregroundStyle(.primary)
}
}
}
}
.padding(.horizontal)
}
}
.navigationTitle(viewModel.categoryName)
.navigationBarTitleDisplayMode(.inline)
.background(Theme.background.ignoresSafeArea())
}
}

View File

@@ -0,0 +1,58 @@
import SwiftUI
import SwiftData
struct BookDetailView: View {
@Environment(\.modelContext) private var modelContext
@ObservedObject var viewModel: BookDetailViewModel
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
AsyncImage(url: viewModel.book.coverImageUrl) { phase in
if let image = phase.image {
image.resizable().scaledToFill()
} else {
RoundedRectangle(cornerRadius: 16).fill(.gray.opacity(0.2))
}
}
.frame(maxWidth: .infinity)
.frame(height: 320)
.clipShape(RoundedRectangle(cornerRadius: 16))
Text(viewModel.book.title)
.font(.title.bold())
Text(viewModel.book.authors.joined(separator: ", "))
.font(.headline)
.foregroundStyle(.secondary)
if let year = viewModel.book.publishedYear {
Text("\(year)")
.font(.subheadline)
}
if !viewModel.book.categories.isEmpty {
Text(viewModel.book.categories.joined(separator: ""))
.font(.subheadline)
.foregroundStyle(.secondary)
}
if let description = viewModel.book.description {
Text(description)
.font(.body)
}
PrimaryPillButton(
title: viewModel.isInLibrary ? String(localized: "detail.remove") : String(localized: "detail.add")
) {
viewModel.toggleLibrary(context: modelContext)
}
.padding(.top, 8)
}
.padding(20)
}
.background(Theme.background.ignoresSafeArea())
.task {
viewModel.refresh(context: modelContext)
}
}
}

View File

@@ -0,0 +1,73 @@
import SwiftUI
import SwiftData
struct HomeView: View {
@EnvironmentObject private var router: AppRouter
@Environment(\.dependencies) private var dependencies
@Query(sort: \LibraryBook.dateAdded, order: .reverse) private var libraryBooks: [LibraryBook]
@StateObject private var viewModel: HomeViewModel
init(viewModel: HomeViewModel) {
_viewModel = StateObject(wrappedValue: viewModel)
}
var body: some View {
ScrollView {
VStack(spacing: 28) {
header
ForEach(viewModel.categories) { category in
ShelfSectionView(
title: category.name,
books: category.books,
gradient: viewModel.gradient(for: category.name),
imageCache: dependencies.imageCache,
onTapCategory: { router.path.append(.category(name: category.name)) },
onTapBook: { router.path.append(.detail($0)) }
)
}
}
.padding(.top, 16)
.padding(.bottom, 120)
}
.background(Theme.background.ignoresSafeArea())
.safeAreaInset(edge: .bottom, spacing: 0) {
ZStack {
BlurFogOverlay()
.frame(height: 96)
PrimaryPillButton(title: String(localized: "home.addBooks")) {
router.path.append(.addBooks)
}
.padding(.horizontal, 24)
.padding(.bottom, 12)
}
.frame(height: 100)
}
.onAppear {
viewModel.refresh(from: libraryBooks)
}
.onChange(of: libraryBooks.map(\.localId)) { _, _ in
viewModel.refresh(from: libraryBooks)
}
.task(id: libraryBooks.count) {
viewModel.refresh(from: libraryBooks)
}
}
private var header: some View {
VStack(spacing: 4) {
Text(String(localized: "home.myFavourite"))
.font(.footnote.weight(.light))
.kerning(1.2)
.foregroundStyle(Color.black.opacity(0.7))
Text(String(localized: "home.books"))
.font(Theme.headerSerif(size: 56).weight(.bold))
.foregroundStyle(.black)
.kerning(1)
}
.frame(maxWidth: .infinity)
.padding(.top, 12)
.accessibilityElement(children: .combine)
}
}