You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diplomatic-quarter/lib/core/viewModels/project_view_model.dart

146 lines
4.0 KiB
Dart

import 'dart:async';
import 'package:connectivity/connectivity.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
4 years ago
import 'package:diplomaticquarterapp/core/model/privilege/PrivilegeModel.dart';
import 'package:diplomaticquarterapp/core/service/privilege_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
4 years ago
import 'package:diplomaticquarterapp/locator.dart';
4 years ago
import 'package:diplomaticquarterapp/uitl/PlatformBridge.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
class ProjectViewModel extends BaseViewModel {
4 years ago
// Platform Bridge
PlatformBridge platformBridge() {
4 years ago
return PlatformBridge.shared();
4 years ago
}
AppSharedPreferences sharedPref = AppSharedPreferences();
4 years ago
Locale _appLocale = Locale('ar');
String currentLanguage = 'ar';
bool _isArabic = false;
bool isInternetConnection = true;
bool isLoading = false;
bool isError = false;
String error = '';
4 years ago
dynamic searchvalue;
4 years ago
bool isLogin = false;
4 years ago
4 years ago
dynamic get searchValue => searchvalue;
4 years ago
Locale get appLocal => _appLocale;
LocaleType get localeType => isArabic ? LocaleType.ar : LocaleType.en;
4 years ago
bool get isArabic => _isArabic;
4 years ago
4 years ago
bool isLoginChild = false;
List<PrivilegeModel> privilegeRootUser = List();
List<PrivilegeModel> privilegeChildUser = List();
List<PrivilegeModel> get privileges =>
isLoginChild ? privilegeChildUser : privilegeChildUser;
StreamSubscription subscription;
ProjectViewModel() {
loadSharedPrefLanguage();
4 years ago
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
switch (result) {
case ConnectivityResult.wifi:
isInternetConnection = true;
break;
case ConnectivityResult.mobile:
isInternetConnection = true;
break;
case ConnectivityResult.none:
isInternetConnection = false;
break;
}
notifyListeners();
});
}
Future loadSharedPrefLanguage() async {
4 years ago
currentLanguage = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
_appLocale = Locale(currentLanguage);
_isArabic = currentLanguage == 'ar';
notifyListeners();
}
void changeLanguage(String lan) {
if (lan != "en" && currentLanguage != lan) {
_appLocale = Locale("ar");
_isArabic = true;
currentLanguage = 'ar';
sharedPref.setString(APP_LANGUAGE, 'ar');
} else if (lan != "ar" && currentLanguage != lan) {
_appLocale = Locale("en");
_isArabic = false;
currentLanguage = 'en';
sharedPref.setString(APP_LANGUAGE, 'en');
}
notifyListeners();
}
4 years ago
setPrivilegeModelList(
{List<PrivilegeModel> privilege}) {
this.isLoginChild = isLoginChild;
privilegeRootUser = privilege;
notifyListeners();
}
setPrivilege({privilegeList, bool isLoginChild = false}) {
List<PrivilegeModel> privilege = List();
privilegeList['List'][0]['ListPrivilege'].forEach((item) {
privilege.add(PrivilegeModel.fromJson(item));
});
this.isLoginChild = isLoginChild;
if (isLoginChild)
privilegeChildUser = privilege;
else
privilegeRootUser = privilege;
notifyListeners();
}
setIsLoginChild({@required bool isLoginChild}){
this.isLoginChild = isLoginChild;
notifyListeners();
}
bool havePrivilege(int id) {
bool isHavePrivilege = false;
if(isLoginChild)
privilegeChildUser.forEach((element) {
if (element.iD == id) isHavePrivilege = element.privilege;
});
else{
privilegeRootUser.forEach((element) {
if (element.iD == id) isHavePrivilege = element.privilege;
});
}
return isHavePrivilege;
}
@override
void dispose() {
if (subscription != null) subscription.cancel();
super.dispose();
}
4 years ago
setSearchValue(data) {
searchvalue = data;
notifyListeners();
}
}