@@ -20,6 +20,7 @@ public class SyncEngine : IDisposable
2020
2121 public event Action < string > ? ConnectionStateChanged ;
2222 public event Action < Network . ClipboardItem > ? ClipboardItemReceived ;
23+ public event Action ? ClipboardHistoryUpdated ;
2324 public event Action < string > ? ErrorOccurred ;
2425 public event Action < List < Network . Device > > ? DeviceListUpdated ;
2526
@@ -156,7 +157,7 @@ private async void OnWebSocketMessage(string message)
156157 _heartbeatTimer ? . OnHeartbeatAck ( ) ;
157158 break ;
158159 case "clipboard_history" :
159- HandleClipboardHistory ( wsMessage ) ;
160+ await HandleClipboardHistoryAsync ( wsMessage ) ;
160161 break ;
161162 case "device_list_response" :
162163 HandleDeviceListResponse ( wsMessage ) ;
@@ -278,13 +279,54 @@ private async Task HandleClipboardSync(WebSocketMessage message)
278279
279280 if ( _database != null )
280281 {
281- await _database . InsertClipboardItemAsync ( item ) ;
282+ _ = await _database . InsertClipboardItemAsync ( item ) ;
282283 }
283284 }
284285
285- private void HandleClipboardHistory ( WebSocketMessage message )
286+ private async Task HandleClipboardHistoryAsync ( WebSocketMessage message )
286287 {
287- // Handle clipboard history response if needed
288+ var itemsArray = message . Payload ? . Property ( "items" ) ? . Value ;
289+ if ( itemsArray == null )
290+ {
291+ AppLogger . Warn ( "SyncEngine" , "收到 clipboard_history,但缺少 items 字段" ) ;
292+ return ;
293+ }
294+
295+ var importedCount = 0 ;
296+ foreach ( var itemToken in itemsArray )
297+ {
298+ var item = new Network . ClipboardItem
299+ {
300+ Id = GetLong ( itemToken , "id" ) ,
301+ ContentType = GetString ( itemToken , "content_type" , "text" ) ,
302+ Content = GetString ( itemToken , "content" ) ,
303+ Format = GetString ( itemToken , "format" ) ,
304+ Size = GetLong ( itemToken , "size" ) ,
305+ Checksum = GetString ( itemToken , "checksum" ) ,
306+ SourceDeviceId = GetString ( itemToken , "source_device_id" ) ,
307+ SourceDeviceName = GetString ( itemToken , "source_device_name" , "Unknown device" ) ,
308+ CreatedAt = GetLong ( itemToken , "created_at" )
309+ } ;
310+
311+ if ( string . IsNullOrEmpty ( item . Content ) || string . IsNullOrEmpty ( item . Checksum ) )
312+ {
313+ continue ;
314+ }
315+
316+ if ( _database != null && await _database . InsertClipboardItemAsync ( item ) )
317+ {
318+ importedCount ++ ;
319+ }
320+ }
321+
322+ var total = GetLong ( message . Payload , "total" ) ;
323+ var hasMore = GetBool ( message . Payload , "has_more" ) ;
324+ AppLogger . Info ( "SyncEngine" , $ "收到剪贴板历史: imported={ importedCount } , total={ total } , has_more={ hasMore } ") ;
325+
326+ if ( importedCount > 0 )
327+ {
328+ ClipboardHistoryUpdated ? . Invoke ( ) ;
329+ }
288330 }
289331
290332 private void HandleDeviceListResponse ( WebSocketMessage message )
@@ -479,7 +521,7 @@ private async Task SaveClipboardItemAsync(ClipboardChangedEventArgs args, string
479521 CreatedAt = DateTimeOffset . UtcNow . ToUnixTimeMilliseconds ( )
480522 } ;
481523
482- await _database . InsertClipboardItemAsync ( item ) ;
524+ _ = await _database . InsertClipboardItemAsync ( item ) ;
483525 }
484526
485527 private async Task PublishClipboardChangedEventAsync ( ClipboardChangedEventArgs args )
0 commit comments