List Selection and JSON datasource
This commit is contained in:
@@ -22,18 +22,9 @@ import WebKit
|
|||||||
|
|
||||||
@main
|
@main
|
||||||
struct Clemens_PlaygroundApp: App {
|
struct Clemens_PlaygroundApp: App {
|
||||||
|
|
||||||
private var url = "https://fressnapf-dev-gameassets.gamegame-server.de/"
|
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
VStack {
|
GameSelection()
|
||||||
// Use this for >= Xcode 26 and remove commentary in "NewWebView.swift"
|
|
||||||
// NewWebView(url)
|
|
||||||
|
|
||||||
// Use this for < Xcode 26
|
|
||||||
OldWebView(url)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
Clemens Playground/Data/Game.swift
Normal file
14
Clemens Playground/Data/Game.swift
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
//
|
||||||
|
// Game.swift
|
||||||
|
// Clemens Playground
|
||||||
|
//
|
||||||
|
// Created by Maximilian Roider on 14.11.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
struct Game: Hashable, Codable, Identifiable {
|
||||||
|
var id: Int
|
||||||
|
var name: String
|
||||||
|
var url: String
|
||||||
|
}
|
||||||
33
Clemens Playground/Data/Loader.swift
Normal file
33
Clemens Playground/Data/Loader.swift
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
//
|
||||||
|
// Loader.swift
|
||||||
|
// Clemens Playground
|
||||||
|
//
|
||||||
|
// Created by Maximilian Roider on 14.11.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
var games: [Game] = load("games.json")
|
||||||
|
|
||||||
|
func load<T: Decodable>(_ filename: String) -> T {
|
||||||
|
|
||||||
|
let data: Data
|
||||||
|
|
||||||
|
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
|
||||||
|
else {
|
||||||
|
fatalError("Couldn't find \(filename) in main bundle.")
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
data = try Data(contentsOf: file)
|
||||||
|
} catch {
|
||||||
|
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
let decoder = JSONDecoder()
|
||||||
|
return try decoder.decode(T.self, from: data)
|
||||||
|
} catch {
|
||||||
|
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Clemens Playground/Screens/GameSelection.swift
Normal file
25
Clemens Playground/Screens/GameSelection.swift
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// GameSelection.swift
|
||||||
|
// Clemens Playground
|
||||||
|
//
|
||||||
|
// Created by Maximilian Roider on 14.11.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct GameSelection: View {
|
||||||
|
var body: some View {
|
||||||
|
NavigationSplitView {
|
||||||
|
List(games) { game in
|
||||||
|
NavigationLink {
|
||||||
|
GameView(game)
|
||||||
|
} label: {
|
||||||
|
GameRow(game)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.navigationTitle("Games")
|
||||||
|
} detail: {
|
||||||
|
Text("Select a game")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
Clemens Playground/Screens/GameView.swift
Normal file
24
Clemens Playground/Screens/GameView.swift
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
//
|
||||||
|
// GameView.swift
|
||||||
|
// Clemens Playground
|
||||||
|
//
|
||||||
|
// Created by Maximilian Roider on 14.11.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct GameView: View {
|
||||||
|
private let game: Game
|
||||||
|
|
||||||
|
init(_ game: Game) {
|
||||||
|
self.game = game
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
// Use this for >= Xcode 26 and remove commentary in "NewWebView.swift"
|
||||||
|
// NewWebView(game.url)
|
||||||
|
|
||||||
|
// Use this for < Xcode 26
|
||||||
|
OldWebView(game.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Clemens Playground/Views/GameSelection/GameRow.swift
Normal file
25
Clemens Playground/Views/GameSelection/GameRow.swift
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// GameRow.swift
|
||||||
|
// Clemens Playground
|
||||||
|
//
|
||||||
|
// Created by Maximilian Roider on 14.11.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct GameRow: View {
|
||||||
|
|
||||||
|
var game: Game
|
||||||
|
|
||||||
|
init(_ game: Game) {
|
||||||
|
self.game = game
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
HStack {
|
||||||
|
Text(game.name)
|
||||||
|
Spacer()
|
||||||
|
Image(systemName: "chevron.forward")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import SwiftUI
|
|||||||
import WebKit
|
import WebKit
|
||||||
|
|
||||||
struct OldWebView: UIViewRepresentable {
|
struct OldWebView: UIViewRepresentable {
|
||||||
let url: String
|
private let url: String
|
||||||
|
|
||||||
init(_ url: String) {
|
init(_ url: String) {
|
||||||
self.url = url
|
self.url = url
|
||||||
12
Clemens Playground/games.json
Normal file
12
Clemens Playground/games.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 1001,
|
||||||
|
"name": "Fressnapf",
|
||||||
|
"url": "https://fressnapf-dev-gameassets.gamegame-server.de/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1002,
|
||||||
|
"name": "Projekt-Git",
|
||||||
|
"url": "https://git.roider.tech/darksider110/Clemens-Webview-Playground"
|
||||||
|
},
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user