Skip to content

Commit

Permalink
IOS-3069 Add p2p status support
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatovv committed Jul 12, 2024
1 parent 15bdeaf commit 2ff12de
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Anytype.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@
3D929B2B263302160063F1BE /* TypographyExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D929B2A263302160063F1BE /* TypographyExample.swift */; };
3D929B2F263307700063F1BE /* AnytypeFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D929B2E263307700063F1BE /* AnytypeFont.swift */; };
3D92E9A52C41219B00A1998C /* P2PStatusStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D92E9A42C41219B00A1998C /* P2PStatusStorage.swift */; };
3D92E9A72C41334B00A1998C /* P2PStatusInfoExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D92E9A62C41334B00A1998C /* P2PStatusInfoExtension.swift */; };
3D92E9A92C4133A700A1998C /* NetworkIconProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D92E9A82C4133A700A1998C /* NetworkIconProvider.swift */; };
3D932D5726317EFC00C7D1EA /* CommonViews+Pickers+File+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D932D5326317EFB00C7D1EA /* CommonViews+Pickers+File+UIKit.swift */; };
3D932D5826317EFC00C7D1EA /* BaseFilePickerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D932D5426317EFB00C7D1EA /* BaseFilePickerViewModel.swift */; };
Expand Down Expand Up @@ -3119,6 +3120,7 @@
3D929B2A263302160063F1BE /* TypographyExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypographyExample.swift; sourceTree = "<group>"; };
3D929B2E263307700063F1BE /* AnytypeFont.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnytypeFont.swift; sourceTree = "<group>"; };
3D92E9A42C41219B00A1998C /* P2PStatusStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = P2PStatusStorage.swift; sourceTree = "<group>"; };
3D92E9A62C41334B00A1998C /* P2PStatusInfoExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = P2PStatusInfoExtension.swift; sourceTree = "<group>"; };
3D92E9A82C4133A700A1998C /* NetworkIconProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkIconProvider.swift; sourceTree = "<group>"; };
3D932D5326317EFB00C7D1EA /* CommonViews+Pickers+File+UIKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CommonViews+Pickers+File+UIKit.swift"; sourceTree = "<group>"; };
3D932D5426317EFB00C7D1EA /* BaseFilePickerViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseFilePickerViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9566,6 +9568,7 @@
children = (
3D92E9A82C4133A700A1998C /* NetworkIconProvider.swift */,
3D2ED6D72C3ECA47003718DE /* SyncStatusInfoExtension.swift */,
3D92E9A62C41334B00A1998C /* P2PStatusInfoExtension.swift */,
3DD72C2A2C3FEDDA002C5ACC /* Anytype_Event.Space.SyncError+Localizable.swift */,
);
path = Model;
Expand Down Expand Up @@ -11725,6 +11728,7 @@
2AE276242BD0270000E0244A /* ObjectSettingsCoordinatorView.swift in Sources */,
3DE50F952C130F5C00B92C1F /* SpaceShareUpgradeView.swift in Sources */,
53EDA8802837EC4000887804 /* CreateObjectView.swift in Sources */,
3D92E9A72C41334B00A1998C /* P2PStatusInfoExtension.swift in Sources */,
1244792227D8A8BE00581174 /* StatusSearchRowView.swift in Sources */,
2A845D752B0D027300AFD252 /* WidgetSwipeActionView.swift in Sources */,
2E1AA7FD2BCEBF180089B136 /* SetLayoutSettingsData.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Anytype/Generated/ImageAssets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ internal extension ImageAsset {
static let syncInProgress = ImageAsset.bundle(name: "SyncStatus/sync_in_progress")
static let syncLocalonlyDefault = ImageAsset.bundle(name: "SyncStatus/sync_localonly_default")
static let syncOffline = ImageAsset.bundle(name: "SyncStatus/sync_offline")
static let syncP2pConnected = ImageAsset.bundle(name: "SyncStatus/sync_p2p_connected")
static let syncP2pDefault = ImageAsset.bundle(name: "SyncStatus/sync_p2p_default")
static let syncSelfhostConnected = ImageAsset.bundle(name: "SyncStatus/sync_selfhost_connected")
static let syncSelfhostDefault = ImageAsset.bundle(name: "SyncStatus/sync_selfhost_default")
static let syncSelfhostError = ImageAsset.bundle(name: "SyncStatus/sync_selfhost_error")
Expand Down
10 changes: 9 additions & 1 deletion Anytype/Generated/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal enum Loc {
internal static let color = Loc.tr("Localizable", "Color")
internal static let companiesContactsFriendsAndFamily = Loc.tr("Localizable", "Companies, contacts, friends and family")
internal static let confirm = Loc.tr("Localizable", "Confirm")
internal static let connecting = Loc.tr("Localizable", "Connecting...")
internal static let connecting = Loc.tr("Localizable", "Connecting")
internal static let copied = Loc.tr("Localizable", "Copied")
internal static func copiedToClipboard(_ p1: Any) -> String {
return Loc.tr("Localizable", "copied to clipboard", String(describing: p1))
Expand Down Expand Up @@ -108,6 +108,9 @@ internal enum Loc {
internal static let description = Loc.tr("Localizable", "Description")
internal static let deselectAll = Loc.tr("Localizable", "Deselect all")
internal static let designedToCaptureThoughtsQuickly = Loc.tr("Localizable", "Designed to capture thoughts quickly")
internal static func devicesConnected(_ p1: Int) -> String {
return Loc.tr("Localizable", "Devices connected", p1)
}
internal static let done = Loc.tr("Localizable", "Done")
internal static let download = Loc.tr("Localizable", "Download")
internal static let downloadingOrUploadingDataToSomeNode = Loc.tr("Localizable", "Downloading or uploading data to some node")
Expand Down Expand Up @@ -219,6 +222,8 @@ internal enum Loc {
}
internal static let other = Loc.tr("Localizable", "Other")
internal static let otherRelations = Loc.tr("Localizable", "Other relations")
internal static let p2PConnecting = Loc.tr("Localizable", "P2P Connecting")
internal static let p2PConnection = Loc.tr("Localizable", "P2P Connection")
internal static func paidBy(_ p1: Any) -> String {
return Loc.tr("Localizable", "Paid by", String(describing: p1))
}
Expand Down Expand Up @@ -1709,6 +1714,9 @@ internal enum Loc {
internal static let anytypeNetwork = Loc.tr("Localizable", "SyncStatus.Info.AnytypeNetwork")
internal static let localOnly = Loc.tr("Localizable", "SyncStatus.Info.localOnly")
}
internal enum P2P {
internal static let restricted = Loc.tr("Localizable", "SyncStatus.P2P.Restricted")
}
}
internal enum TalbeOfContents {
internal static let empty = Loc.tr("Localizable", "TalbeOfContents.Empty")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "P2P.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "P2P.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
5 changes: 4 additions & 1 deletion Anytype/Resources/Strings/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@
"Node is not connected" = "Node is not connected";
"Initializing sync" = "Initializing sync";
"Incompatible version" = "Incompatible version";
"Connecting..." = "Connecting...";
"Connecting" = "Connecting...";
"P2P Connecting" = "P2P Connecting...";
"P2P Connection" = "P2P Connection";
"Anytype Network" = "Anytype Network";
"Self Host" = "Self Host";
"Local Only" = "Local Only";
Expand Down Expand Up @@ -1176,6 +1178,7 @@ Please provide specific details of your needs here.";
"SyncStatus.Error.incompatibleVersion" = "Incompatible version";
"SyncStatus.Error.networkError" = "No access to the space";
"SyncStatus.Error.UNRECOGNIZED" = "Unrecognized error";
"SyncStatus.P2P.Restricted" = "Restricted. Check device settings.";


// MARK: - Debug
Expand Down
16 changes: 16 additions & 0 deletions Anytype/Resources/Strings/en.lproj/Localizable.stringsdict
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
<string>%ld items</string>
</dict>
</dict>
<key>Devices connected</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@device@ connected</string>
<key>device</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>ld</string>
<key>one</key>
<string>%ld device</string>
<key>other</key>
<string>%ld devices</string>
</dict>
</dict>
<key>Are you sure you want to delete</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Services
import SwiftUI


extension P2PStatusInfo {
static func `default`(spaceId: String) -> P2PStatusInfo {
var info = P2PStatusInfo()
info.status = .UNRECOGNIZED(1337)
info.spaceID = spaceId
return info
}
}

// Texts
extension P2PStatusInfo {
var networkTitle: String {
switch status {
case .notConnected, .UNRECOGNIZED:
Loc.p2PConnecting
case .notPossible, .connected:
Loc.p2PConnection
}
}

var networkSubtitle: String {
switch status {
case .notConnected, .UNRECOGNIZED:
""
case .notPossible:
Loc.SyncStatus.P2P.restricted
case .connected:
Loc.devicesConnected(Int(devicesCounter))
}
}
}

// MARK: - NetworkIconProvider
extension P2PStatusInfo: NetworkIconProvider {
var icon: ImageAsset {
switch self.status {
case .notConnected, .notPossible, .UNRECOGNIZED:
ImageAsset.SyncStatus.syncP2pDefault
case .connected:
ImageAsset.SyncStatus.syncP2pConnected
}
}

var background: NetworkIconBackground {
switch status {
case .notConnected:
.animation(start: .Light.green, end: .Light.green.opacity(0.5))
case .notPossible, .UNRECOGNIZED:
.static(.Shape.secondary)
case .connected:
.static(.Light.green)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct SyncStatusInfoView: View {
var body: some View {
VStack(spacing: 0) {
DragIndicator()
networkInfo
networkInfo.newDivider()
p2pInfo
}
.padding(8)
.cornerRadius(16, style: .continuous)
Expand All @@ -20,14 +21,34 @@ struct SyncStatusInfoView: View {
}

var networkInfo: some View {
HStack(spacing: 12) {
HStack(alignment: .center, spacing: 12) {
NetworkIconView(iconProvider: $model.syncStatusInfo)
VStack(alignment: .leading, spacing: 0) {
AnytypeText(model.syncStatusInfo.networkTitle, style: .uxTitle2Regular)
.lineLimit(1)
AnytypeText(model.syncStatusInfo.networkSubtitle, style: .relation3Regular)
.foregroundColor(.Text.secondary)
if model.syncStatusInfo.networkSubtitle.isNotEmpty {
AnytypeText(model.syncStatusInfo.networkSubtitle, style: .relation3Regular)
.foregroundColor(.Text.secondary)
.lineLimit(1)
}
}
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 12)
}

var p2pInfo: some View {
HStack(alignment: .center, spacing: 12) {
NetworkIconView(iconProvider: $model.p2pStatusInfo)
VStack(alignment: .leading, spacing: 0) {
AnytypeText(model.p2pStatusInfo.networkTitle, style: .uxTitle2Regular)
.lineLimit(1)
if model.p2pStatusInfo.networkSubtitle.isNotEmpty {
AnytypeText(model.p2pStatusInfo.networkSubtitle, style: .relation3Regular)
.foregroundColor(.Text.secondary)
.lineLimit(1)
}
}
Spacer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import Services
final class SyncStatusInfoViewModel: ObservableObject {
@Injected(\.syncStatusStorage)
private var syncStatusStorage: SyncStatusStorageProtocol
@Injected(\.p2pStatusStorage)
private var p2pStatusStorage: P2PStatusStorageProtocol

@Published var syncStatusInfo: SyncStatusInfo
@Published var p2pStatusInfo: P2PStatusInfo

init(spaceId: String) {
syncStatusInfo = .default(spaceId: spaceId)
p2pStatusInfo = .default(spaceId: spaceId)

Task { await syncStatusStorage.statusPublisher(spaceId: spaceId).assign(to: &$syncStatusInfo) }
}

Task { await p2pStatusStorage.statusPublisher(spaceId: spaceId).assign(to: &$p2pStatusInfo) }
}
}
8 changes: 5 additions & 3 deletions Anytype/Sources/ServiceLayer/P2PStatus/P2PStatusStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ProtobufMessages


protocol P2PStatusStorageProtocol {
func statusPublisher(spaceId: String) async -> AnyPublisher<P2PStatusInfo?, Never>
func statusPublisher(spaceId: String) async -> AnyPublisher<P2PStatusInfo, Never>

func startSubscription() async
func stopSubscriptionAndClean() async
Expand All @@ -20,10 +20,12 @@ actor P2PStatusStorage: P2PStatusStorageProtocol {

init() { }

func statusPublisher(spaceId: String) -> AnyPublisher<P2PStatusInfo?, Never> {
func statusPublisher(spaceId: String) -> AnyPublisher<P2PStatusInfo, Never> {
updatePublisher
.filter { $0?.spaceID == spaceId}
.merge(with: Just(defaultValues[spaceId]))
.compactMap { $0 }
.merge(with: Just(defaultValues[spaceId] ?? .default(spaceId: spaceId)))
.receiveOnMain()
.eraseToAnyPublisher()
}

Expand Down
4 changes: 4 additions & 0 deletions Anytype/Sources/ServiceLayer/ServicesDI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,8 @@ extension Container {
var syncStatusStorage: Factory< any SyncStatusStorageProtocol> {
self { SyncStatusStorage() }.singleton
}

var p2pStatusStorage: Factory< any P2PStatusStorageProtocol> {
self { P2PStatusStorage() }.singleton
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ actor SyncStatusStorage: SyncStatusStorageProtocol {
.filter { $0?.id == spaceId}
.compactMap { $0 }
.merge(with: Just(defaultValues[spaceId] ?? .default(spaceId: spaceId)))
.receiveOnMain()
.eraseToAnyPublisher()
}

Expand Down

0 comments on commit 2ff12de

Please sign in to comment.