diff --git a/Music/AGENTS.md b/Music/AGENTS.md index cac4acf..3c17996 100644 --- a/Music/AGENTS.md +++ b/Music/AGENTS.md @@ -37,6 +37,9 @@ The iOS app should feel native to iOS and use an Apple Music-inspired **liquid g ## Working principles - The user directs product decisions; keep implementation clear and maintainable, with comments where they clarify non-obvious behavior. +- Before creating or updating a UI, inspect the corresponding screen and shared layout in `~/music-v2`. Mirror its structure and information hierarchy in native SwiftUI, using liquid glass only where it suits the screen and supports clarity. +- Use Apple's standard red accent (`.red`) and otherwise prefer SwiftUI's semantic system colors and materials. Avoid custom color values unless the user asks for them. +- Always add a SwiftUI preview for every UI view you create or update. - Before completing code changes, run available linting and type checking or a build for the affected code. Fix errors introduced by the change, and clearly report any unrelated baseline or environment failures that prevent a clean check. - Reuse the web app’s backend API and data model. Inspect `~/music-v2` when route behavior, authentication, content fields, or feature scope is unclear. - Match the web version’s structure and scope, while making the experience idiomatic to SwiftUI and iOS. diff --git a/Music/Auth/AuthSessionStore.swift b/Music/Auth/AuthSessionStore.swift new file mode 100644 index 0000000..7354f4e --- /dev/null +++ b/Music/Auth/AuthSessionStore.swift @@ -0,0 +1,33 @@ +import Combine + +@MainActor +final class AuthSessionStore: ObservableObject { + enum State { + case checking + case signedOut + case signedIn + } + + @Published private(set) var state: State = .checking + @Published private(set) var isSigningIn = false + + private let api = MusicAPI() + + func restoreSession() async { + guard state == .checking else { return } + + do { + state = try await api.sessionStatus().authenticated ? .signedIn : .signedOut + } catch { + state = .signedOut + } + } + + func signIn(email: String, password: String) async throws { + isSigningIn = true + defer { isSigningIn = false } + + try await api.signIn(email: email, password: password) + state = .signedIn + } +} diff --git a/Music/Auth/SignInView.swift b/Music/Auth/SignInView.swift new file mode 100644 index 0000000..722e0e1 --- /dev/null +++ b/Music/Auth/SignInView.swift @@ -0,0 +1,164 @@ +import SwiftUI + +struct SignInView: View { + @ObservedObject var session: AuthSessionStore + + @State private var email = "" + @State private var password = "" + @State private var errorMessage: String? + @FocusState private var focusedField: Field? + + private enum Field: Hashable { + case email + case password + } + + private var canSubmit: Bool { + !email.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + && !password.isEmpty + && !session.isSigningIn + } + + var body: some View { + GeometryReader { geometry in + ScrollView { + formCard + .frame(maxWidth: 440) + .frame(maxWidth: .infinity) + .padding(.horizontal, 24) + .padding(.vertical, 24) + .frame(minHeight: geometry.size.height) + } + .scrollIndicators(.hidden) + .scrollDismissesKeyboard(.interactively) + } + .background(Color(uiColor: .systemBackground).ignoresSafeArea()) + } + + private var formCard: some View { + VStack(spacing: 18) { + Text("Welcome back!") + .font(.system(size: 24, weight: .semibold)) + .frame(maxWidth: .infinity, alignment: .center) + + VStack(spacing: 13) { + emailField + passwordField + } + + if let errorMessage { + Label(errorMessage, systemImage: "exclamationmark.circle.fill") + .font(.footnote) + .foregroundStyle(.red.opacity(0.9)) + .frame(maxWidth: .infinity, alignment: .leading) + .fixedSize(horizontal: false, vertical: true) + } + + Button(action: submit) { + HStack(spacing: 9) { + if session.isSigningIn { + ProgressView() + .tint(.white) + } + Text(session.isSigningIn ? "Signing in…" : "Sign in") + .font(.headline) + } + .frame(maxWidth: .infinity) + .frame(minHeight: 48) + .foregroundStyle(.white) + .background(.red, in: RoundedRectangle(cornerRadius: 14)) + } + .buttonStyle(.plain) + .disabled(!canSubmit) + .opacity(canSubmit ? 1 : 0.55) + .accessibilityHint("Signs in with your email address and password") + + Text("Sorry, but registrations are closed") + .font(.footnote) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, alignment: .leading) + } + .padding(22) + .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 22)) + .overlay { + RoundedRectangle(cornerRadius: 22) + .strokeBorder(.primary.opacity(0.08), lineWidth: 1) + } + } + + private var emailField: some View { + VStack(alignment: .leading, spacing: 7) { + Text("Email address") + .font(.footnote.weight(.medium)) + .foregroundStyle(.secondary) + + TextField("E-mail", text: $email) + .keyboardType(.emailAddress) + .textContentType(.emailAddress) + .textInputAutocapitalization(.never) + .autocorrectionDisabled() + .submitLabel(.next) + .focused($focusedField, equals: .email) + .onSubmit { focusedField = .password } + .padding(.horizontal, 13) + .frame(minHeight: 48) + .background( + Color(uiColor: .secondarySystemBackground), + in: RoundedRectangle(cornerRadius: 12) + ) + .overlay { + RoundedRectangle(cornerRadius: 12) + .strokeBorder(.primary.opacity(0.08), lineWidth: 1) + } + .accessibilityLabel("Email address") + } + } + + private var passwordField: some View { + VStack(alignment: .leading, spacing: 7) { + Text("Password") + .font(.footnote.weight(.medium)) + .foregroundStyle(.secondary) + + SecureField("Password", text: $password) + .textContentType(.password) + .submitLabel(.go) + .focused($focusedField, equals: .password) + .onSubmit(submit) + .padding(.horizontal, 13) + .frame(minHeight: 48) + .background( + Color(uiColor: .secondarySystemBackground), + in: RoundedRectangle(cornerRadius: 12) + ) + .overlay { + RoundedRectangle(cornerRadius: 12) + .strokeBorder(.primary.opacity(0.08), lineWidth: 1) + } + .accessibilityLabel("Password") + } + } + + private func submit() { + guard canSubmit else { return } + focusedField = nil + errorMessage = nil + + Task { + do { + try await session.signIn( + email: email.trimmingCharacters(in: .whitespacesAndNewlines), + password: password + ) + } catch { + errorMessage = error.localizedDescription + } + } + } +} + +struct SignInView_Previews: PreviewProvider { + static var previews: some View { + SignInView(session: AuthSessionStore()) + } +} diff --git a/Music/ContentView.swift b/Music/ContentView.swift index 775711d..c74c743 100644 --- a/Music/ContentView.swift +++ b/Music/ContentView.swift @@ -1,24 +1,37 @@ -// -// ContentView.swift -// Music -// -// Created by Lucas Täkker on 06/10/2026. -// - import SwiftUI struct ContentView: View { + @StateObject private var session = AuthSessionStore() + var body: some View { - VStack { - Image(systemName: "globe") - .imageScale(.large) - .foregroundStyle(.tint) - Text("Hello, world!") + Group { + switch session.state { + case .checking: + ProgressView() + .tint(.red) + .frame(maxWidth: .infinity, maxHeight: .infinity) + .background(Color(uiColor: .systemBackground)) + .accessibilityLabel("Checking sign-in") + case .signedOut: + SignInView(session: session) + case .signedIn: + VStack(spacing: 12) { + Image(systemName: "music.note.list") + .imageScale(.large) + .foregroundStyle(.tint) + Text("Hello, world!") + } + .padding() + } + } + .task { + await session.restoreSession() } - .padding() } } -#Preview { - ContentView() +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + ContentView() + } }