sendTrackScreenRequest method
Implementation
Future<Map<String, dynamic>> sendTrackScreenRequest(String screen) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
if (traceId.isEmpty) {
traceId.value = generateTraceId();
}
Map<String, dynamic> payloadUserDetails = Map.from(userDetails);
if (payloadUserDetails["email"] == null &&
payloadUserDetails["uuid"] == null &&
payloadUserDetails["mobile"] == null) {
String? uuid = prefs.getString("SurveySparrowUUID");
if (uuid != null && uuid.isNotEmpty) {
payloadUserDetails["uuid"] = "uuid";
}
}
Map<String, dynamic> payload = {
"screenName": screen,
"variables": variables,
"userDetails": payloadUserDetails,
"visitor": {
"deviceType": "MOBILE",
"operatingSystem": Platform.operatingSystem,
"screenResolution": {"height": screenHeight, "width": screenWidth},
"currentDate": DateTime.now().toIso8601String(),
"timezone": DateTime.now().timeZoneName,
},
"traceId": traceId.value,
"customProperties": customProperties
};
final Uri url = Uri.parse(
'https://$domainName/api/internal/spotcheck/widget/$targetToken/properties')
.replace(queryParameters: {"isSpotCheck": "true"});
try {
final http.Response response = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json',
},
body: jsonEncode(payload),
);
if (response.statusCode == 200) {
final Map<String, dynamic>? responseJson = jsonDecode(response.body);
if (responseJson?["uuid"] != null) {
prefs.setString(
"SurveySparrowUUID", responseJson?["uuid"].toString() ?? "");
}
if (responseJson?["show"] != null) {
bool show = responseJson?["show"];
if (show) {
setAppearance(responseJson!, screen);
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..loadRequest(Uri.parse(spotcheckURL.value))
..setNavigationDelegate(NavigationDelegate(
onPageFinished: (url) {
isLoading.value = false;
},
))
..addJavaScriptChannel("flutterSpotCheckData",
onMessageReceived: (JavaScriptMessage response) {
try {
var jsonResponse = json.decode(response.message);
log(jsonResponse.toString());
if (jsonResponse['type'] == "spotCheckData") {
var height =
jsonResponse['data']['currentQuestionSize']['height'];
currentQuestionHeight.value = height;
} else if (jsonResponse['type'] == "surveyCompleted") {
end();
}
} catch (e) {
log("Error decoding JSON: $e");
}
});
_isSpotPassed.value = true;
log("Success: Spots or Checks or Visitor or Reccurence Condition Passed");
return {"valid": true};
} else {
log("Error: Spots or Checks or Visitor or Reccurence Condition Failed");
return {"valid": false};
}
} else {
log("Error: Show not Received");
}
if (!_isSpotPassed.value) {
if (responseJson?["checkPassed"] != null) {
bool checkPassed = responseJson?["checkPassed"];
if (checkPassed) {
if (responseJson?["checkCondition"] != null) {
Map<String, dynamic> checkCondition =
responseJson?["checkCondition"];
if (checkCondition["afterDelay"] != null) {
afterDelay.value = checkCondition["afterDelay"];
}
if (checkCondition["customEvent"] != null) {
customEventsSpotChecks.value = [responseJson!];
return {"valid": false};
}
}
setAppearance(responseJson!, screen);
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..loadRequest(Uri.parse(spotcheckURL.value))
..setNavigationDelegate(NavigationDelegate(
onPageFinished: (url) {
isLoading.value = false;
},
))
..addJavaScriptChannel("flutterSpotCheckData",
onMessageReceived: (JavaScriptMessage response) {
try {
var jsonResponse = json.decode(response.message);
log(jsonResponse.toString());
if (jsonResponse['type'] == "spotCheckData") {
var height =
jsonResponse['data']['currentQuestionSize']['height'];
currentQuestionHeight.value = height;
} else if (jsonResponse['type'] == "surveyCompleted") {
end();
}
} catch (e) {
log("Error decoding JSON: $e");
}
});
_isChecksPassed.value = true;
log("Success: Checks Condition Passed");
return {"valid": true};
} else {
log("Error: Checks Condition Failed");
return {"valid": false};
}
} else {
log("Error: CheckPassed not Received");
}
}
if (!_isSpotPassed.value && !_isChecksPassed.value) {
if (responseJson?["multiShow"] != null) {
if (responseJson?["multiShow"]) {
customEventsSpotChecks.value =
responseJson?["resultantSpotCheck"];
Map<String, dynamic> selectedSpotCheck = {};
int minDelay = 4294967296;
for (var spotCheck in customEventsSpotChecks) {
Map<String, dynamic> checks = spotCheck["checks"];
if (checks.isEmpty) {
selectedSpotCheck = spotCheck;
} else if (checks["afterDelay"] != null) {
int delay = checks["afterDelay"];
if (minDelay > delay) {
minDelay = delay;
selectedSpotCheck = spotCheck;
}
}
}
if (selectedSpotCheck.isNotEmpty) {
Map<String, dynamic> checks = selectedSpotCheck["checks"]!;
if (checks.isNotEmpty) {
afterDelay.value = checks["afterDelay"] as int;
}
}
setAppearance(selectedSpotCheck, screen);
if (selectedSpotCheck.isNotEmpty) {
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..loadRequest(Uri.parse(spotcheckURL.value))
..setNavigationDelegate(NavigationDelegate(
onPageFinished: (url) {
isLoading.value = false;
},
))
..addJavaScriptChannel("flutterSpotCheckData",
onMessageReceived: (JavaScriptMessage response) {
try {
var jsonResponse = json.decode(response.message);
log(jsonResponse.toString());
if (jsonResponse['type'] == "spotCheckData") {
var height = jsonResponse['data']['currentQuestionSize']
['height'];
currentQuestionHeight.value = height;
} else if (jsonResponse['type'] == "surveyCompleted") {
end();
}
} catch (e) {
log("Error decoding JSON: $e");
}
});
return {"valid": true};
}
}
} else {
log("Info: MultiShow Not Received");
}
}
return {"valid": false};
} else {
log('Error: ${response.statusCode}');
return {"valid": false};
}
} catch (error) {
log('Error: $error');
return {"valid": false};
}
}