Skip to content

Conversation

Copy link

Copilot AI commented Jan 17, 2026

Implements normalized state management architecture separating data models (entities), state container (AppStore), mutation logic (services), and presentation (views).

Architecture

Entities (Store/Entities.swift)

  • Unified models: Track, Album, Artist, Playlist, Device
  • Sendable, ID-based relationships, no nested objects
  • Denormalized display fields (artistName, albumName, imageURL) to avoid lookup overhead

AppStore (Store/AppStore.swift)

  • Normalized entity dictionaries keyed by ID
  • ID arrays for ordered collections (userPlaylistIds, savedTrackIds)
  • Set-based favorites for O(1) lookup (favoriteTrackIds)
  • Computed properties return entity arrays from IDs

Services (Store/Services/)

  • Handle API calls, convert responses to entities, update AppStore
  • Task deduplication in Album/Artist/Playlist services (prevents race conditions during 2→3 column layout transitions)
  • Optimistic updates with rollback (e.g., favorite toggling)

Usage Pattern

struct AlbumDetailView: View {
    @Environment(AppStore.self) private var store
    @Environment(AlbumService.self) private var albumService
    
    private var tracks: [Track] {
        store.albums[albumId]?.trackIds.compactMap { store.tracks[$0] } ?? []
    }
    
    func loadTracks() async {
        _ = try? await albumService.getAlbumTracks(albumId: id, accessToken: token)
        // Tracks now in store.tracks, view updates automatically
    }
}

Service Initialization

Services with task deduplication or subscriptions stored as @State in LoggedInView to survive view recreation. Stateless services created on-demand—all state lives in AppStore.

Conversions

API response types (APITrack, APIAlbum) separated from entities. Extension-based conversions in EntityConversions.swift handle context-aware initialization (e.g., tracks from album endpoint lack album metadata).


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI mentioned this pull request Jan 17, 2026
Copilot AI changed the title [WIP] Work in progress on entity refactoring Entity refactoring: normalized state store with service layer Jan 17, 2026
Copilot AI requested a review from ralph January 17, 2026 12:42
@ralph
Copy link
Owner

ralph commented Jan 17, 2026

ok

@ralph ralph closed this Jan 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants