-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentModel.swift
85 lines (66 loc) · 3.26 KB
/
ContentModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// File.swift
// Guess by emoji
//
// Created by Kurbatov Artem on 06.04.2022.
//
import Foundation
class ContentModel: ObservableObject {
@Published var movies: [Movie]
@Published var results: [Result]
@Published var timeRemaining = 60
init(){
results = []
if let data = UserDefaults.standard.data(forKey: "SavedData") {
if let decoded = try? JSONDecoder().decode([Result].self, from: data) {
results = decoded
}
}
movies = [Movie(id: UUID(), emojis: "🐘🎪", title: "Dumbo"),
Movie(id: UUID(), emojis: "🛥🏴☠️🏝", title: "Pirates of the Caribbean", validAnswer: "Pirates of Caribbean"),
Movie(id: UUID(), emojis: "🇺🇸🍰", title: "American Pie"),
Movie(id: UUID(), emojis: "👩❤️👨🛳🥶", title: "Titanic"),
Movie(id: UUID(), emojis: "👦🏻⚡️🏰", title: "Harry Potter"),
Movie(id: UUID(), emojis: "🕷🙍♂️", title: "Spider-Man", validAnswer: "Spider Man"),
Movie(id: UUID(), emojis: "😱🔪", title: "Scream"),
Movie(id: UUID(), emojis: "🦁👑", title: "The Lion King", validAnswer: "Lion King"),
Movie(id: UUID(), emojis: "🚙↔️🤖", title: "Transformers"),
Movie(id: UUID(), emojis: "🏠🎄👦", title: "Home Alone"),
Movie(id: UUID(), emojis: "♾️✨🧠", title: "Eternal Sunshine of the Spotless Mind", validAnswer: "Eternal Sunshine of Spotless Mind"),
Movie(id: UUID(), emojis: "👑🦍", title: "King Kong"),
Movie(id: UUID(), emojis: "🙍🏻♂️🤲✂️", title: "Edward Scissorhands"),
Movie(id: UUID(), emojis: "🧔♂️⚡️🔨", title: "Thor"),
Movie(id: UUID(), emojis: "🎥🙍🏻♂️📺", title: "The Truman Show", validAnswer: "Truman Show"),
Movie(id: UUID(), emojis: "🥋👶", title: "The Karate Kid", validAnswer: "Karate Kid"),
Movie(id: UUID(), emojis: "😎😎👽", title: "Men in Black"),
Movie(id: UUID(), emojis: "🔍🐠", title: "Finding Nemo", validAnswer: "Finding Dory"),
Movie(id: UUID(), emojis: "🚫👻", title: "Ghostbusters"),
Movie(id: UUID(), emojis: "🌃🏛🦖", title: "Night at the Museum", validAnswer: "Night at Museum")]
}
func save() {
if let encoded = try? JSONEncoder().encode(results) {
UserDefaults.standard.set(encoded, forKey: "SavedData")
}
}
func findMovie(_ id: UUID) -> Int {
for index in 0..<movies.count {
if movies[index].id == id {
return index
}
}
return 0
}
func removeResult(id: UUID) {
for index in 0..<results.count {
if results[index].id == id {
results.remove(at: index)
break
}
}
}
func resetGuessed() {
for index in 0..<movies.count {
movies[index].guessed = false
}
}
}