-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added plugin for sparrow. * Created survey service. * Added spot check survey for testing. * Add spot check support in homscreen. * Write survey config on file. * Use vpn status as enum to avoid mistakes. * Update home.dart * Add country segmentation for survey. * Added localisation support in survey. * Fix Di issue. * Complete country segment. * Update app.env step in CI. * Fix merge conflicts. * Added support for macos survey. * Add default testing survey. * Dynamic survey handling. * Read config changes. * Hide survey in windows and Linux. * add default contry.
- Loading branch information
Showing
17 changed files
with
446 additions
and
192 deletions.
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.