import SwiftUI import WidgetKit #if os(macOS) import AppKit #endif @main struct TabelloneApp: App { var body: some Scene { #if os(macOS) WindowGroup("Tabellone") { ContentView() } .windowResizability(.contentSize) #else WindowGroup { ContentView() } #endif } } struct ContentView: View { @State private var snapshot = Snapshot.current() @State private var tokenInput = "" @State private var busy = false @State private var status = "" @State private var statusIsError = false @State private var previewMilestone = false private let store = FollowerStore.shared /// L'anteprima, con il traguardo finto quando si vuole vedere com'รจ. private var shown: Snapshot { var value = snapshot if previewMilestone, value.milestone == nil { let step = FollowerStore.milestoneStep value.milestone = max(step, (value.count ?? 0) / step * step + step) value.count = value.milestone } return value } var body: some View { #if os(macOS) content .padding(28) .frame(width: 460) #else ScrollView { content .padding(20) .frame(maxWidth: 460) .frame(maxWidth: .infinity) } #endif } private var content: some View { VStack(alignment: .leading, spacing: 22) { MediumStazione(snapshot: shown) .padding(16) .frame(width: 364, height: 170) .background(Stazione.background, in: RoundedRectangle(cornerRadius: 22, style: .continuous)) .frame(maxWidth: .infinity) if snapshot.connected { WeekList(values: snapshot.week) .padding(16) .frame(width: 364) .background(Stazione.background, in: RoundedRectangle(cornerRadius: 22, style: .continuous)) .frame(maxWidth: .infinity) } if !status.isEmpty { Text(status) .foregroundStyle(statusIsError ? Color.red : Color.secondary) .fixedSize(horizontal: false, vertical: true) } if snapshot.connected { HStack { Button("Aggiorna ora") { Task { await refresh() } } .keyboardShortcut("r") Button(previewMilestone ? "Torna al numero vero" : "Anteprima traguardo") { withAnimation(.smooth(duration: 0.8)) { previewMilestone.toggle() } } Spacer() } .disabled(busy) Text(helpText) .font(.callout) .foregroundStyle(.secondary) .fixedSize(horizontal: false, vertical: true) HStack { #if os(macOS) Button("Copia il token per l'iPhone") { copyToken() } #endif Spacer() Button("Scollega Instagram", role: .destructive) { disconnect() } } .disabled(busy) } else { connectForm } } .task { await refresh() } } private var helpText: String { #if os(macOS) "Per aggiungere il widget: clic destro sulla scrivania, \"Modifica widget\", cerca \"Tabellone\". L'app puoi chiuderla, il widget si aggiorna da solo. Ogni 500 follower il tabellone diventa giallo per un giorno." #else "Per aggiungere il widget: tieni premuto sulla schermata Home, tocca \"Modifica\" e poi \"Aggiungi widget\", cerca \"Tabellone\". Ogni 500 follower il tabellone diventa giallo per un giorno." #endif } private var connectForm: some View { VStack(alignment: .leading, spacing: 12) { Text("Collega Instagram").font(.headline) Text(connectHelp) .foregroundStyle(.secondary) .fixedSize(horizontal: false, vertical: true) SecureField("Token di accesso", text: $tokenInput) .textFieldStyle(.roundedBorder) .onSubmit { Task { await connect() } } Button("Collega") { Task { await connect() } } .keyboardShortcut(.defaultAction) .disabled(busy || tokenInput.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) } } private var connectHelp: String { #if os(macOS) "Incolla il token di accesso creato nella tua app su developers.facebook.com (Instagram > Configurazione API > Genera token). Dura 60 giorni e Tabellone lo rinnova da solo." #else "Sul Mac apri Tabellone e premi \"Copia il token per l'iPhone\". Poi qui tieni premuto nel campo e scegli Incolla." #endif } private func connect() async { let token = tokenInput.trimmingCharacters(in: .whitespacesAndNewlines) guard !token.isEmpty else { return } busy = true defer { busy = false } do { let profile = try await Instagram.profile(token: token) store.token = token store.tokenDate = Date() await Updater.save(profile) tokenInput = "" snapshot = Snapshot.current() show("Collegato come @\(profile.username).", error: false) WidgetCenter.shared.reloadAllTimelines() } catch { show("Non riesco a collegarmi: \(error.localizedDescription)", error: true) } } private func refresh() async { guard store.token != nil else { snapshot = Snapshot.current(); return } busy = true defer { busy = false } snapshot = await Updater.update() if let problem = store.lastError { show("Ultimo aggiornamento non riuscito: \(problem)", error: true) } else if let date = store.lastUpdate { show("Aggiornato alle \(date.formatted(date: .omitted, time: .shortened)).", error: false) } WidgetCenter.shared.reloadAllTimelines() } #if os(macOS) private func copyToken() { guard let token = store.token else { return } NSPasteboard.general.clearContents() NSPasteboard.general.setString(token, forType: .string) show("Token copiato. Sull'iPhone apri Tabellone e incollalo entro un paio di minuti.", error: false) } #endif private func disconnect() { store.disconnect() snapshot = Snapshot.current() previewMilestone = false show("Instagram scollegato.", error: false) WidgetCenter.shared.reloadAllTimelines() } private func show(_ text: String, error: Bool) { status = text statusIsError = error } }