Basic Setup of Classes and First Load/ Store Methods

This commit is contained in:
severin.memmishofer 2023-07-09 11:12:27 +02:00
parent b24bfca630
commit 51e1215ddf
5 changed files with 126 additions and 0 deletions

View File

@ -15,6 +15,10 @@
96454F362A558EBE0040BEBD /* RippleChatUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96454F352A558EBE0040BEBD /* RippleChatUITests.swift */; };
96454F382A558EBE0040BEBD /* RippleChatUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96454F372A558EBE0040BEBD /* RippleChatUITestsLaunchTests.swift */; };
96454F452A5593900040BEBD /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = 96454F442A5593900040BEBD /* .gitignore */; };
F5847B622A599BF4009E28D4 /* Body.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5847B612A599BF4009E28D4 /* Body.swift */; };
F5847B642A599CC3009E28D4 /* LogEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5847B632A599CC3009E28D4 /* LogEntry.swift */; };
F5847B662A599EA4009E28D4 /* Feed.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5847B652A599EA4009E28D4 /* Feed.swift */; };
F5847B6A2A59AB24009E28D4 /* FeedStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5847B692A59AB24009E28D4 /* FeedStore.swift */; };
F58EB2D02A5590E800E22DA6 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = F58EB2CF2A5590E800E22DA6 /* README.md */; };
/* End PBXBuildFile section */
@ -47,6 +51,10 @@
96454F352A558EBE0040BEBD /* RippleChatUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RippleChatUITests.swift; sourceTree = "<group>"; };
96454F372A558EBE0040BEBD /* RippleChatUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RippleChatUITestsLaunchTests.swift; sourceTree = "<group>"; };
96454F442A5593900040BEBD /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
F5847B612A599BF4009E28D4 /* Body.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Body.swift; sourceTree = "<group>"; };
F5847B632A599CC3009E28D4 /* LogEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogEntry.swift; sourceTree = "<group>"; };
F5847B652A599EA4009E28D4 /* Feed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Feed.swift; sourceTree = "<group>"; };
F5847B692A59AB24009E28D4 /* FeedStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedStore.swift; sourceTree = "<group>"; };
F58EB2CF2A5590E800E22DA6 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -104,6 +112,10 @@
96454F1C2A558EBC0040BEBD /* ContentView.swift */,
96454F1E2A558EBD0040BEBD /* Assets.xcassets */,
96454F202A558EBD0040BEBD /* Preview Content */,
F5847B612A599BF4009E28D4 /* Body.swift */,
F5847B632A599CC3009E28D4 /* LogEntry.swift */,
F5847B652A599EA4009E28D4 /* Feed.swift */,
F5847B692A59AB24009E28D4 /* FeedStore.swift */,
);
path = RippleChat;
sourceTree = "<group>";
@ -265,6 +277,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F5847B622A599BF4009E28D4 /* Body.swift in Sources */,
F5847B662A599EA4009E28D4 /* Feed.swift in Sources */,
F5847B642A599CC3009E28D4 /* LogEntry.swift in Sources */,
F5847B6A2A59AB24009E28D4 /* FeedStore.swift in Sources */,
96454F1D2A558EBC0040BEBD /* ContentView.swift in Sources */,
96454F1B2A558EBC0040BEBD /* RippleChatApp.swift in Sources */,
);

19
RippleChat/Body.swift Normal file
View File

@ -0,0 +1,19 @@
//
// Body.swift
// RippleChat
//
// Created by Severin Memmishofer on 08.07.23.
//
import Foundation
struct Body: Codable {
let tag: String
let value: String
init(tag: String, value: String) {
self.tag = tag
self.value = value
}
}

25
RippleChat/Feed.swift Normal file
View File

@ -0,0 +1,25 @@
//
// Feed.swift
// RippleChat
//
// Created by Severin Memmishofer on 08.07.23.
//
import Foundation
struct Feed: Codable {
let feed: [LogEntry]
}
extension Feed {
static let sampleData: [LogEntry] =
[
LogEntry(feedid: "BOB", sequenceNumber: 1, body: Body(tag: "nam", value: "Bob")),
LogEntry(feedid: "BOB", sequenceNumber: 2, body: Body(tag: "txt", value: "My first post!")),
LogEntry(feedid: "BOB", sequenceNumber: 3, body: Body(tag: "txt", value: "Welcome Alice"))
]
}

View File

@ -0,0 +1,44 @@
//
// FeedStore.swift
// RippleChat
//
// Created by Severin Memmishofer on 08.07.23.
//
import SwiftUI
@MainActor
class FeedStore: ObservableObject {
@Published var feed: [LogEntry] = []
private static func fileURL() throws -> URL {
try FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
.appendingPathComponent("feed.data")
}
func load() async throws {
let task = Task<[LogEntry], Error> {
let fileURL = try Self.fileURL()
guard let data = try? Data(contentsOf: fileURL) else {
return []
}
let logEntries = try JSONDecoder().decode([LogEntry].self, from: data)
return logEntries
}
let feed = try await task.value
self.feed = feed
}
func save(feed: [LogEntry]) async throws {
let task = Task {
let data = try JSONEncoder().encode(feed)
let outfile = try Self.fileURL()
try data.write(to: outfile)
}
_ = try await task.value
}
}

22
RippleChat/LogEntry.swift Normal file
View File

@ -0,0 +1,22 @@
//
// LogEntry.swift
// RippleChat
//
// Created by Severin Memmishofer on 08.07.23.
//
import Foundation
struct LogEntry: Codable {
let feedid: String
let sequenceNumber: Int
let body: Body
init(feedid: String, sequenceNumber: Int, body: Body) {
self.feedid = feedid
self.sequenceNumber = sequenceNumber
self.body = body
}
}