@@ -34,6 +34,79 @@ extension CbfClient {
3434 }
3535
3636 func startBackgroundMonitoring( ) {
37+ Task {
38+ var isConnected = false
39+ while true {
40+ if let log = try ? await self . nextLog ( ) {
41+ // Parse specific sync stage messages
42+ if log. contains ( " Attempting to load headers from the database " ) {
43+ await MainActor . run {
44+ NotificationCenter . default. post (
45+ name: NSNotification . Name ( " KyotoProgressUpdate " ) ,
46+ object: nil ,
47+ userInfo: [ " progress " : Float ( 0.2 ) ]
48+ )
49+ }
50+ } else if log. contains ( " ]: headers " ) {
51+ await MainActor . run {
52+ NotificationCenter . default. post (
53+ name: NSNotification . Name ( " KyotoProgressUpdate " ) ,
54+ object: nil ,
55+ userInfo: [ " progress " : Float ( 0.4 ) ]
56+ )
57+ }
58+ } else if log. contains ( " Chain updated " ) {
59+ let components = log. components ( separatedBy: " " )
60+ if components. count >= 4 ,
61+ components [ 0 ] == " Chain " && components [ 1 ] == " updated " ,
62+ let height = UInt32 ( components [ 2 ] )
63+ {
64+ await MainActor . run {
65+ NotificationCenter . default. post (
66+ name: NSNotification . Name ( " KyotoChainHeightUpdate " ) ,
67+ object: nil ,
68+ userInfo: [ " height " : height]
69+ )
70+ }
71+ }
72+
73+ if !isConnected {
74+ isConnected = true
75+ await MainActor . run {
76+ NotificationCenter . default. post (
77+ name: NSNotification . Name ( " KyotoConnectionUpdate " ) ,
78+ object: nil ,
79+ userInfo: [ " connected " : true ]
80+ )
81+ }
82+ }
83+ }
84+
85+ if log. contains ( " Established an encrypted connection " ) && !isConnected {
86+ isConnected = true
87+ await MainActor . run {
88+ NotificationCenter . default. post (
89+ name: NSNotification . Name ( " KyotoConnectionUpdate " ) ,
90+ object: nil ,
91+ userInfo: [ " connected " : true ]
92+ )
93+ }
94+ }
95+
96+ if log. contains ( " Need connections " ) && isConnected {
97+ isConnected = false
98+ await MainActor . run {
99+ NotificationCenter . default. post (
100+ name: NSNotification . Name ( " KyotoConnectionUpdate " ) ,
101+ object: nil ,
102+ userInfo: [ " connected " : false ]
103+ )
104+ }
105+ }
106+ }
107+ try? await Task. sleep ( nanoseconds: 100_000_000 )
108+ }
109+ }
37110
38111 Task {
39112 var hasEstablishedConnection = false
0 commit comments