Add announcements to home view (#195)

* Add announcement system

* Fetch announcements from GitHub

* Add visibility and timestamps to announcements
This commit is contained in:
Stephen B 2025-06-04 09:54:16 -04:00 • committed by GitHub
parent 67abc8c2d9
commit 2d10756ca4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 128 additions and 4 deletions

View file

@ -0,0 +1,18 @@
[
{
"id": 1,
"title": "Welcome!",
"body": "Thank you for using StikDebug. Stay tuned for updates.",
"date": "2025-06-04",
"time": "12:00",
"visible": true
},
{
"id": 2,
"title": "New Feature",
"body": "Announcements are now shown on the home screen.",
"date": "2025-06-04",
"time": "12:30",
"visible": true
}
]

View file

@ -0,0 +1,38 @@
import Foundation
struct Announcement: Identifiable, Codable {
let id: Int
let title: String
let body: String
let date: String
let time: String
let visible: Bool
}
class AnnouncementManager {
private static let announcementsURL = "https://raw.githubusercontent.com/0-Blu/StikJIT/main/StikJIT/Resources/announcements.json"
static func fetchAnnouncements(completion: @escaping ([Announcement]) -> Void) {
guard let url = URL(string: announcementsURL) else {
completion([])
return
}
URLSession.shared.dataTask(with: url) { data, response, _ in
guard let data = data,
(response as? HTTPURLResponse)?.statusCode == 200,
let announcements = try? JSONDecoder().decode([Announcement].self, from: data) else {
DispatchQueue.main.async {
completion([])
}
return
}
let visibleAnnouncements = announcements.filter { $0.visible }
DispatchQueue.main.async {
completion(visibleAnnouncements)
}
}.resume()
}
}

View file

@ -0,0 +1,50 @@
import SwiftUI
struct AnnouncementCard: View {
var announcement: Announcement
@AppStorage("customAccentColor") private var customAccentColorHex: String = ""
private var accentColor: Color {
if customAccentColorHex.isEmpty {
return .blue
} else {
return Color(hex: customAccentColorHex) ?? .blue
}
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(announcement.title)
.font(.headline)
.foregroundColor(.primary)
Text(announcement.body)
.font(.subheadline)
.foregroundColor(.secondary)
Text("\(announcement.date) \(announcement.time)")
.font(.caption)
.foregroundColor(.secondary)
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color(UIColor.secondarySystemBackground))
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(accentColor, lineWidth: 2)
)
.cornerRadius(16)
.shadow(color: Color.black.opacity(0.08), radius: 3, x: 0, y: 2)
}
}
#Preview {
AnnouncementCard(
announcement: Announcement(
id: 0,
title: "Title",
body: "Body",
date: "2025-01-01",
time: "00:00",
visible: true
)
)
}

View file

@ -8,6 +8,9 @@
import SwiftUI
import UniformTypeIdentifiers
// Load announcements from remote JSON
import Foundation
extension UIDocumentPickerViewController {
@objc func fix_init(forOpeningContentTypes contentTypes: [UTType], asCopy: Bool) -> UIDocumentPickerViewController {
return fix_init(forOpeningContentTypes: contentTypes, asCopy: true)
@ -32,6 +35,8 @@ struct HomeView: View {
@State private var isImportingFile = false
@State private var showingConsoleLogsView = false
@State private var importProgress: Float = 0.0
@State private var announcements: [Announcement] = []
@State private var pidTextAlertShow = false
@State private var pidStr = ""
@ -60,14 +65,23 @@ struct HomeView: View {
Text("Welcome to StikDebug \(username)!")
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.bold)
Text(pairingFileExists ? "Click connect to get started" : "Pick pairing file to get started")
.font(.system(.subheadline, design: .rounded))
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.padding(.top, 40)
if !announcements.isEmpty {
VStack(spacing: 12) {
ForEach(announcements) { announcement in
AnnouncementCard(announcement: announcement)
}
}
.padding(.horizontal, 20)
}
Button(action: {
@ -203,10 +217,14 @@ struct HomeView: View {
checkPairingFileExists()
// Don't initialize specific color value when empty - empty means "use system theme"
// This was causing the toggle to turn off when returning to settings
// Initialize background color
refreshBackground()
AnnouncementManager.fetchAnnouncements { loaded in
announcements = loaded
}
// Add notification observer for showing pairing file picker
NotificationCenter.default.addObserver(
forName: NSNotification.Name("ShowPairingFilePicker"),