Skip to content

Commit

Permalink
Merge pull request #1 from Snesnopic/coreData
Browse files Browse the repository at this point in the history
Core data migration
  • Loading branch information
Snesnopic authored May 16, 2024
2 parents f7d7327 + 770c66a commit 5be2e50
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 147 deletions.
28 changes: 24 additions & 4 deletions Morser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
9D0CB0572BF5E78500FD8E57 /* Sentence.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 9D0CB0552BF5E78500FD8E57 /* Sentence.xcdatamodeld */; };
9D0CB0592BF5E87F00FD8E57 /* DataController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D0CB0582BF5E87F00FD8E57 /* DataController.swift */; };
9D1CB9B02BCEB5930088F90C /* SpeechRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D1CB9AF2BCEB5930088F90C /* SpeechRecognizer.swift */; };
9D2099812BCD37AA00229594 /* Sentence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D2099802BCD37AA00229594 /* Sentence.swift */; };
9D39222F2BCFB86E00FA1D05 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 9D39222E2BCFB86E00FA1D05 /* Localizable.xcstrings */; };
9D3DED2B2BCD109400DAD675 /* MorserApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D3DED2A2BCD109400DAD675 /* MorserApp.swift */; };
9D3DED2D2BCD109400DAD675 /* ParentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D3DED2C2BCD109400DAD675 /* ParentView.swift */; };
Expand All @@ -27,9 +28,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
9D0CB0562BF5E78500FD8E57 /* Sentence.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Sentence.xcdatamodel; sourceTree = "<group>"; };
9D0CB0582BF5E87F00FD8E57 /* DataController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataController.swift; sourceTree = "<group>"; };
9D1CB9AE2BCEB2810088F90C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
9D1CB9AF2BCEB5930088F90C /* SpeechRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeechRecognizer.swift; sourceTree = "<group>"; };
9D2099802BCD37AA00229594 /* Sentence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sentence.swift; sourceTree = "<group>"; };
9D39222E2BCFB86E00FA1D05 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
9D3DED272BCD109400DAD675 /* Morser.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Morser.app; sourceTree = BUILT_PRODUCTS_DIR; };
9D3DED2A2BCD109400DAD675 /* MorserApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MorserApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -67,7 +69,6 @@
9DD425092BE3C5BF009EE623 /* BeepPlayer.swift */,
9D3DED412BCD248100DAD675 /* MorseEncoder.swift */,
9D1CB9AF2BCEB5930088F90C /* SpeechRecognizer.swift */,
9D2099802BCD37AA00229594 /* Sentence.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -130,6 +131,8 @@
9D3DED3B2BCD16DE00DAD675 /* QuickTranslateView.swift */,
9D3DED3D2BCD16EB00DAD675 /* ListenView.swift */,
9D9BC1572BECE24200CD2C5B /* SettingsView.swift */,
9D0CB0552BF5E78500FD8E57 /* Sentence.xcdatamodeld */,
9D0CB0582BF5E87F00FD8E57 /* DataController.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -215,11 +218,12 @@
9DD4250A2BE3C5BF009EE623 /* BeepPlayer.swift in Sources */,
9D3DED2D2BCD109400DAD675 /* ParentView.swift in Sources */,
9D1CB9B02BCEB5930088F90C /* SpeechRecognizer.swift in Sources */,
9D2099812BCD37AA00229594 /* Sentence.swift in Sources */,
9D3DED3C2BCD16DE00DAD675 /* QuickTranslateView.swift in Sources */,
9D3DED402BCD23E700DAD675 /* VibrationEngine.swift in Sources */,
9DA696702BCD7154002B9CBB /* Extensions.swift in Sources */,
9D0CB0592BF5E87F00FD8E57 /* DataController.swift in Sources */,
9D3DED3E2BCD16EB00DAD675 /* ListenView.swift in Sources */,
9D0CB0572BF5E78500FD8E57 /* Sentence.xcdatamodeld in Sources */,
9D3DED2B2BCD109400DAD675 /* MorserApp.swift in Sources */,
9D3DED3A2BCD167600DAD675 /* EncodeView.swift in Sources */,
9D3DED422BCD248100DAD675 /* MorseEncoder.swift in Sources */,
Expand Down Expand Up @@ -380,6 +384,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MARKETING_VERSION = 1.2;
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = com.snesnopic.Morser;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
Expand Down Expand Up @@ -423,6 +428,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MARKETING_VERSION = 1.2;
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = com.snesnopic.Morser;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
Expand Down Expand Up @@ -456,6 +462,20 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCVersionGroup section */
9D0CB0552BF5E78500FD8E57 /* Sentence.xcdatamodeld */ = {
isa = XCVersionGroup;
children = (
9D0CB0562BF5E78500FD8E57 /* Sentence.xcdatamodel */,
);
currentVersion = 9D0CB0562BF5E78500FD8E57 /* Sentence.xcdatamodel */;
name = Sentence.xcdatamodeld;
path = /Users/giuseppefrancione/Documents/Morser/Morser/Views/Sentence.xcdatamodeld;
sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
/* End XCVersionGroup section */
};
rootObject = 9D3DED1F2BCD109400DAD675 /* Project object */;
}
14 changes: 14 additions & 0 deletions Morser/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ extension String {
func charAt(_ i: Int) -> Character {
return Array(self)[i]
}

func morseCode() -> String {
return MorseEncoder.encode(string: self)
}
}

