@@ -3,20 +3,25 @@ import UIKit
3
3
4
4
import Survicate
5
5
6
- public class SwiftSurvicateFlutterSdkPlugin : NSObject , FlutterPlugin , SurvicateDelegate {
7
-
6
+ public class SwiftSurvicateFlutterSdkPlugin : NSObject , FlutterPlugin {
7
+ private var survicateDelegate : FlutterSurvicateDelegate ?
8
+
8
9
class FlutterChannel : NSObject {
9
10
static let shared : FlutterChannel = FlutterChannel ( )
11
+
10
12
var channel : FlutterMethodChannel ?
13
+
11
14
private override init ( ) { }
12
15
}
13
16
14
17
public static func register( with registrar: FlutterPluginRegistrar ) {
15
18
let channel = FlutterMethodChannel ( name: " survicate_flutter_sdk " , binaryMessenger: registrar. messenger ( ) )
16
19
let instance = SwiftSurvicateFlutterSdkPlugin ( )
20
+
21
+ instance. survicateDelegate = FlutterSurvicateDelegate ( channel)
17
22
registrar. addMethodCallDelegate ( instance, channel: channel)
18
-
19
23
FlutterChannel . shared. channel = channel
24
+
20
25
Survicate . shared. initialize ( )
21
26
}
22
27
@@ -25,18 +30,23 @@ public class SwiftSurvicateFlutterSdkPlugin: NSObject, FlutterPlugin, SurvicateD
25
30
26
31
if ( call. method == " invokeEvent " ) {
27
32
let eventName = arguments [ " eventName " ] ?? " "
33
+
28
34
Survicate . shared. invokeEvent ( name: eventName)
35
+
29
36
result ( true )
30
37
} else if ( call. method == " enterScreen " ) {
31
38
let screenName = arguments [ " screenName " ] ?? " "
39
+
32
40
Survicate . shared. enterScreen ( value: screenName)
41
+
33
42
result ( true )
34
43
} else if ( call. method == " leaveScreen " ) {
35
44
let screenName = arguments [ " screenName " ] ?? " "
45
+
36
46
Survicate . shared. leaveScreen ( value: screenName)
47
+
37
48
result ( true )
38
49
} else if ( call. method == " setUserTraits " ) {
39
-
40
50
var traits = [ UserTrait] ( )
41
51
42
52
for (key, value) in arguments {
@@ -48,45 +58,59 @@ public class SwiftSurvicateFlutterSdkPlugin: NSObject, FlutterPlugin, SurvicateD
48
58
result ( true )
49
59
} else if ( call. method == " reset " ) {
50
60
Survicate . shared. reset ( )
61
+
51
62
result ( true )
52
63
} else if ( call. method == " registerSurveyListeners " ) {
53
- Survicate . shared. delegate = self
64
+ Survicate . shared. delegate = survicateDelegate
65
+
54
66
result ( true )
55
67
} else if ( call. method == " unregisterSurveyListeners " ) {
56
68
Survicate . shared. delegate = nil
69
+
57
70
result ( true )
58
- } else {
71
+ } else {
59
72
result ( FlutterMethodNotImplemented)
60
73
}
61
74
}
75
+ }
76
+
77
+ class FlutterSurvicateDelegate : SurvicateDelegate {
78
+ let channel : FlutterMethodChannel ?
79
+
80
+ init ( _ channel: FlutterMethodChannel ) {
81
+ self . channel = channel
82
+ }
62
83
63
84
public func surveyDisplayed( surveyId: String ) {
64
85
let arguments = [ " surveyId " : surveyId]
86
+
65
87
DispatchQueue . main. async {
66
- FlutterChannel . shared . channel? . invokeMethod ( " onSurveyDisplayed " , arguments: arguments)
88
+ self . channel? . invokeMethod ( " onSurveyDisplayed " , arguments: arguments)
67
89
}
68
90
}
69
91
70
92
public func questionAnswered( surveyId: String , questionId: Int , answer: SurvicateAnswer ) {
71
93
let answerMap = [ " type " : answer. type as Any , " id " : answer. id as Any , " ids " : answer. ids as Any , " value " : answer. value as Any ] as [ String : Any ]
72
-
73
94
let arguments = [ " surveyId " : surveyId, " questionId " : questionId, " answer " : answerMap] as [ String : Any ]
95
+
74
96
DispatchQueue . main. async {
75
- FlutterChannel . shared . channel? . invokeMethod ( " onQuestionAnswered " , arguments: arguments)
97
+ self . channel? . invokeMethod ( " onQuestionAnswered " , arguments: arguments)
76
98
}
77
99
}
78
100
79
101
public func surveyCompleted( surveyId: String ) {
80
102
let arguments = [ " surveyId " : surveyId]
103
+
81
104
DispatchQueue . main. async {
82
- FlutterChannel . shared . channel? . invokeMethod ( " onSurveyCompleted " , arguments: arguments)
105
+ self . channel? . invokeMethod ( " onSurveyCompleted " , arguments: arguments)
83
106
}
84
107
}
85
108
86
109
public func surveyClosed( surveyId: String ) {
87
110
let arguments = [ " surveyId " : surveyId]
111
+
88
112
DispatchQueue . main. async {
89
- FlutterChannel . shared . channel? . invokeMethod ( " onSurveyClosed " , arguments: arguments)
113
+ self . channel? . invokeMethod ( " onSurveyClosed " , arguments: arguments)
90
114
}
91
115
}
92
116
}
0 commit comments