Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spot the Seal v2.0 #74

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions SwiftIslandApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
2E19CADE2A7EEC9700177B61 /* UpNextSystemLargeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E19CADD2A7EEC9700177B61 /* UpNextSystemLargeView.swift */; };
2E19CAE02A7EF7B100177B61 /* UpNextSystemRectangularView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E19CADF2A7EF7B100177B61 /* UpNextSystemRectangularView.swift */; };
2E19CAE22A7EFD9800177B61 /* UpNextSystemCircularView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E19CAE12A7EFD9800177B61 /* UpNextSystemCircularView.swift */; };
B30E2E172C6E5E71007A48E5 /* Hint.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30E2E162C6E5E71007A48E5 /* Hint.swift */; };
B332ADF72A9A813C00557AA7 /* truchet-tiles.pdf in Resources */ = {isa = PBXBuildFile; fileRef = B332ADF12A9A813900557AA7 /* truchet-tiles.pdf */; };
B332ADF82A9A813C00557AA7 /* double-names.pdf in Resources */ = {isa = PBXBuildFile; fileRef = B332ADF22A9A813B00557AA7 /* double-names.pdf */; };
B332ADF92A9A813C00557AA7 /* valuta.pdf in Resources */ = {isa = PBXBuildFile; fileRef = B332ADF32A9A813B00557AA7 /* valuta.pdf */; };
Expand All @@ -99,7 +100,6 @@
B35057FB2A9DD24400B71436 /* mystery-museum.pdf in Resources */ = {isa = PBXBuildFile; fileRef = B35057FA2A9DD24400B71436 /* mystery-museum.pdf */; };
B354DDE52A9BEEE700C24461 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = B354DDE42A9BEEE700C24461 /* Utils.swift */; };
B362F5E12AA1044F00AFE1EE /* ConferenceBoxSponsors.swift in Sources */ = {isa = PBXBuildFile; fileRef = B362F5E02AA1044F00AFE1EE /* ConferenceBoxSponsors.swift */; };
B3733D772A9951B10069D490 /* PuzzleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3733D732A9951B10069D490 /* PuzzleView.swift */; };
B3733D792A9951B10069D490 /* PuzzleItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3733D752A9951B10069D490 /* PuzzleItemView.swift */; };
B3733D7A2A9951B10069D490 /* PuzzlePageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3733D762A9951B10069D490 /* PuzzlePageView.swift */; };
B3C626422A942291002754A8 /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3C626412A942291002754A8 /* WebView.swift */; };
Expand Down Expand Up @@ -229,6 +229,7 @@
2E19CADD2A7EEC9700177B61 /* UpNextSystemLargeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpNextSystemLargeView.swift; sourceTree = "<group>"; };
2E19CADF2A7EF7B100177B61 /* UpNextSystemRectangularView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpNextSystemRectangularView.swift; sourceTree = "<group>"; };
2E19CAE12A7EFD9800177B61 /* UpNextSystemCircularView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpNextSystemCircularView.swift; sourceTree = "<group>"; };
B30E2E162C6E5E71007A48E5 /* Hint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Hint.swift; sourceTree = "<group>"; };
B332ADF12A9A813900557AA7 /* truchet-tiles.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "truchet-tiles.pdf"; sourceTree = "<group>"; };
B332ADF22A9A813B00557AA7 /* double-names.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "double-names.pdf"; sourceTree = "<group>"; };
B332ADF32A9A813B00557AA7 /* valuta.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = valuta.pdf; sourceTree = "<group>"; };
Expand All @@ -243,7 +244,6 @@
B35057FA2A9DD24400B71436 /* mystery-museum.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "mystery-museum.pdf"; sourceTree = "<group>"; };
B354DDE42A9BEEE700C24461 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
B362F5E02AA1044F00AFE1EE /* ConferenceBoxSponsors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConferenceBoxSponsors.swift; sourceTree = "<group>"; };
B3733D732A9951B10069D490 /* PuzzleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PuzzleView.swift; sourceTree = "<group>"; };
B3733D752A9951B10069D490 /* PuzzleItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PuzzleItemView.swift; sourceTree = "<group>"; };
B3733D762A9951B10069D490 /* PuzzlePageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PuzzlePageView.swift; sourceTree = "<group>"; };
B3C626412A942291002754A8 /* WebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -688,8 +688,8 @@
children = (
B3733D762A9951B10069D490 /* PuzzlePageView.swift */,
B3733D752A9951B10069D490 /* PuzzleItemView.swift */,
B3733D732A9951B10069D490 /* PuzzleView.swift */,
B354DDE42A9BEEE700C24461 /* Utils.swift */,
B30E2E162C6E5E71007A48E5 /* Hint.swift */,
);
path = "Spot the Seal";
sourceTree = "<group>";
Expand Down Expand Up @@ -916,7 +916,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B3733D772A9951B10069D490 /* PuzzleView.swift in Sources */,
2A7F12982A6B19E0007C28EA /* SwiftIslandLogo.swift in Sources */,
2AEDC4C32A4A089100CD0180 /* ConferencePageView.swift in Sources */,
B3733D792A9951B10069D490 /* PuzzleItemView.swift in Sources */,
Expand All @@ -937,6 +936,7 @@
2AEDC4C62A4A08AF00CD0180 /* TabBarView.swift in Sources */,
2AEDC4972A4A030F00CD0180 /* MainApp.swift in Sources */,
2AE43FFF2A702999009078E3 /* ConferenceBoxEvent.swift in Sources */,
B30E2E172C6E5E71007A48E5 /* Hint.swift in Sources */,
2A7F12832A648C59007C28EA /* SwiftIslandButtonStyle.swift in Sources */,
2A7F12902A69D9C7007C28EA /* PracticalGenericPageView.swift in Sources */,
2AE440012A702BC0009078E3 /* Date+Formatting.swift in Sources */,
Expand Down Expand Up @@ -1011,7 +1011,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = V38QRBT6X5;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SwiftIslandWidgets/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "Swift Island Widgets";
Expand Down Expand Up @@ -1041,7 +1041,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = V38QRBT6X5;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = SwiftIslandWidgets/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "Swift Island Widgets";
Expand Down
60 changes: 45 additions & 15 deletions SwiftIslandApp/MainApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ private extension MainApp {
func handleOpenURL(_ url: URL) {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true), components.host == "swiftisland.nl" else { return }

let actions = components.queryItems?.compactMap { URLTask(rawValue: $0) }
actions?.forEach { handleUrlTask($0) }
if let items = components.queryItems {
guard let task = URLTask(items: items) else { return }
handleUrlTask(task)
}
}

