Compare commits
2 Commits
857db7501d
...
40e840ee50
| Author | SHA1 | Date | |
|---|---|---|---|
| 40e840ee50 | |||
| 2346ca1b49 |
@@ -6,8 +6,19 @@
|
|||||||
objectVersion = 77;
|
objectVersion = 77;
|
||||||
objects = {
|
objects = {
|
||||||
|
|
||||||
|
/* Begin PBXContainerItemProxy section */
|
||||||
|
47D771C32FC2141600C4C002 /* PBXContainerItemProxy */ = {
|
||||||
|
isa = PBXContainerItemProxy;
|
||||||
|
containerPortal = 479E819F2DD09F9400B82386 /* Project object */;
|
||||||
|
proxyType = 1;
|
||||||
|
remoteGlobalIDString = 479E81A62DD09F9400B82386;
|
||||||
|
remoteInfo = Peered;
|
||||||
|
};
|
||||||
|
/* End PBXContainerItemProxy section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
479E81A72DD09F9400B82386 /* Peered.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Peered.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
479E81A72DD09F9400B82386 /* Peered.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Peered.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
|
47D771BF2FC2141600C4C002 /* PeeredTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PeeredTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
|
||||||
@@ -29,6 +40,11 @@
|
|||||||
path = Peered;
|
path = Peered;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
|
47D771C02FC2141600C4C002 /* PeeredTests */ = {
|
||||||
|
isa = PBXFileSystemSynchronizedRootGroup;
|
||||||
|
path = PeeredTests;
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
/* End PBXFileSystemSynchronizedRootGroup section */
|
/* End PBXFileSystemSynchronizedRootGroup section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
@@ -39,6 +55,13 @@
|
|||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
47D771BC2FC2141600C4C002 /* Frameworks */ = {
|
||||||
|
isa = PBXFrameworksBuildPhase;
|
||||||
|
buildActionMask = 2147483647;
|
||||||
|
files = (
|
||||||
|
);
|
||||||
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
};
|
||||||
/* End PBXFrameworksBuildPhase section */
|
/* End PBXFrameworksBuildPhase section */
|
||||||
|
|
||||||
/* Begin PBXGroup section */
|
/* Begin PBXGroup section */
|
||||||
@@ -46,6 +69,7 @@
|
|||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
479E81A92DD09F9400B82386 /* Peered */,
|
479E81A92DD09F9400B82386 /* Peered */,
|
||||||
|
47D771C02FC2141600C4C002 /* PeeredTests */,
|
||||||
479E81A82DD09F9400B82386 /* Products */,
|
479E81A82DD09F9400B82386 /* Products */,
|
||||||
);
|
);
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -54,6 +78,7 @@
|
|||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
479E81A72DD09F9400B82386 /* Peered.app */,
|
479E81A72DD09F9400B82386 /* Peered.app */,
|
||||||
|
47D771BF2FC2141600C4C002 /* PeeredTests.xctest */,
|
||||||
);
|
);
|
||||||
name = Products;
|
name = Products;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -83,6 +108,29 @@
|
|||||||
productReference = 479E81A72DD09F9400B82386 /* Peered.app */;
|
productReference = 479E81A72DD09F9400B82386 /* Peered.app */;
|
||||||
productType = "com.apple.product-type.application";
|
productType = "com.apple.product-type.application";
|
||||||
};
|
};
|
||||||
|
47D771BE2FC2141600C4C002 /* PeeredTests */ = {
|
||||||
|
isa = PBXNativeTarget;
|
||||||
|
buildConfigurationList = 47D771C52FC2141600C4C002 /* Build configuration list for PBXNativeTarget "PeeredTests" */;
|
||||||
|
buildPhases = (
|
||||||
|
47D771BB2FC2141600C4C002 /* Sources */,
|
||||||
|
47D771BC2FC2141600C4C002 /* Frameworks */,
|
||||||
|
47D771BD2FC2141600C4C002 /* Resources */,
|
||||||
|
);
|
||||||
|
buildRules = (
|
||||||
|
);
|
||||||
|
dependencies = (
|
||||||
|
47D771C42FC2141600C4C002 /* PBXTargetDependency */,
|
||||||
|
);
|
||||||
|
fileSystemSynchronizedGroups = (
|
||||||
|
47D771C02FC2141600C4C002 /* PeeredTests */,
|
||||||
|
);
|
||||||
|
name = PeeredTests;
|
||||||
|
packageProductDependencies = (
|
||||||
|
);
|
||||||
|
productName = PeeredTests;
|
||||||
|
productReference = 47D771BF2FC2141600C4C002 /* PeeredTests.xctest */;
|
||||||
|
productType = "com.apple.product-type.bundle.unit-test";
|
||||||
|
};
|
||||||
/* End PBXNativeTarget section */
|
/* End PBXNativeTarget section */
|
||||||
|
|
||||||
/* Begin PBXProject section */
|
/* Begin PBXProject section */
|
||||||
@@ -90,12 +138,16 @@
|
|||||||
isa = PBXProject;
|
isa = PBXProject;
|
||||||
attributes = {
|
attributes = {
|
||||||
BuildIndependentTargetsInParallel = 1;
|
BuildIndependentTargetsInParallel = 1;
|
||||||
LastSwiftUpdateCheck = 1630;
|
LastSwiftUpdateCheck = 2650;
|
||||||
LastUpgradeCheck = 1640;
|
LastUpgradeCheck = 1640;
|
||||||
TargetAttributes = {
|
TargetAttributes = {
|
||||||
479E81A62DD09F9400B82386 = {
|
479E81A62DD09F9400B82386 = {
|
||||||
CreatedOnToolsVersion = 16.3;
|
CreatedOnToolsVersion = 16.3;
|
||||||
};
|
};
|
||||||
|
47D771BE2FC2141600C4C002 = {
|
||||||
|
CreatedOnToolsVersion = 26.5;
|
||||||
|
TestTargetID = 479E81A62DD09F9400B82386;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
buildConfigurationList = 479E81A22DD09F9400B82386 /* Build configuration list for PBXProject "Peered" */;
|
buildConfigurationList = 479E81A22DD09F9400B82386 /* Build configuration list for PBXProject "Peered" */;
|
||||||
@@ -113,6 +165,7 @@
|
|||||||
projectRoot = "";
|
projectRoot = "";
|
||||||
targets = (
|
targets = (
|
||||||
479E81A62DD09F9400B82386 /* Peered */,
|
479E81A62DD09F9400B82386 /* Peered */,
|
||||||
|
47D771BE2FC2141600C4C002 /* PeeredTests */,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
/* End PBXProject section */
|
/* End PBXProject section */
|
||||||
@@ -125,6 +178,13 @@
|
|||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
47D771BD2FC2141600C4C002 /* Resources */ = {
|
||||||
|
isa = PBXResourcesBuildPhase;
|
||||||
|
buildActionMask = 2147483647;
|
||||||
|
files = (
|
||||||
|
);
|
||||||
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
};
|
||||||
/* End PBXResourcesBuildPhase section */
|
/* End PBXResourcesBuildPhase section */
|
||||||
|
|
||||||
/* Begin PBXSourcesBuildPhase section */
|
/* Begin PBXSourcesBuildPhase section */
|
||||||
@@ -135,8 +195,23 @@
|
|||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
47D771BB2FC2141600C4C002 /* Sources */ = {
|
||||||
|
isa = PBXSourcesBuildPhase;
|
||||||
|
buildActionMask = 2147483647;
|
||||||
|
files = (
|
||||||
|
);
|
||||||
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
};
|
||||||
/* End PBXSourcesBuildPhase section */
|
/* End PBXSourcesBuildPhase section */
|
||||||
|
|
||||||
|
/* Begin PBXTargetDependency section */
|
||||||
|
47D771C42FC2141600C4C002 /* PBXTargetDependency */ = {
|
||||||
|
isa = PBXTargetDependency;
|
||||||
|
target = 479E81A62DD09F9400B82386 /* Peered */;
|
||||||
|
targetProxy = 47D771C32FC2141600C4C002 /* PBXContainerItemProxy */;
|
||||||
|
};
|
||||||
|
/* End PBXTargetDependency section */
|
||||||
|
|
||||||
/* Begin XCBuildConfiguration section */
|
/* Begin XCBuildConfiguration section */
|
||||||
479E81C72DD09F9500B82386 /* Debug */ = {
|
479E81C72DD09F9500B82386 /* Debug */ = {
|
||||||
isa = XCBuildConfiguration;
|
isa = XCBuildConfiguration;
|
||||||
@@ -364,6 +439,58 @@
|
|||||||
};
|
};
|
||||||
name = Release;
|
name = Release;
|
||||||
};
|
};
|
||||||
|
47D771C62FC2141600C4C002 /* Debug */ = {
|
||||||
|
isa = XCBuildConfiguration;
|
||||||
|
buildSettings = {
|
||||||
|
BUNDLE_LOADER = "$(TEST_HOST)";
|
||||||
|
CODE_SIGN_STYLE = Automatic;
|
||||||
|
CURRENT_PROJECT_VERSION = 1;
|
||||||
|
DEVELOPMENT_TEAM = LTFJ368N25;
|
||||||
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
|
IPHONEOS_DEPLOYMENT_TARGET = 26.5;
|
||||||
|
MACOSX_DEPLOYMENT_TARGET = 26.5;
|
||||||
|
MARKETING_VERSION = 1.0;
|
||||||
|
PRODUCT_BUNDLE_IDENTIFIER = me.oschly.PeeredTests;
|
||||||
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
|
SDKROOT = auto;
|
||||||
|
STRING_CATALOG_GENERATE_SYMBOLS = NO;
|
||||||
|
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx xros xrsimulator";
|
||||||
|
SWIFT_APPROACHABLE_CONCURRENCY = YES;
|
||||||
|
SWIFT_EMIT_LOC_STRINGS = NO;
|
||||||
|
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;
|
||||||
|
SWIFT_VERSION = 5.0;
|
||||||
|
TARGETED_DEVICE_FAMILY = "1,2,7";
|
||||||
|
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Peered.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Peered";
|
||||||
|
XROS_DEPLOYMENT_TARGET = 26.5;
|
||||||
|
};
|
||||||
|
name = Debug;
|
||||||
|
};
|
||||||
|
47D771C72FC2141600C4C002 /* Release */ = {
|
||||||
|
isa = XCBuildConfiguration;
|
||||||
|
buildSettings = {
|
||||||
|
BUNDLE_LOADER = "$(TEST_HOST)";
|
||||||
|
CODE_SIGN_STYLE = Automatic;
|
||||||
|
CURRENT_PROJECT_VERSION = 1;
|
||||||
|
DEVELOPMENT_TEAM = LTFJ368N25;
|
||||||
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
|
IPHONEOS_DEPLOYMENT_TARGET = 26.5;
|
||||||
|
MACOSX_DEPLOYMENT_TARGET = 26.5;
|
||||||
|
MARKETING_VERSION = 1.0;
|
||||||
|
PRODUCT_BUNDLE_IDENTIFIER = me.oschly.PeeredTests;
|
||||||
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
|
SDKROOT = auto;
|
||||||
|
STRING_CATALOG_GENERATE_SYMBOLS = NO;
|
||||||
|
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx xros xrsimulator";
|
||||||
|
SWIFT_APPROACHABLE_CONCURRENCY = YES;
|
||||||
|
SWIFT_EMIT_LOC_STRINGS = NO;
|
||||||
|
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES;
|
||||||
|
SWIFT_VERSION = 5.0;
|
||||||
|
TARGETED_DEVICE_FAMILY = "1,2,7";
|
||||||
|
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Peered.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Peered";
|
||||||
|
XROS_DEPLOYMENT_TARGET = 26.5;
|
||||||
|
};
|
||||||
|
name = Release;
|
||||||
|
};
|
||||||
/* End XCBuildConfiguration section */
|
/* End XCBuildConfiguration section */
|
||||||
|
|
||||||
/* Begin XCConfigurationList section */
|
/* Begin XCConfigurationList section */
|
||||||
@@ -385,6 +512,15 @@
|
|||||||
defaultConfigurationIsVisible = 0;
|
defaultConfigurationIsVisible = 0;
|
||||||
defaultConfigurationName = Release;
|
defaultConfigurationName = Release;
|
||||||
};
|
};
|
||||||
|
47D771C52FC2141600C4C002 /* Build configuration list for PBXNativeTarget "PeeredTests" */ = {
|
||||||
|
isa = XCConfigurationList;
|
||||||
|
buildConfigurations = (
|
||||||
|
47D771C62FC2141600C4C002 /* Debug */,
|
||||||
|
47D771C72FC2141600C4C002 /* Release */,
|
||||||
|
);
|
||||||
|
defaultConfigurationIsVisible = 0;
|
||||||
|
defaultConfigurationName = Release;
|
||||||
|
};
|
||||||
/* End XCConfigurationList section */
|
/* End XCConfigurationList section */
|
||||||
};
|
};
|
||||||
rootObject = 479E819F2DD09F9400B82386 /* Project object */;
|
rootObject = 479E819F2DD09F9400B82386 /* Project object */;
|
||||||
|
|||||||
@@ -8,70 +8,70 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct AllNotesScreen: View {
|
struct AllNotesScreen: View {
|
||||||
@AppStorage("peered_username") private var username: String?
|
@AppStorage("peered_username") private var username: String?
|
||||||
@State private var notes = [Note]()
|
@State private var notes = [Note]()
|
||||||
@State private var notesClient: NoteEditingSessionClient?
|
@State private var notesClient: NoteEditingSessionClient?
|
||||||
@State private var ownPeer: OwnPeer?
|
@State private var ownPeer: OwnPeer?
|
||||||
|
|
||||||
var isUsernameValid: Bool {
|
var isUsernameValid: Bool {
|
||||||
!(username.map(\.isEmpty) ?? true)
|
!(username.map(\.isEmpty) ?? true)
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
List {
|
List {
|
||||||
Section("Your notes") {
|
Section("Your notes") {
|
||||||
ForEach(notes) { note in
|
ForEach(notes) { note in
|
||||||
NavigationLink(note.name) {
|
NavigationLink(note.name) {
|
||||||
if let ownPeer {
|
if let ownPeer {
|
||||||
NoteEditorScreen(note: note, peer: ownPeer)
|
NoteEditorScreen(note: note, peer: ownPeer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let notesClient {
|
if let notesClient {
|
||||||
Section("External notes") {
|
Section("External notes") {
|
||||||
ForEach(notesClient.invitations) { invitation in
|
ForEach(notesClient.invitations) { invitation in
|
||||||
NavigationLink(invitation.noteName) {
|
NavigationLink(invitation.noteName) {
|
||||||
SharedNoteEditor(invitation: invitation, noteClient: notesClient)
|
SharedNoteEditor(invitation: invitation, noteClient: notesClient)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle("Peered")
|
.navigationTitle("Peered")
|
||||||
.toolbar {
|
.toolbar {
|
||||||
Button("Create note") {
|
Button("Create note") {
|
||||||
NotesStorage().createNote(name: "New Note")
|
NotesStorage().createNote(name: "New Note")
|
||||||
notes = NotesStorage().loadNotes()
|
notes = NotesStorage().loadNotes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
notes = NotesStorage().loadNotes()
|
notes = NotesStorage().loadNotes()
|
||||||
if let username, isUsernameValid {
|
if let username, isUsernameValid {
|
||||||
setupSession(username: username)
|
setupSession(username: username)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChange(of: username) { _, newUsername in
|
.onChange(of: username) { _, newUsername in
|
||||||
guard let newUsername, isUsernameValid else { return }
|
guard let newUsername, isUsernameValid else { return }
|
||||||
setupSession(username: newUsername)
|
setupSession(username: newUsername)
|
||||||
}
|
}
|
||||||
.sheet(isPresented: .constant(!isUsernameValid)) {
|
.sheet(isPresented: .constant(!isUsernameValid)) {
|
||||||
SetUserNameBottomSheetView(username: $username)
|
SetUserNameBottomSheetView(username: $username)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupSession(username: String) {
|
private func setupSession(username: String) {
|
||||||
notesClient?.stopBrowsingForNotes()
|
notesClient?.stopBrowsingForNotes()
|
||||||
let peer = OwnPeer(peer: .init(displayName: username))
|
let peer = OwnPeer(peer: .init(displayName: username))
|
||||||
ownPeer = peer
|
ownPeer = peer
|
||||||
notesClient = .init(peer: peer.peer)
|
notesClient = .init(peer: peer.peer)
|
||||||
notesClient?.startBrowsingForNotes()
|
notesClient?.startBrowsingForNotes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
AllNotesScreen()
|
AllNotesScreen()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,22 +7,22 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ManageMembersScreen: View {
|
struct ManageMembersScreen: View {
|
||||||
@Bindable var noteAdvertiser: NoteEditingSessionServer
|
@Bindable var noteAdvertiser: NoteEditingSessionServer
|
||||||
let noteTitle: String
|
let noteTitle: String
|
||||||
@Binding var noteContent: String
|
@Binding var noteContent: String
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
List(noteAdvertiser.visiblePeers) { peer in
|
List(noteAdvertiser.visiblePeers) { peer in
|
||||||
HStack {
|
HStack {
|
||||||
Text(peer.id)
|
Text(peer.id)
|
||||||
|
|
||||||
Spacer()
|
Spacer()
|
||||||
|
|
||||||
PeerStateButton(peerState: peer.state) {
|
PeerStateButton(peerState: peer.state) {
|
||||||
noteAdvertiser.invite(peer: peer, to: .init(title: noteTitle, noteSnapshot: noteContent))
|
noteAdvertiser.invite(peer: peer, to: .init(title: noteTitle, noteSnapshot: noteContent))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle("Visible users")
|
.navigationTitle("Visible users")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,126 +1,142 @@
|
|||||||
import MultipeerConnectivity
|
|
||||||
import Foundation
|
|
||||||
import Combine
|
import Combine
|
||||||
|
import Foundation
|
||||||
|
import MultipeerConnectivity
|
||||||
|
|
||||||
struct OwnPeer {
|
struct OwnPeer {
|
||||||
let peer: MCPeerID
|
let peer: MCPeerID
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Peer: Identifiable {
|
struct Peer: Identifiable {
|
||||||
enum ConnectionState {
|
enum ConnectionState {
|
||||||
case available
|
case available
|
||||||
case joined
|
case joined
|
||||||
case rejected
|
case rejected
|
||||||
case invitationPending
|
case invitationPending
|
||||||
}
|
}
|
||||||
|
|
||||||
var id: String { mcPeer.displayName }
|
var id: String { mcPeer.displayName }
|
||||||
let mcPeer: MCPeerID
|
let mcPeer: MCPeerID
|
||||||
var state: ConnectionState
|
var state: ConnectionState
|
||||||
}
|
}
|
||||||
|
|
||||||
@Observable
|
@Observable
|
||||||
final class NoteEditingSessionServer: NSObject {
|
final class NoteEditingSessionServer: NSObject {
|
||||||
private let session: MCSession
|
private let session: MCSession
|
||||||
private let browser: MCNearbyServiceBrowser
|
private let browser: MCNearbyServiceBrowser
|
||||||
private(set) var ownPeer: OwnPeer
|
private(set) var ownPeer: OwnPeer
|
||||||
|
|
||||||
var visiblePeers: [Peer] = []
|
var visiblePeers: [Peer] = []
|
||||||
let noteChangesEmitter = PassthroughSubject<NoteMessage, Never>()
|
let noteChangesEmitter = PassthroughSubject<NoteMessage, Never>()
|
||||||
|
|
||||||
init(peer: OwnPeer) {
|
init(peer: OwnPeer) {
|
||||||
ownPeer = peer
|
ownPeer = peer
|
||||||
browser = .init(peer: peer.peer, serviceType: "peered")
|
browser = .init(peer: peer.peer, serviceType: "peered")
|
||||||
session = .init(peer: peer.peer, securityIdentity: nil, encryptionPreference: .required)
|
session = .init(peer: peer.peer, securityIdentity: nil, encryptionPreference: .required)
|
||||||
super.init()
|
super.init()
|
||||||
browser.delegate = self
|
browser.delegate = self
|
||||||
session.delegate = self
|
session.delegate = self
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer() {
|
func startServer() {
|
||||||
browser.startBrowsingForPeers()
|
browser.startBrowsingForPeers()
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopServer() {
|
func stopServer() {
|
||||||
browser.stopBrowsingForPeers()
|
browser.stopBrowsingForPeers()
|
||||||
session.disconnect()
|
session.disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func invite(peer: Peer, to note: NoteInvitation.NoteContent) {
|
func invite(peer: Peer, to note: NoteInvitation.NoteContent) {
|
||||||
guard peer.state == .available, let note = try? JSONEncoder().encode(note) else { return }
|
guard peer.state == .available, let note = try? JSONEncoder().encode(note) else { return }
|
||||||
browser.invitePeer(
|
browser.invitePeer(
|
||||||
peer.mcPeer,
|
peer.mcPeer,
|
||||||
to: session,
|
to: session,
|
||||||
withContext: note,
|
withContext: note,
|
||||||
timeout: 600
|
timeout: 600
|
||||||
)
|
)
|
||||||
guard let idxToUpdate = visiblePeers.firstIndex(where: { $0.mcPeer == peer.mcPeer }) else { return }
|
guard let idxToUpdate = visiblePeers.firstIndex(where: { $0.mcPeer == peer.mcPeer }) else {
|
||||||
visiblePeers[idxToUpdate].state = .invitationPending
|
return
|
||||||
}
|
}
|
||||||
|
visiblePeers[idxToUpdate].state = .invitationPending
|
||||||
|
}
|
||||||
|
|
||||||
func send(note: String, to peers: [MCPeerID]) {
|
func send(note: String, to peers: [MCPeerID]) {
|
||||||
let message = NoteMessage(senderID: ownPeer.peer.displayName, content: note)
|
let message = NoteMessage(senderID: ownPeer.peer.displayName, content: note)
|
||||||
guard !peers.isEmpty, let data = try? JSONEncoder().encode(message) else { return }
|
guard !peers.isEmpty, let data = try? JSONEncoder().encode(message) else { return }
|
||||||
try? session.send(data, toPeers: peers, with: .reliable)
|
try? session.send(data, toPeers: peers, with: .reliable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NoteEditingSessionServer: MCNearbyServiceBrowserDelegate {
|
extension NoteEditingSessionServer: MCNearbyServiceBrowserDelegate {
|
||||||
func browser(
|
func browser(
|
||||||
_ browser: MCNearbyServiceBrowser,
|
_ browser: MCNearbyServiceBrowser,
|
||||||
foundPeer peerID: MCPeerID,
|
foundPeer peerID: MCPeerID,
|
||||||
withDiscoveryInfo info: [String: String]?
|
withDiscoveryInfo info: [String: String]?
|
||||||
) {
|
) {
|
||||||
guard !visiblePeers.contains(where: { $0.mcPeer == peerID }) && peerID.displayName != ownPeer.peer.displayName else { return }
|
guard
|
||||||
DispatchQueue.main.async {
|
!visiblePeers.contains(where: { $0.mcPeer == peerID })
|
||||||
self.visiblePeers.append(Peer(mcPeer: peerID, state: .available))
|
&& peerID.displayName != ownPeer.peer.displayName
|
||||||
}
|
else { return }
|
||||||
}
|
DispatchQueue.main.async {
|
||||||
|
self.visiblePeers.append(Peer(mcPeer: peerID, state: .available))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
|
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
guard let peerIdx = self.visiblePeers.firstIndex(where: { $0.mcPeer == peerID }) else { return }
|
guard let peerIdx = self.visiblePeers.firstIndex(where: { $0.mcPeer == peerID }) else {
|
||||||
self.visiblePeers.remove(at: peerIdx)
|
return
|
||||||
}
|
}
|
||||||
}
|
self.visiblePeers.remove(at: peerIdx)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NoteEditingSessionServer: MCSessionDelegate {
|
extension NoteEditingSessionServer: MCSessionDelegate {
|
||||||
func session(
|
func session(
|
||||||
_ session: MCSession,
|
_ session: MCSession,
|
||||||
peer peerID: MCPeerID,
|
peer peerID: MCPeerID,
|
||||||
didChange state: MCSessionState
|
didChange state: MCSessionState
|
||||||
) {
|
) {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
guard let idx = self.visiblePeers.firstIndex(where: { $0.mcPeer == peerID }) else { return }
|
guard let idx = self.visiblePeers.firstIndex(where: { $0.mcPeer == peerID }) else { return }
|
||||||
switch state {
|
switch state {
|
||||||
case .connected:
|
case .connected:
|
||||||
self.visiblePeers[idx].state = .joined
|
self.visiblePeers[idx].state = .joined
|
||||||
case .notConnected:
|
case .notConnected:
|
||||||
let currentState = self.visiblePeers[idx].state
|
let currentState = self.visiblePeers[idx].state
|
||||||
if currentState == .invitationPending || currentState == .joined {
|
if currentState == .invitationPending || currentState == .joined {
|
||||||
self.visiblePeers[idx].state = .rejected
|
self.visiblePeers[idx].state = .rejected
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
|
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
|
||||||
guard let message = try? JSONDecoder().decode(NoteMessage.self, from: data) else { return }
|
guard let message = try? JSONDecoder().decode(NoteMessage.self, from: data) else { return }
|
||||||
|
|
||||||
let otherPeers = session.connectedPeers.filter { $0 != peerID }
|
let otherPeers = session.connectedPeers.filter { $0 != peerID }
|
||||||
if !otherPeers.isEmpty {
|
if !otherPeers.isEmpty {
|
||||||
try? session.send(data, toPeers: otherPeers, with: .reliable)
|
try? session.send(data, toPeers: otherPeers, with: .reliable)
|
||||||
}
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.noteChangesEmitter.send(message)
|
self.noteChangesEmitter.send(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {}
|
func session(
|
||||||
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {}
|
_ session: MCSession, didReceive stream: InputStream, withName streamName: String,
|
||||||
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?) {}
|
fromPeer peerID: MCPeerID
|
||||||
|
) {}
|
||||||
|
func session(
|
||||||
|
_ session: MCSession, didStartReceivingResourceWithName resourceName: String,
|
||||||
|
fromPeer peerID: MCPeerID, with progress: Progress
|
||||||
|
) {}
|
||||||
|
func session(
|
||||||
|
_ session: MCSession, didFinishReceivingResourceWithName resourceName: String,
|
||||||
|
fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?
|
||||||
|
) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,69 +5,69 @@
|
|||||||
// Created by Oskar Chybowski on 25/09/2025.
|
// Created by Oskar Chybowski on 25/09/2025.
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
import MultipeerConnectivity
|
import MultipeerConnectivity
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
struct NoteEditorScreen: View {
|
struct NoteEditorScreen: View {
|
||||||
let note: Note
|
let note: Note
|
||||||
@State private var noteAdvertiser: NoteEditingSessionServer
|
@State private var noteAdvertiser: NoteEditingSessionServer
|
||||||
@State private var noteContent: String = ""
|
@State private var noteContent: String = ""
|
||||||
@State private var remoteNoteContent: String? = nil
|
@State private var remoteNoteContent: String? = nil
|
||||||
@State private var timer = Timer.publish(every: 5, on: .current, in: .common)
|
@State private var timer = Timer.publish(every: 5, on: .current, in: .common)
|
||||||
.autoconnect()
|
.autoconnect()
|
||||||
@State private var showManageMembers = false
|
@State private var showManageMembers = false
|
||||||
|
|
||||||
init(note: Note, peer: OwnPeer) {
|
init(note: Note, peer: OwnPeer) {
|
||||||
self.note = note
|
self.note = note
|
||||||
self._noteAdvertiser = .init(initialValue: .init(peer: peer))
|
self._noteAdvertiser = .init(initialValue: .init(peer: peer))
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NoteTextEditor(text: $noteContent, remoteText: remoteNoteContent)
|
NoteTextEditor(text: $noteContent, remoteText: remoteNoteContent)
|
||||||
.toolbar {
|
.toolbar {
|
||||||
ToolbarItem(placement: .primaryAction) {
|
ToolbarItem(placement: .primaryAction) {
|
||||||
Button("Manage members") {
|
Button("Manage members") {
|
||||||
showManageMembers = true
|
showManageMembers = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.sheet(isPresented: $showManageMembers) {
|
.sheet(isPresented: $showManageMembers) {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
ManageMembersScreen(
|
ManageMembersScreen(
|
||||||
noteAdvertiser: noteAdvertiser,
|
noteAdvertiser: noteAdvertiser,
|
||||||
noteTitle: note.name,
|
noteTitle: note.name,
|
||||||
noteContent: $noteContent
|
noteContent: $noteContent
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onReceive(noteAdvertiser.noteChangesEmitter) { message in
|
.onReceive(noteAdvertiser.noteChangesEmitter) { message in
|
||||||
guard message.senderID != noteAdvertiser.ownPeer.peer.displayName else { return }
|
guard message.senderID != noteAdvertiser.ownPeer.peer.displayName else { return }
|
||||||
remoteNoteContent = message.content
|
remoteNoteContent = message.content
|
||||||
}
|
}
|
||||||
.task(id: noteContent) {
|
.task(id: noteContent) {
|
||||||
if noteContent == remoteNoteContent { return }
|
if noteContent == remoteNoteContent { return }
|
||||||
let connectedPeers = noteAdvertiser.visiblePeers
|
let connectedPeers = noteAdvertiser.visiblePeers
|
||||||
.filter { $0.state == .joined }
|
.filter { $0.state == .joined }
|
||||||
.map(\.mcPeer)
|
.map(\.mcPeer)
|
||||||
guard !connectedPeers.isEmpty else { return }
|
guard !connectedPeers.isEmpty else { return }
|
||||||
try? await Task.sleep(nanoseconds: 500_000_000)
|
try? await Task.sleep(nanoseconds: 500_000_000)
|
||||||
guard !Task.isCancelled else { return }
|
guard !Task.isCancelled else { return }
|
||||||
noteAdvertiser.send(note: noteContent, to: connectedPeers)
|
noteAdvertiser.send(note: noteContent, to: connectedPeers)
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
noteContent = (try? String(contentsOf: note.path, encoding: .utf8)) ?? ""
|
noteContent = (try? String(contentsOf: note.path, encoding: .utf8)) ?? ""
|
||||||
noteAdvertiser.startServer()
|
noteAdvertiser.startServer()
|
||||||
}
|
}
|
||||||
.onDisappear {
|
.onDisappear {
|
||||||
noteAdvertiser.stopServer()
|
noteAdvertiser.stopServer()
|
||||||
saveNote()
|
saveNote()
|
||||||
}
|
}
|
||||||
.onReceive(timer) { _ in
|
.onReceive(timer) { _ in
|
||||||
saveNote()
|
saveNote()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveNote() {
|
func saveNote() {
|
||||||
try? noteContent.write(to: note.path, atomically: true, encoding: .utf8)
|
try? noteContent.write(to: note.path, atomically: true, encoding: .utf8)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct NoteTextEditor: View {
|
struct NoteTextEditor: View {
|
||||||
@Binding var text: String
|
@Binding var text: String
|
||||||
let remoteText: String?
|
let remoteText: String?
|
||||||
@State private var selection: TextSelection? = nil
|
@State private var selection: TextSelection? = nil
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
TextEditor(text: $text, selection: $selection)
|
TextEditor(text: $text, selection: $selection)
|
||||||
.onChange(of: remoteText) { oldValue, newValue in
|
.onChange(of: remoteText) { oldValue, newValue in
|
||||||
guard let newValue, newValue != text else { return }
|
guard let newValue, newValue != text else { return }
|
||||||
// Apply remote text first
|
// Apply remote text first
|
||||||
text = newValue
|
text = newValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,19 +8,19 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct PeerStateButton: View {
|
struct PeerStateButton: View {
|
||||||
let peerState: Peer.ConnectionState
|
let peerState: Peer.ConnectionState
|
||||||
let onTap: () -> Void
|
let onTap: () -> Void
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
switch peerState {
|
switch peerState {
|
||||||
case .available:
|
case .available:
|
||||||
Button("Invite", action: onTap)
|
Button("Invite", action: onTap)
|
||||||
case .joined:
|
case .joined:
|
||||||
Text("Joined")
|
Text("Joined")
|
||||||
case .rejected:
|
case .rejected:
|
||||||
Text("Rejected")
|
Text("Rejected")
|
||||||
case .invitationPending:
|
case .invitationPending:
|
||||||
Text("Invitation pending")
|
Text("Invitation pending")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,41 +7,41 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct SharedNoteEditor: View {
|
struct SharedNoteEditor: View {
|
||||||
@State var note: String?
|
@State var note: String?
|
||||||
@State var remoteNote: String? = nil
|
@State var remoteNote: String? = nil
|
||||||
@State var invitation: NoteInvitation
|
@State var invitation: NoteInvitation
|
||||||
@Bindable var noteClient: NoteEditingSessionClient
|
@Bindable var noteClient: NoteEditingSessionClient
|
||||||
|
|
||||||
init(
|
init(
|
||||||
invitation: NoteInvitation,
|
invitation: NoteInvitation,
|
||||||
noteClient: NoteEditingSessionClient
|
noteClient: NoteEditingSessionClient
|
||||||
) {
|
) {
|
||||||
self._invitation = .init(initialValue: invitation)
|
self._invitation = .init(initialValue: invitation)
|
||||||
self._noteClient = .init(noteClient)
|
self._noteClient = .init(noteClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ZStack {
|
ZStack {
|
||||||
if let note = Binding($note) {
|
if let note = Binding($note) {
|
||||||
NoteTextEditor(text: note, remoteText: remoteNote)
|
NoteTextEditor(text: note, remoteText: remoteNote)
|
||||||
} else {
|
} else {
|
||||||
ProgressView {
|
ProgressView {
|
||||||
Text("Fetching note...")
|
Text("Fetching note...")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onReceive(noteClient.noteChangesEmitter) { message in
|
.onReceive(noteClient.noteChangesEmitter) { message in
|
||||||
guard message.senderID != noteClient.ownPeer.displayName else { return }
|
guard message.senderID != noteClient.ownPeer.displayName else { return }
|
||||||
remoteNote = message.content
|
remoteNote = message.content
|
||||||
}
|
}
|
||||||
.task(id: note) {
|
.task(id: note) {
|
||||||
try? await Task.sleep(nanoseconds: 500_000_000)
|
try? await Task.sleep(nanoseconds: 500_000_000)
|
||||||
guard !Task.isCancelled, let note else { return }
|
guard !Task.isCancelled, let note else { return }
|
||||||
noteClient.send(note: note, to: invitation.invitatorID)
|
noteClient.send(note: note, to: invitation.invitatorID)
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
invitation.accept()
|
invitation.accept()
|
||||||
note = invitation.note.noteSnapshot
|
note = invitation.note.noteSnapshot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct Note: Identifiable {
|
struct Note: Identifiable {
|
||||||
var id: URL { path }
|
var id: URL { path }
|
||||||
|
|
||||||
let name: String
|
let name: String
|
||||||
let path: URL
|
let path: URL
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,131 +1,140 @@
|
|||||||
import MultipeerConnectivity
|
|
||||||
import Foundation
|
|
||||||
import Combine
|
import Combine
|
||||||
|
import Foundation
|
||||||
|
import MultipeerConnectivity
|
||||||
|
|
||||||
struct NoteInvitation: Identifiable {
|
struct NoteInvitation: Identifiable {
|
||||||
struct NoteContent: Codable {
|
struct NoteContent: Codable {
|
||||||
let title: String
|
let title: String
|
||||||
let noteSnapshot: String
|
let noteSnapshot: String
|
||||||
}
|
}
|
||||||
|
|
||||||
var id: MCPeerID { invitatorID }
|
var id: MCPeerID { invitatorID }
|
||||||
var noteName: String { note.title }
|
var noteName: String { note.title }
|
||||||
let invitatorID: MCPeerID
|
let invitatorID: MCPeerID
|
||||||
let note: NoteContent
|
let note: NoteContent
|
||||||
private var invitationHandler: ((Bool) -> Void)?
|
private var invitationHandler: ((Bool) -> Void)?
|
||||||
|
|
||||||
init(
|
init(
|
||||||
invitatorID: MCPeerID,
|
invitatorID: MCPeerID,
|
||||||
note: NoteContent,
|
note: NoteContent,
|
||||||
invitationHandler: ((Bool) -> Void)? = nil
|
invitationHandler: ((Bool) -> Void)? = nil
|
||||||
) {
|
) {
|
||||||
self.invitatorID = invitatorID
|
self.invitatorID = invitatorID
|
||||||
self.note = note
|
self.note = note
|
||||||
self.invitationHandler = invitationHandler
|
self.invitationHandler = invitationHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func accept() {
|
mutating func accept() {
|
||||||
invitationHandler?(true)
|
invitationHandler?(true)
|
||||||
invitationHandler = nil
|
invitationHandler = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func decline() {
|
mutating func decline() {
|
||||||
invitationHandler?(false)
|
invitationHandler?(false)
|
||||||
invitationHandler = nil
|
invitationHandler = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Observable
|
@Observable
|
||||||
final class NoteEditingSessionClient: NSObject {
|
final class NoteEditingSessionClient: NSObject {
|
||||||
private let session: MCSession
|
private let session: MCSession
|
||||||
private let advertiser: MCNearbyServiceAdvertiser
|
private let advertiser: MCNearbyServiceAdvertiser
|
||||||
private(set) var ownPeer: MCPeerID
|
private(set) var ownPeer: MCPeerID
|
||||||
|
|
||||||
var invitations: [NoteInvitation] = []
|
var invitations: [NoteInvitation] = []
|
||||||
let noteChangesEmitter = PassthroughSubject<NoteMessage, Never>()
|
let noteChangesEmitter = PassthroughSubject<NoteMessage, Never>()
|
||||||
|
|
||||||
init(peer: MCPeerID) {
|
init(peer: MCPeerID) {
|
||||||
ownPeer = peer
|
ownPeer = peer
|
||||||
session = MCSession(
|
session = MCSession(
|
||||||
peer: peer,
|
peer: peer,
|
||||||
securityIdentity: nil,
|
securityIdentity: nil,
|
||||||
encryptionPreference: .required
|
encryptionPreference: .required
|
||||||
)
|
)
|
||||||
advertiser = MCNearbyServiceAdvertiser(
|
advertiser = MCNearbyServiceAdvertiser(
|
||||||
peer: peer,
|
peer: peer,
|
||||||
discoveryInfo: [:],
|
discoveryInfo: [:],
|
||||||
serviceType: "peered"
|
serviceType: "peered"
|
||||||
)
|
)
|
||||||
super.init()
|
super.init()
|
||||||
advertiser.delegate = self
|
advertiser.delegate = self
|
||||||
session.delegate = self
|
session.delegate = self
|
||||||
}
|
}
|
||||||
|
|
||||||
func startBrowsingForNotes() {
|
func startBrowsingForNotes() {
|
||||||
advertiser.startAdvertisingPeer()
|
advertiser.startAdvertisingPeer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopBrowsingForNotes() {
|
func stopBrowsingForNotes() {
|
||||||
advertiser.stopAdvertisingPeer()
|
advertiser.stopAdvertisingPeer()
|
||||||
session.disconnect()
|
session.disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(note: String, to peer: MCPeerID) {
|
func send(note: String, to peer: MCPeerID) {
|
||||||
let message = NoteMessage(senderID: ownPeer.displayName, content: note)
|
let message = NoteMessage(senderID: ownPeer.displayName, content: note)
|
||||||
guard let data = try? JSONEncoder().encode(message) else { return }
|
guard let data = try? JSONEncoder().encode(message) else { return }
|
||||||
try? session.send(data, toPeers: [peer], with: .reliable)
|
try? session.send(data, toPeers: [peer], with: .reliable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NoteEditingSessionClient: MCNearbyServiceAdvertiserDelegate {
|
extension NoteEditingSessionClient: MCNearbyServiceAdvertiserDelegate {
|
||||||
func advertiser(
|
func advertiser(
|
||||||
_ advertiser: MCNearbyServiceAdvertiser,
|
_ advertiser: MCNearbyServiceAdvertiser,
|
||||||
didReceiveInvitationFromPeer peerID: MCPeerID,
|
didReceiveInvitationFromPeer peerID: MCPeerID,
|
||||||
withContext context: Data?,
|
withContext context: Data?,
|
||||||
invitationHandler: @escaping (Bool, MCSession?) -> Void
|
invitationHandler: @escaping (Bool, MCSession?) -> Void
|
||||||
) {
|
) {
|
||||||
guard
|
guard
|
||||||
let context,
|
let context,
|
||||||
let noteContent = try? JSONDecoder().decode(NoteInvitation.NoteContent.self, from: context)
|
let noteContent = try? JSONDecoder().decode(NoteInvitation.NoteContent.self, from: context)
|
||||||
else { return }
|
else { return }
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.invitations.append(
|
self.invitations.append(
|
||||||
.init(
|
.init(
|
||||||
invitatorID: peerID,
|
invitatorID: peerID,
|
||||||
note: noteContent,
|
note: noteContent,
|
||||||
invitationHandler: { [weak self, invitationHandler] accepted in
|
invitationHandler: { [weak self, invitationHandler] accepted in
|
||||||
guard let self else { return }
|
guard let self else { return }
|
||||||
invitationHandler(accepted, self.session)
|
invitationHandler(accepted, self.session)
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.invitations.removeAll { $0.id == peerID }
|
self.invitations.removeAll { $0.id == peerID }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NoteEditingSessionClient: MCSessionDelegate {
|
extension NoteEditingSessionClient: MCSessionDelegate {
|
||||||
func session(
|
func session(
|
||||||
_ session: MCSession,
|
_ session: MCSession,
|
||||||
peer peerID: MCPeerID,
|
peer peerID: MCPeerID,
|
||||||
didChange state: MCSessionState
|
didChange state: MCSessionState
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
func session(
|
func session(
|
||||||
_ session: MCSession,
|
_ session: MCSession,
|
||||||
didReceive data: Data,
|
didReceive data: Data,
|
||||||
fromPeer peerID: MCPeerID
|
fromPeer peerID: MCPeerID
|
||||||
) {
|
) {
|
||||||
guard let message = try? JSONDecoder().decode(NoteMessage.self, from: data) else { return }
|
guard let message = try? JSONDecoder().decode(NoteMessage.self, from: data) else { return }
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.noteChangesEmitter.send(message)
|
self.noteChangesEmitter.send(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {}
|
func session(
|
||||||
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {}
|
_ session: MCSession, didReceive stream: InputStream, withName streamName: String,
|
||||||
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?) {}
|
fromPeer peerID: MCPeerID
|
||||||
|
) {}
|
||||||
|
func session(
|
||||||
|
_ session: MCSession, didStartReceivingResourceWithName resourceName: String,
|
||||||
|
fromPeer peerID: MCPeerID, with progress: Progress
|
||||||
|
) {}
|
||||||
|
func session(
|
||||||
|
_ session: MCSession, didFinishReceivingResourceWithName resourceName: String,
|
||||||
|
fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?
|
||||||
|
) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct NoteInvitationView: View {
|
struct NoteInvitationView: View {
|
||||||
@Binding var invitation: NoteInvitation
|
@Binding var invitation: NoteInvitation
|
||||||
let joinTapped: () -> Void
|
let joinTapped: () -> Void
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
HStack {
|
HStack {
|
||||||
Text(invitation.noteName)
|
Text(invitation.noteName)
|
||||||
Spacer()
|
Spacer()
|
||||||
Button("Join", action: joinTapped)
|
Button("Join", action: joinTapped)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
struct NoteMessage: Codable {
|
struct NoteMessage: Codable {
|
||||||
let senderID: String
|
let senderID: String
|
||||||
let content: String
|
let content: String
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,48 +7,77 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct NotesStorage {
|
protocol StorageProvider {
|
||||||
let storageProvider: FileManager = FileManager.default
|
func contentsOfDirectory(atPath path: String) throws -> [String]
|
||||||
|
|
||||||
func loadNotes() -> [Note] {
|
@discardableResult
|
||||||
let files = try! storageProvider
|
func createFile(
|
||||||
.contentsOfDirectory(atPath: URL.documentsDirectory.path)
|
atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey: Any]?
|
||||||
var notes = [Note]()
|
) -> Bool
|
||||||
|
}
|
||||||
for file in files.compactMap({
|
|
||||||
URL(
|
extension FileManager: StorageProvider {}
|
||||||
filePath: $0,
|
|
||||||
directoryHint: .notDirectory,
|
struct NotesStorage {
|
||||||
relativeTo: URL.documentsDirectory
|
let storageProvider: StorageProvider
|
||||||
)
|
let rootDirectory: URL
|
||||||
}) {
|
|
||||||
let name = file.lastPathComponent
|
init(
|
||||||
let note = Note(
|
storageProvider: StorageProvider = FileManager.default,
|
||||||
name: name,
|
rootDirectory: URL = .documentsDirectory
|
||||||
path: file
|
) {
|
||||||
)
|
self.storageProvider = storageProvider
|
||||||
|
self.rootDirectory = rootDirectory
|
||||||
notes.append(note)
|
}
|
||||||
}
|
|
||||||
|
func loadNotes() -> [Note] {
|
||||||
return notes
|
let files =
|
||||||
}
|
try! storageProvider
|
||||||
|
.contentsOfDirectory(atPath: rootDirectory.path)
|
||||||
func createNote(name: String) {
|
var notes = [Note]()
|
||||||
let currentNotes = loadNotes()
|
|
||||||
var index: Int? = nil
|
for file in files.compactMap({
|
||||||
var proposedName: String {
|
URL(
|
||||||
index.map { name + " \($0)" } ?? name
|
filePath: $0,
|
||||||
}
|
directoryHint: .notDirectory,
|
||||||
|
relativeTo: rootDirectory
|
||||||
while currentNotes.contains(where: { $0.name == proposedName }) {
|
)
|
||||||
if let _index = index {
|
}) {
|
||||||
index = _index + 1
|
let name = file.lastPathComponent
|
||||||
} else {
|
let note = Note(
|
||||||
index = 1
|
name: name,
|
||||||
}
|
path: file
|
||||||
}
|
)
|
||||||
let pathToWrite = URL.documentsDirectory.appendingPathComponent(proposedName).appendingPathExtension(for: .text)
|
notes.append(note)
|
||||||
try! String().write(to: pathToWrite, atomically: true, encoding: .utf8)
|
}
|
||||||
}
|
|
||||||
|
return notes
|
||||||
|
}
|
||||||
|
|
||||||
|
func createNote(name: String) {
|
||||||
|
let currentNotes = loadNotes()
|
||||||
|
var index: Int? = nil
|
||||||
|
var proposedName: String {
|
||||||
|
index.map { name + " \($0)" } ?? name
|
||||||
|
}
|
||||||
|
|
||||||
|
while currentNotes.contains(where: { $0.name == proposedName }) {
|
||||||
|
if let _index = index {
|
||||||
|
index = _index + 1
|
||||||
|
} else {
|
||||||
|
index = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pathToWrite =
|
||||||
|
rootDirectory
|
||||||
|
.appendingPathComponent(proposedName)
|
||||||
|
.appendingPathExtension(for: .text)
|
||||||
|
|
||||||
|
storageProvider.createFile(
|
||||||
|
atPath: pathToWrite.path,
|
||||||
|
contents: Data(),
|
||||||
|
attributes: nil
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
@testable import Peered
|
||||||
|
|
||||||
|
final class InMemoryStorageProvider: StorageProvider {
|
||||||
|
private(set) var files: [String: Data] = [:]
|
||||||
|
|
||||||
|
func contentsOfDirectory(atPath path: String) throws -> [String] {
|
||||||
|
return files.keys.compactMap { filePath -> String? in
|
||||||
|
guard filePath.hasPrefix(path) else { return nil }
|
||||||
|
let remainder = String(filePath.dropFirst(path.count))
|
||||||
|
let stripped = remainder.hasPrefix("/") ? String(remainder.dropFirst()) : remainder
|
||||||
|
guard !stripped.isEmpty, !stripped.contains("/") else { return nil }
|
||||||
|
return stripped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
func createFile(
|
||||||
|
atPath path: String,
|
||||||
|
contents data: Data?,
|
||||||
|
attributes attr: [FileAttributeKey: Any]?
|
||||||
|
) -> Bool {
|
||||||
|
files[path] = data ?? Data()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@testable import Peered
|
||||||
|
|
||||||
|
@Suite
|
||||||
|
struct NoteContentCodableTests {
|
||||||
|
@Test("encode → decode")
|
||||||
|
func coding() throws {
|
||||||
|
let original = NoteInvitation.NoteContent(
|
||||||
|
title: "My note", noteSnapshot: "Note Sample Snapshot")
|
||||||
|
let data = try JSONEncoder().encode(original)
|
||||||
|
let decoded = try JSONDecoder().decode(NoteInvitation.NoteContent.self, from: data)
|
||||||
|
|
||||||
|
#expect(decoded.title == original.title)
|
||||||
|
#expect(decoded.noteSnapshot == original.noteSnapshot)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func containsExpectedKeys() throws {
|
||||||
|
let content = NoteInvitation.NoteContent(title: "Titlee", noteSnapshot: "Snapshott")
|
||||||
|
let data = try JSONEncoder().encode(content)
|
||||||
|
let json = try #require(try? JSONSerialization.jsonObject(with: data) as? [String: String])
|
||||||
|
|
||||||
|
#expect(json["title"] == "Titlee")
|
||||||
|
#expect(json["noteSnapshot"] == "Snapshott")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
import MultipeerConnectivity
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@testable import Peered
|
||||||
|
|
||||||
|
extension NoteInvitation {
|
||||||
|
fileprivate init(handler: @escaping (Bool) -> Void) {
|
||||||
|
self.init(
|
||||||
|
invitatorID: .init(
|
||||||
|
displayName: "host"
|
||||||
|
),
|
||||||
|
note: .init(title: "Shared note", noteSnapshot: "Snapshot"),
|
||||||
|
invitationHandler: handler
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suite
|
||||||
|
struct NoteInvitationTests {
|
||||||
|
@Test
|
||||||
|
func acceptCallsHandlerWithTrue() {
|
||||||
|
var received: Bool? = nil
|
||||||
|
var invitation = NoteInvitation { received = $0 }
|
||||||
|
|
||||||
|
invitation.accept()
|
||||||
|
|
||||||
|
#expect(received == true)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func declineCallsHandlerWithFalse() {
|
||||||
|
var received: Bool? = nil
|
||||||
|
var invitation = NoteInvitation { received = $0 }
|
||||||
|
|
||||||
|
invitation.decline()
|
||||||
|
|
||||||
|
#expect(received == false)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func acceptIsIdempotent() {
|
||||||
|
var callCount = 0
|
||||||
|
var invitation = NoteInvitation { _ in callCount += 1 }
|
||||||
|
|
||||||
|
invitation.accept()
|
||||||
|
invitation.accept()
|
||||||
|
|
||||||
|
#expect(callCount == 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func declineIsIdempotent() {
|
||||||
|
var callCount = 0
|
||||||
|
var invitation = NoteInvitation { _ in callCount += 1 }
|
||||||
|
|
||||||
|
invitation.decline()
|
||||||
|
invitation.decline()
|
||||||
|
|
||||||
|
#expect(callCount == 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func declineAfterAcceptIsNoOp() {
|
||||||
|
var callCount = 0
|
||||||
|
var invitation = NoteInvitation { _ in callCount += 1 }
|
||||||
|
|
||||||
|
invitation.accept()
|
||||||
|
invitation.decline()
|
||||||
|
|
||||||
|
#expect(callCount == 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func noteNameReturnsTitle() {
|
||||||
|
let invitation = NoteInvitation { _ in }
|
||||||
|
#expect(invitation.noteName == "Shared note")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func idIsInvitatorPeerID() {
|
||||||
|
let peerID = MCPeerID(displayName: "host")
|
||||||
|
let invitation = NoteInvitation(
|
||||||
|
invitatorID: peerID,
|
||||||
|
note: .init(title: "T", noteSnapshot: "S")
|
||||||
|
)
|
||||||
|
#expect(invitation.id == peerID)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@testable import Peered
|
||||||
|
|
||||||
|
@Suite
|
||||||
|
struct NoteMessageCodableTests {
|
||||||
|
@Test
|
||||||
|
func encodesToJSON() throws {
|
||||||
|
let message = NoteMessage(senderID: "host", content: "Content")
|
||||||
|
let data = try JSONEncoder().encode(message)
|
||||||
|
let json = try #require(try? JSONSerialization.jsonObject(with: data) as? [String: String])
|
||||||
|
|
||||||
|
#expect(json["senderID"] == "host")
|
||||||
|
#expect(json["content"] == "Content")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func decodesFromJSON() throws {
|
||||||
|
let json = #"{"senderID":"host","content":"contentt"}"#
|
||||||
|
let data = try #require(json.data(using: .utf8))
|
||||||
|
let message = try JSONDecoder().decode(NoteMessage.self, from: data)
|
||||||
|
|
||||||
|
#expect(message.senderID == "host")
|
||||||
|
#expect(message.content == "contentt")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("encode → decode")
|
||||||
|
func coding() throws {
|
||||||
|
let original = NoteMessage(senderID: "host", content: "coding test")
|
||||||
|
let data = try JSONEncoder().encode(original)
|
||||||
|
let decoded = try JSONDecoder().decode(NoteMessage.self, from: data)
|
||||||
|
|
||||||
|
#expect(decoded.senderID == original.senderID)
|
||||||
|
#expect(decoded.content == original.content)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@testable import Peered
|
||||||
|
|
||||||
|
@Suite
|
||||||
|
struct NotesStorageTests {
|
||||||
|
private func makeStorage(root: URL = URL(filePath: "/test")) -> (
|
||||||
|
NotesStorage, InMemoryStorageProvider
|
||||||
|
) {
|
||||||
|
let provider = InMemoryStorageProvider()
|
||||||
|
let storage = NotesStorage(storageProvider: provider, rootDirectory: root)
|
||||||
|
return (storage, provider)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func loadNotesReturnsEmptyArrayWhenNoFiles() {
|
||||||
|
let (storage, _) = makeStorage()
|
||||||
|
let notes = storage.loadNotes()
|
||||||
|
#expect(notes.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func createNoteCreatesFile() {
|
||||||
|
let root = URL(filePath: "/test")
|
||||||
|
let (storage, provider) = makeStorage(root: root)
|
||||||
|
|
||||||
|
storage.createNote(name: "My note")
|
||||||
|
|
||||||
|
#expect(!provider.files.isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func loadNotesReturnCreatedNote() {
|
||||||
|
let root = URL(filePath: "/test")
|
||||||
|
let (storage, provider) = makeStorage(root: root)
|
||||||
|
|
||||||
|
let filePath = root.appendingPathComponent("Note").path
|
||||||
|
provider.createFile(atPath: filePath, contents: Data(), attributes: nil)
|
||||||
|
|
||||||
|
let notes = storage.loadNotes()
|
||||||
|
|
||||||
|
#expect(notes.count == 1)
|
||||||
|
#expect(notes.first?.name == "Note")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func createNoteDeduplicatesNames() {
|
||||||
|
let root = URL(filePath: "/test")
|
||||||
|
let (storage, provider) = makeStorage(root: root)
|
||||||
|
|
||||||
|
let existingPath = root.appendingPathComponent("Note").path
|
||||||
|
provider.createFile(atPath: existingPath, contents: Data(), attributes: nil)
|
||||||
|
|
||||||
|
storage.createNote(name: "Note")
|
||||||
|
|
||||||
|
#expect(provider.files.count == 2)
|
||||||
|
|
||||||
|
let paths = provider.files.keys
|
||||||
|
#expect(paths.contains(where: { $0.contains("Note 1") }))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func createNoteTwiceCreatesTwoFiles() {
|
||||||
|
let root = URL(filePath: "/test")
|
||||||
|
let (storage, _) = makeStorage(root: root)
|
||||||
|
|
||||||
|
storage.createNote(name: "Note")
|
||||||
|
storage.createNote(name: "Note")
|
||||||
|
|
||||||
|
let notes = storage.loadNotes()
|
||||||
|
#expect(notes.count == 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user