1
1
import 'dart:developer' ;
2
+ import 'dart:io' ;
2
3
3
4
import 'package:apidash_core/apidash_core.dart' ;
4
5
import 'package:flutter/material.dart' ;
@@ -29,21 +30,24 @@ final requestSequenceProvider = StateProvider<List<String>>((ref) {
29
30
});
30
31
31
32
final httpClientManager = HttpClientManager ();
33
+ final WebSocketManager webSocketManager = WebSocketManager ();
32
34
33
35
final StateNotifierProvider <CollectionStateNotifier , Map <String , RequestModel >?>
34
36
collectionStateNotifierProvider =
35
37
StateNotifierProvider ((ref) => CollectionStateNotifier (
36
38
ref,
37
39
hiveHandler,
38
40
httpClientManager,
39
- ));
41
+ webSocketManager
42
+ ));
40
43
41
44
class CollectionStateNotifier
42
45
extends StateNotifier <Map <String , RequestModel >?> {
43
46
CollectionStateNotifier (
44
47
this .ref,
45
48
this .hiveHandler,
46
49
this .httpClientManager,
50
+ this .webSocketManager
47
51
) : super (null ) {
48
52
var status = loadData ();
49
53
Future .microtask (() {
@@ -61,6 +65,8 @@ class CollectionStateNotifier
61
65
final HiveHandler hiveHandler;
62
66
final baseHttpResponseModel = const HttpResponseModel ();
63
67
final HttpClientManager httpClientManager;
68
+ final WebSocketManager webSocketManager;
69
+
64
70
65
71
bool hasId (String id) => state? .keys.contains (id) ?? false ;
66
72
@@ -471,8 +477,8 @@ class CollectionStateNotifier
471
477
return ;
472
478
}
473
479
RequestModel ? requestModel = state! [requestId];
474
-
475
- if (requestModel ? .webSocketRequestModel == null ) {
480
+ var currentWebSocketRequestModel = requestModel ! .webSocketRequestModel;
481
+ if (currentWebSocketRequestModel == null ) {
476
482
return ;
477
483
}
478
484
@@ -482,20 +488,51 @@ class CollectionStateNotifier
482
488
483
489
// set current model's isWorking to true and update state
484
490
var map = {...state! };
485
- map[requestId] = requestModel! .copyWith (
491
+ map[requestId] = requestModel.copyWith (
486
492
isWorking: true ,
487
493
);
488
494
state = map;
489
495
490
496
491
497
492
- final client = httpClientManager.createWebSocketClient (requestId);
493
- await client.sendText (requestModel.webSocketRequestModel! );
498
+ String message = currentWebSocketRequestModel.message ?? '' ;
499
+ (String ? ,DateTime ? ,String ? ) frame = await webSocketManager.sendText (requestId,message);
500
+
501
+ var newWebSocketResponseModel;
502
+ if (frame.$1 != null ){
503
+ newWebSocketResponseModel = requestModel.webSocketResponseModel! .copyWith (
504
+ frames: [
505
+ ...? requestModel.webSocketResponseModel? .frames,
506
+ WebSocketFrameModel (
507
+ id: getNewUuid (),
508
+ message: frame.$1! ,
509
+ timeStamp: frame.$2,
510
+ isSend: true
511
+ ),
512
+ ],
513
+ );
514
+
515
+ }else if (frame.$3 != null ){
516
+ newWebSocketResponseModel = requestModel.webSocketResponseModel! .copyWith (
517
+ frames: [
518
+ ...? requestModel.webSocketResponseModel? .frames,
519
+ WebSocketFrameModel (
520
+ id: getNewUuid (),
521
+ message: frame.$3! ,
522
+ timeStamp: null ,
523
+ isSend: true
524
+ ),
525
+ ],
526
+ );
527
+
528
+
529
+ }
530
+
531
+
494
532
495
533
496
534
final newRequestModel = requestModel.copyWith (
497
- webSocketRequestModel: requestModel.webSocketRequestModel,
498
- isWorking: false ,
535
+ webSocketResponseModel: newWebSocketResponseModel,
499
536
);
500
537
// update state with response data
501
538
map = {...state! };
@@ -517,6 +554,7 @@ class CollectionStateNotifier
517
554
518
555
RequestModel ? requestModel = state! [requestId];
519
556
557
+
520
558
// if (requestModel?.webSocketRequestModel == null) {
521
559
// print("no web socket request model");
522
560
// return;
@@ -525,48 +563,61 @@ class CollectionStateNotifier
525
563
print ("entered null" );
526
564
return ;
527
565
}
528
-
529
- final client = httpClientManager. createWebSocketClient (requestId );
566
+ webSocketManager. createWebSocketClient (requestId);
567
+ // webSocketClient.pingDuration = Duration(seconds: 5 );
530
568
final url = requestModel! .webSocketRequestModel! .url;
531
- (String ? ,DateTime ? ) result = await client .connect (url);
569
+ (String ? ,DateTime ? ) result = await webSocketManager .connect (requestId, url);
532
570
533
571
var map = {...state! };
534
572
map[requestId] = requestModel.copyWith (
535
573
isWorking: result.$1 == "Connected" ,
536
574
sendingTime: result.$2,
537
- webSocketResponseModel: WebSocketResponseModel (),
575
+ webSocketResponseModel: const WebSocketResponseModel (),
538
576
);
577
+
539
578
state = map;
540
579
541
580
542
- client.listen (
581
+ webSocketManager.listen (
582
+ requestId,
543
583
(message) async {
544
584
var map = {...state! };
545
585
RequestModel ? requestModel = state! [requestId];
546
586
WebSocketResponseModel webSocketResponseModel = requestModel! .webSocketResponseModel! ;
547
587
WebSocketResponseModel newWebSocketResponseModel = webSocketResponseModel.copyWith (
548
588
frames: [...webSocketResponseModel.frames, WebSocketFrameModel (
549
- id: '1' ,
589
+ id: getNewUuid () ,
550
590
message: message,
551
591
timeStamp: DateTime .now (),
592
+ isSend: false
552
593
)]
553
594
);
554
- map[requestId] = requestModel.copyWith (
595
+ var newRequestModel = requestModel.copyWith (
555
596
webSocketResponseModel: newWebSocketResponseModel,
556
597
);
557
- state = map;
598
+
599
+
600
+ map = {...state! };
601
+ map[requestId] = newRequestModel;
602
+ state = map;
558
603
print (message);
559
604
},
560
605
onError: (error) async {
606
+ print ("error found" );
607
+
561
608
// var map = {...state!};
562
609
// map[requestId] = requestModel.copyWith(
563
610
// responseStatus: -1,
564
611
// message: error.toString(),
565
612
// isWorking: false,
566
613
// );
567
614
// state = map;
615
+
568
616
},
569
617
onDone: () async {
618
+ print ("Connection done" );
619
+
620
+
570
621
// var map = {...state!};
571
622
// map[requestId] = requestModel.copyWith(
572
623
// responseStatus: 200,
@@ -575,36 +626,85 @@ class CollectionStateNotifier
575
626
// );
576
627
// state = map;
577
628
},
629
+ cancelOnError: false ,
578
630
);
579
631
}
632
+ Future <void > disconnect () async {
633
+ final requestId = ref.read (selectedIdStateProvider);
634
+ if (requestId == null || state == null ) {
635
+ print (requestId);
636
+ return ;
637
+ }
580
638
581
- // ignore: unused_element
582
- // Future<void> disconnect() async {
583
- // print("connect fired");
584
- // final requestId = ref.read(selectedIdStateProvider);
585
- // ref.read(codePaneVisibleStateProvider.notifier).state = false;
586
- // if (requestId == null || state == null) {
587
- // print(requestId);
588
- // return;
589
- // }
590
-
639
+ webSocketManager.disconnect (requestId);
591
640
592
641
593
642
594
- // RequestModel? requestModel = state![requestId];
643
+ RequestModel ? requestModel = state! [requestId];
644
+ WebSocketRequestModel webSocketRequestModel = requestModel! .webSocketRequestModel! ;
645
+ WebSocketRequestModel newWebSocketRequestModel = webSocketRequestModel.copyWith (
646
+ isConnected: false
647
+ );
648
+
649
+ var newRequestModel = requestModel.copyWith (
650
+ isWorking: false ,
651
+ webSocketRequestModel: newWebSocketRequestModel,
652
+
653
+ );
654
+
655
+
656
+ var map = {...state! };
657
+ map[requestId] = newRequestModel;
658
+ state = map;
595
659
660
+ }
661
+
662
+
663
+ void deleteAllFrames (){
664
+ final requestId = ref.read (selectedIdStateProvider);
665
+ if (requestId == null || state == null ) {
666
+ print (requestId);
667
+ return ;
668
+ }
669
+ RequestModel ? requestModel = state! [requestId];
670
+ WebSocketResponseModel webSocketResponseModel = requestModel! .webSocketResponseModel! ;
671
+ WebSocketResponseModel newWebSocketResponseModel = webSocketResponseModel.copyWith (
672
+ frames: [],
673
+ );
596
674
597
- // // if (requestModel?.webSocketRequestModel == null) {
598
- // // print("no web socket request model");
599
- // // return;
600
- // // }
675
+ var newRequestModel = requestModel.copyWith (
676
+ webSocketResponseModel: newWebSocketResponseModel,
677
+ );
678
+ var map = {...state! };
679
+ map[requestId] = newRequestModel;
680
+ state = map;
681
+ }
601
682
602
- // final client = httpClientManager.(requestId);
683
+ void deleteFrame (String id){
684
+ final requestId = ref.read (selectedIdStateProvider);
685
+ if (requestId == null || state == null ) {
686
+ print (requestId);
687
+ return ;
688
+ }
689
+ RequestModel ? requestModel = state! [requestId];
690
+ if (requestModel == null || state == null ) {
691
+ print (requestId);
692
+ return ;
693
+ }
694
+ WebSocketResponseModel webSocketResponseModel = requestModel! .webSocketResponseModel! ;
695
+ List <WebSocketFrameModel > newFrames = requestModel! .webSocketResponseModel! .frames.where ((element) => element.id != id).toList ();
696
+ WebSocketResponseModel newWebSocketResponseModel = webSocketResponseModel.copyWith (
697
+ frames: newFrames,
698
+ );
699
+
700
+ var newRequestModel = requestModel.copyWith (
701
+ webSocketResponseModel: newWebSocketResponseModel,
702
+ );
703
+ var map = {...state! };
704
+ map[requestId] = newRequestModel;
705
+ state = map;
706
+
707
+ }
603
708
604
- // var map = {...state!};
605
- // map[requestId] = requestModel!.copyWith(
606
- // isWorking: false,
607
- // );
608
- // state = map;
609
- // }
709
+
610
710
}
0 commit comments