func handleUrlTask(_ urlTask: URLTask) {
Expand All @@ -88,14 +90,30 @@ private extension MainApp {
print(error)
}
}
case .seal(let slug):
case .seal(let slug, let key):
print("Seal: \(urlTask)")
if slug == "reset" {
Defaults.reset(.puzzleStatus)
Defaults.reset(.puzzleHints)
currentPuzzleSlug = nil
} else {
let currentStatus = Defaults[.puzzleStatus][slug]
if currentStatus == nil || currentStatus == .notFound {
Defaults[.puzzleStatus][slug] = .found
findSlug(slug: slug, key: key)
}
}
}

func findSlug(slug: String, key: String) {
if slug == "reset" {
Defaults.reset(.puzzleStatus)
Defaults.reset(.puzzleHints)
} else {
let currentStatus = Defaults[.puzzleStatus][slug]
if currentStatus == nil || currentStatus == .notFound {
Defaults[.puzzleStatus][slug] = .found
}
if let puzzle = appDataModel.puzzles.first(where: { $0.slug == slug }) {
if let hint = try? decrypt(value: puzzle.encryptedHint, solution: key, type: Hint.self) {
Defaults[.puzzleHints][puzzle.slug] = hint
}
}
currentPuzzleSlug = slug
Expand Down Expand Up @@ -131,19 +149,31 @@ private extension MainApp {
enum URLTask {
case action(appAction: AppActions)
case ticket(slug: String)
case seal(slug: String)
case seal(slug: String, key: String)

init?(rawValue: URLQueryItem) {
switch rawValue.name {
init?(items: [URLQueryItem]) {
for item in items {
if let task = URLTask.parseItem(item: item, allItems: items) {
self = task
return
}
}
return nil
}

static func parseItem(item: URLQueryItem, allItems: [URLQueryItem]) -> URLTask? {
switch item.name {
case "action":
guard let value = rawValue.value, let actionTriggered = AppActions(rawValue: value) else { return nil }
self = .action(appAction: actionTriggered)
guard let value = item.value, let actionTriggered = AppActions(rawValue: value) else { return nil }
return .action(appAction: actionTriggered)
case "ticket":
guard let value = rawValue.value else { return nil }
self = .ticket(slug: value)
guard let value = item.value else { return nil }
return .ticket(slug: value)
case "seal":
guard let value = rawValue.value else { return nil }
self = .seal(slug: value)
guard let value = item.value, let key
= allItems.first(where: {$0.name == "key"})?.value else { return nil }

return .seal(slug: value, key: key)
default:
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions SwiftIslandApp/Pages/Practical/Spot the Seal/Hint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by Niels van Hoorn for the use in the Swift Island app
// Copyright © 2024 AppTrix AB. All rights reserved.
//

import Foundation
import Defaults

struct Hint: Decodable, Encodable, Defaults.Serializable {
let number: Int
let text: String
var used: Bool = false
var description: String {
return "\(number). \(text)"
}
mutating func toggleUsed() {
used = !used
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PuzzleItemViewModel: ObservableObject {
@Published var scale: Double = 1
@Published var flipAngle: CGFloat = 0
@Published var isCurrent = false

init(puzzle: Puzzle, isCurrent: Bool = false) {
self.puzzle = puzzle
self.isCurrent = isCurrent
Expand All @@ -29,14 +29,14 @@ class PuzzleItemViewModel: ObservableObject {

struct PuzzleItemView: View {
@StateObject var viewModel: PuzzleItemViewModel

@Environment(\.colorScheme)
private var colorScheme

init(puzzle: Puzzle, isCurrent: Bool = false) {
_viewModel = StateObject(wrappedValue: PuzzleItemViewModel(puzzle: puzzle, isCurrent: isCurrent))
}

var body: some View {
let color: Color = colorScheme == .light ? .black : .white
let puzzle = viewModel.puzzle
Expand Down
69 changes: 38 additions & 31 deletions SwiftIslandApp/Pages/Practical/Spot the Seal/PuzzlePageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftIslandDataLogic

extension Defaults.Keys {
static let puzzleStatus = Key<[String: PuzzleState]>("puzzleStatus", default: [:])
static let puzzleHints = Key<[String: String]>("puzzleHints", default: [:])
static let puzzleHints = Key<[String: Hint]>("puzzleHints", default: [:])
}

enum PuzzleState: String, Defaults.Serializable {
Expand All @@ -18,10 +18,10 @@ enum PuzzleState: String, Defaults.Serializable {
case nearby = "Nearby"
case activated = "Activated"
case solved = "Solved"

var next: PuzzleState {
switch self {
// Not all states are currently used, they were part of an more elaborate flow we had in mind initially. Currently we only use .NotFound, .Found and .Solved
// Not all states are currently used, they were part of an more elaborate flow we had in mind initially. Currently we only use .NotFound, .Found and .Solved
case .notFound: return .found
case .found: return .nearby
case .nearby: return .activated
Expand Down Expand Up @@ -59,54 +59,61 @@ struct PuzzleGrid: View {
var body: some View {
LazyVGrid(columns: columns, spacing: spacing) {
ForEach(appDataModel.puzzles) { puzzle in
NavigationLink(value: puzzle) {
PuzzleItemView(puzzle: puzzle, isCurrent: (puzzle.slug == currentPuzzleSlug))
}
.disabled(puzzle.state == .notFound)
PuzzleItemView(puzzle: puzzle, isCurrent: (puzzle.slug == currentPuzzleSlug))
}
}
.padding(20)
.navigationTitle("Spot the Seal")
}
}

struct PuzzlePageView: View {
@EnvironmentObject private var appDataModel: AppDataModel

@Default(.puzzleStatus)
var puzzleStatus
@Default(.puzzleHints)
var puzzleHints

@State var currentPuzzleSlug: String?
var body: some View {
VStack {
if appDataModel.puzzles.isEmpty {
Text("Loading...")
} else {
PuzzleGrid(currentPuzzleSlug: currentPuzzleSlug)
Text("Hints").font(.title)
VStack(alignment: .leading) {
Text("Put your phone against any seal you discover ").strikethrough(!puzzleStatus.isEmpty)
ForEach(puzzleHints.keys.sorted(), id: \.self) { slug in
if let hint = puzzleHints[slug] {
let status = puzzleStatus[slug]
let usedHint = status != nil && status != .notFound
Text(hint).strikethrough(usedHint)
ScrollView {
VStack(alignment: .leading) {
if appDataModel.puzzles.isEmpty {
Text("Loading...")
} else {
Text("Search for seals all across the conference grounds to find the 16 clues to solve this puzzle!")
Spacer()
Text("Puzzle").font(.title)
Text("Ten attendees of Swift Island met another person while on their way to the conference. Together they planned a trip on one of the conference days.\n\nWho met who and where did they meet?\nWhat trip did they plan together on which day?")
Spacer()
Text("Clues").font(.title)
VStack(alignment: .leading) {
ForEach(puzzleHints.keys.sorted(by: { slug1, slug2 in
guard let hint1 = puzzleHints[slug1], let hint2 = puzzleHints[slug2] else {
return false
}
return hint1.number < hint2.number
}), id: \.self) { slug in
if let hint = puzzleHints[slug] {
Text(hint.description).strikethrough(hint.used).onTapGesture {
puzzleHints[slug]?.toggleUsed()
}
Spacer()
}
}
}
Spacer()
PuzzleGrid(currentPuzzleSlug: currentPuzzleSlug)
}
}
}
.task {
await appDataModel.fetchPuzzles()
}
.navigationDestination(for: Puzzle.self) { puzzle in
PuzzleView(puzzle: puzzle)
}
.padding(20)
.task {
await appDataModel.fetchPuzzles()
}
}.navigationTitle("Spot the Seal")
}
}


struct PuzzlePageView_Previews: PreviewProvider {
static var previews: some View {
let appDataModel = AppDataModel()
Expand Down
Loading
Loading