-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sparrow SDK #1282
Merged
Merged
Sparrow SDK #1282
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ccb7ef2
added plugin for sparrow.
jigar-f 8765fa3
Created survey service.
jigar-f 24d2843
Added spot check survey for testing.
jigar-f f3161c7
Add spot check support in homscreen.
jigar-f 79f5fa4
Write survey config on file.
jigar-f 5f32ac7
Use vpn status as enum to avoid mistakes.
jigar-f ef5fb0d
Update home.dart
jigar-f 401a46e
Add country segmentation for survey.
jigar-f a99f586
Added localisation support in survey.
jigar-f 17ba205
Fix Di issue.
jigar-f 39ad392
Complete country segment.
jigar-f de54938
Update app.env step in CI.
jigar-f 46e7a19
Merge branch 'main' into jigar/sparrow-sdk
jigar-f 08ce4bf
Fix merge conflicts.
jigar-f 207a35c
Merge branch 'main' into jigar/sparrow-sdk
jigar-f 4e2e94c
Added support for macos survey.
jigar-f 1b2c57d
Add default testing survey.
jigar-f 7c7a222
Dynamic survey handling.
jigar-f 21a0b58
Read config changes.
jigar-f ee92ab6
Hide survey in windows and Linux.
jigar-f 7189f84
add default contry.
jigar-f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:lantern/core/service/app_purchase.dart'; | ||
import 'package:lantern/core/service/survey_service.dart'; | ||
import 'package:lantern/core/utils/common.dart'; | ||
|
||
final GetIt sl = GetIt.instance; | ||
|
||
void initServices() { | ||
//Inject | ||
sl.registerLazySingleton(() => AppPurchase()); | ||
sl<AppPurchase>().init(); | ||
if (isMobile()) { | ||
sl.registerLazySingleton(() => AppPurchase()); | ||
sl<AppPurchase>().init(); | ||
} | ||
|
||
|
||
sl.registerLazySingleton(() => SurveyService()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:surveysparrow_flutter_sdk/surveysparrow.dart'; | ||
|
||
import '../utils/common.dart'; | ||
|
||
enum SurveyScreens { homeScreen } | ||
|
||
enum SurveyCountry { | ||
russia('ru'), | ||
belarus('by'), | ||
ukraine('ua'), | ||
china('cn'), | ||
iran('ir'), | ||
uae('uae'), | ||
myanmar('mm'), | ||
testing('testing'); | ||
|
||
const SurveyCountry(this.countryCode); | ||
|
||
final String countryCode; | ||
} | ||
|
||
//This class use spot check service for survey | ||
class SurveyService { | ||
// Need to have spot check for each region | ||
// Russia, Belarus, Ukraine, China, Iran, UAE, Myanmar | ||
|
||
SpotCheck? spotCheck; | ||
final int _VPNCONNECTED_COUNT = 10; | ||
|
||
SurveyService() { | ||
if (Platform.isWindows || Platform.isLinux) { | ||
return; | ||
} | ||
_createConfigIfNeeded(); | ||
_countryListener(); | ||
} | ||
|
||
void _countryListener() { | ||
if (sessionModel.country.value!.isNotEmpty) { | ||
createSpotCheckByCountry(sessionModel.country.value!.toLowerCase()); | ||
return; | ||
} | ||
sessionModel.country.addListener(() { | ||
final country = sessionModel.country.value; | ||
if (country != null && country.isNotEmpty) { | ||
appLogger.d('Country found $country'); | ||
createSpotCheckByCountry(country.toLowerCase()); | ||
sessionModel.country | ||
.removeListener(() {}); // Remove listener after getting value | ||
} | ||
}); | ||
} | ||
|
||
//Create method to create spot check by country | ||
//argument by string and use enum for country | ||
//make sure when create country should not be null or empty | ||
SpotCheck createSpotCheckByCountry(String country) { | ||
appLogger.d('Create spot check for country $country'); | ||
if (spotCheck != null) { | ||
return spotCheck!; | ||
} | ||
final surveyCountry = SurveyCountry.values.firstWhere( | ||
(e) => e.countryCode == country, | ||
orElse: () => SurveyCountry.testing, | ||
); | ||
String targetToken; | ||
switch (surveyCountry) { | ||
case SurveyCountry.russia: | ||
targetToken = AppSecret.russiaSpotCheckTargetToken; | ||
break; | ||
case SurveyCountry.belarus: | ||
targetToken = AppSecret.belarusSpotCheckTargetToken; | ||
break; | ||
case SurveyCountry.ukraine: | ||
targetToken = AppSecret.ukraineSpotCheckTargetToken; | ||
break; | ||
case SurveyCountry.china: | ||
targetToken = AppSecret.chinaSpotCheckTargetToken; | ||
break; | ||
case SurveyCountry.iran: | ||
targetToken = AppSecret.iranSpotCheckTargetToken; | ||
break; | ||
case SurveyCountry.uae: | ||
targetToken = AppSecret.UAEspotCheckTargetToken; | ||
break; | ||
case SurveyCountry.myanmar: | ||
targetToken = AppSecret.myanmarSpotCheckTargetToken; | ||
break; | ||
case SurveyCountry.testing: | ||
default: | ||
targetToken = AppSecret.testingSpotCheckTargetToken; | ||
appLogger.d('${country.toUpperCase()} not found, using testing token'); | ||
break; | ||
} | ||
spotCheck = SpotCheck( | ||
domainName: "lantern.surveysparrow.com", | ||
targetToken: targetToken, | ||
userDetails: {}); | ||
return spotCheck!; | ||
} | ||
|
||
void trackScreen(SurveyScreens screen) { | ||
appLogger.d('Track screen $screen'); | ||
spotCheck?.trackScreen(screen.name); | ||
} | ||
|
||
Widget surveyWidget() { | ||
if (Platform.isWindows || Platform.isLinux) { | ||
return const SizedBox(); | ||
} | ||
return spotCheck!; | ||
} | ||
|
||
Future<String> get _surveyConfigPath async { | ||
final cacheDir = await getApplicationCacheDirectory(); | ||
final filePath = '${cacheDir.path}/survey_config.json'; | ||
return filePath; | ||
} | ||
|
||
Future<void> _createConfigIfNeeded() async { | ||
final filePath = await _surveyConfigPath; | ||
final file = File(filePath); | ||
try { | ||
if (!await file.exists()) { | ||
await file.create(recursive: true); | ||
const surveyConfig = {"vpnConnectCount": 0}; | ||
final jsonString = jsonEncode(surveyConfig); | ||
await file.writeAsString(jsonString); | ||
appLogger.d("Write init config done $filePath"); | ||
} | ||
} catch (e) { | ||
appLogger.e("Error while creating config"); | ||
} | ||
} | ||
|
||
Future<void> incrementVpnConnectCount() async { | ||
try { | ||
final content = await readSurveyConfig(); | ||
final surveyConfig = jsonDecode(content.$2) as Map<String, dynamic>; | ||
// Increment the vpnConnectCount field | ||
surveyConfig['vpnConnectCount'] = | ||
(surveyConfig['vpnConnectCount'] ?? 0) + 1; | ||
final updatedJsonString = jsonEncode(surveyConfig); | ||
await content.$1.writeAsString(updatedJsonString); | ||
appLogger.i('vpnConnectCount updated successfully.'); | ||
} catch (e) { | ||
appLogger.i('Failed to update vpnConnectCount: $e'); | ||
} | ||
} | ||
|
||
Future<bool> surveyAvailable() async { | ||
try { | ||
final content = await readSurveyConfig(); | ||
final Map<String, dynamic> surveyConfig = jsonDecode(content.$2); | ||
final vpnConnectCount = surveyConfig['vpnConnectCount'] ?? 0; | ||
appLogger.i('Survey config. ${surveyConfig.toString()}'); | ||
if (vpnConnectCount >= _VPNCONNECTED_COUNT) { | ||
appLogger.d('Survey is available.'); | ||
return true; | ||
} | ||
appLogger.i('Survey is not available.'); | ||
return false; | ||
} catch (e) { | ||
appLogger.e('Failed to check survey availability: $e'); | ||
return false; | ||
} | ||
} | ||
|
||
//this read survey config method will return file and string | ||
Future<(File, String)> readSurveyConfig() async { | ||
final filePath = await _surveyConfigPath; | ||
final file = File(filePath); | ||
final content = await file.readAsString(); | ||
return (file, content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.. you could add a utility method to fetch a SpotCheck for a given country code., and then call trackScreen on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done