extension FetchedResults<Sentence> {
func toArray() -> [Sentence] {
var array:[Sentence] = []
self.forEach { result in
array.append(result)
}
return array
}
}
2 changes: 1 addition & 1 deletion Morser/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1165,4 +1165,4 @@
}
},
"version" : "1.0"
}
}
32 changes: 0 additions & 32 deletions Morser/Model/Sentence.swift

This file was deleted.

8 changes: 8 additions & 0 deletions Morser/Model/VibrationEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class VibrationEngine: ObservableObject {
triggerNextVibration()
}

func readMorseCode(sentence: Sentence) {
morseCodeIndex = 0
morseCodeString = MorseEncoder.encode(string: sentence.sentence!)
if VibrationEngine.soundEnabled && dotPlayer == nil && dashPlayer == nil {
dotPlayer = BeepPlayer(frequency: 600, duration: dotDuration)
dashPlayer = BeepPlayer(frequency: 600, duration: dashDuration)
}
triggerNextVibration() }
// Function to trigger vibrations based on Morse code
func triggerNextVibration() {
guard morseCodeIndex < morseCodeString.count else {
Expand Down
21 changes: 21 additions & 0 deletions Morser/Views/DataController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DataController.swift
// Morser
//
// Created by Giuseppe Francione on 16/05/24.
//

import Foundation
import CoreData
import SwiftUI

class DataController: ObservableObject {
let container = NSPersistentContainer(name: "Sentence")
init() {
container.loadPersistentStores { description, error in
if let error = error {
print("Core Data failed to load: \(error.localizedDescription)")
}
}
}
}
4 changes: 2 additions & 2 deletions Morser/Views/MorserApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//

import SwiftUI
import SwiftData

@main
struct MorserApp: App {
@StateObject private var dataController = DataController()
var body: some Scene {
WindowGroup {
ParentView()
.environment(\.managedObjectContext, dataController.container.viewContext)
}
.modelContainer(for: Sentence.self)
}
}
50 changes: 29 additions & 21 deletions Morser/Views/ParentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//

import SwiftUI
import SwiftData
import CoreData

struct ParentView: View {
@Query private var sentences: [Sentence]
@Environment(\.modelContext) private var modelContext: ModelContext
@FetchRequest(sortDescriptors: [SortDescriptor(\.order, order: .forward)]) private var sentences: FetchedResults<Sentence>
@Environment(\.managedObjectContext) var mocModelContext
var body: some View {
TabView {
EncodeView().tabItem{
Expand All @@ -37,29 +37,37 @@ struct ParentView: View {
}
}
.onAppear {
if sentences.isEmpty {
[
Sentence(sentence: String(localized: "Yes, I can guide you."), order: 0),
Sentence(sentence: String(localized: "I'm here to help."), order: 1),
Sentence(sentence: String(localized: "I understand, let me assist you."), order: 2),
Sentence(sentence: String(localized: "I'll write it down for you."), order: 3),
Sentence(sentence: String(localized: "I'll describe the menu options for you."), order: 4),
Sentence(sentence: String(localized: "I'll help you navigate through touch."), order: 5),
Sentence(sentence: String(localized: "I'm communicating with you through touch."), order: 6),
Sentence(sentence: String(localized: "I'll lead you to the bus stop."), order: 7),
Sentence(sentence: String(localized: "Let me describe the location to you."), order: 8),
Sentence(sentence: String(localized: "I'll tap your hand to get your attention."), order: 9)
].forEach { sentence in
modelContext.insert(sentence)
if sentences.toArray().isEmpty {
let strings = [
String(localized: "I'm here to help."),
String(localized: "Yes, I can guide you."),
String(localized: "I understand, let me assist you."),
String(localized: "I'll write it down for you."),
String(localized: "I'll describe the menu options for you."),
String(localized: "I'll help you navigate through touch."),
String(localized: "I'm communicating with you through touch."),
String(localized: "I'll lead you to the bus stop."),
String(localized: "Let me describe the location to you."),
String(localized: "I'll tap your hand to get your attention.")
]
var array:[Sentence] = []
var index:Int32 = 0
strings.forEach { string in
let sentence = Sentence(entity: Sentence.entity(), insertInto: mocModelContext)
sentence.sentence = string
sentence.order = index
index += 1
array.append(sentence)
}
}
}
}
}

#Preview {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try! ModelContainer(for: Sentence.self, configurations: config)

return ParentView().modelContainer(container)
// let config = ModelConfiguration(isStoredInMemoryOnly: true)
// let container = try! ModelContainer(for: Sentence.self, configurations: config)
//
return ParentView()
// .modelContainer(container)
}
Loading

0 comments on commit 5be2e50

Please sign in to comment.