diff --git a/lib/box_user.dart b/lib/box_user.dart new file mode 100644 index 0000000..379df87 --- /dev/null +++ b/lib/box_user.dart @@ -0,0 +1,25 @@ +import 'package:objectbox/objectbox.dart'; +import 'objectbox.g.dart'; + +// ignore_for_file: public_member_api_docs + +@Entity() +class User { + // specify the id + @Id() + int id = 0; + + int id_utilisateur; + String login; + String nom; + String prenom; + String photo; + + User( + {this.id = 0, + required this.id_utilisateur, + required this.login, + required this.nom, + required this.prenom, + required this.photo}); +} diff --git a/lib/main.dart b/lib/main.dart index acdb238..53dbff6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,6 +8,8 @@ import 'package:mobdr/cubit/language/language_cubit.dart'; import 'package:mobdr/cubit/language/app_localizations.dart'; import 'package:mobdr/cubit/language/initial_language.dart'; +import 'package:mobdr/service/shared_prefs.dart'; + import 'package:mobdr/ui/splash_screen.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -25,6 +27,8 @@ Future main() async { // to store the database in. WidgetsFlutterBinding.ensureInitialized(); + await SharedPrefs().init(); + objectbox = await ObjectBox.create(); SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]) diff --git a/lib/network/api_provider.dart b/lib/network/api_provider.dart index e83f342..db418fe 100644 --- a/lib/network/api_provider.dart +++ b/lib/network/api_provider.dart @@ -4,8 +4,8 @@ This page is used to get data from API */ import 'package:mobdr/config/constant.dart'; -import 'package:mobdr/service/local_storage.dart'; -import 'package:mobdr/model/login.dart'; +import 'package:mobdr/main.dart'; +import 'package:mobdr/service/shared_prefs.dart'; import 'package:dio/dio.dart'; import 'dart:convert'; import 'package:crypto/crypto.dart'; @@ -146,7 +146,39 @@ class ApiProvider { switch (response.data['autorisation']) { case 1: - LocalStorage.saveLoginData(LoginModel.fromJson(response.data)); + + /// save user data + SharedPrefs().id_utilisateur = response.data['id_utilisateur']; + SharedPrefs().email = response.data['email']; + SharedPrefs().expire = response.data['expire']; + SharedPrefs().guid = response.data['guid']; + SharedPrefs().langage = response.data['langage']; + SharedPrefs().last_traduction = response.data['last_traduction']; + SharedPrefs().login = response.data['login']; + SharedPrefs().nom = response.data['nom']; + SharedPrefs().prenom = response.data['prenom']; + SharedPrefs().version = response.data['version']; + SharedPrefs().photo = response.data['photo']; + + /// get image from url + Response ReponseImg = + await dio.get(ApiConstants.baseUrl + response.data['photo'], + options: Options( + responseType: ResponseType.bytes, + contentType: 'application/octet-stream', + )); + + /// convert bytes to base64 string + String base64Photo = base64.encode(ReponseImg.data); + + /// create box user + objectbox.addUSer( + response.data['id_utilisateur'], + response.data['login'], + response.data['nom'], + response.data['prenom'], + base64Photo); + return 'OK'; case -1: return 'Compte désactivé ...'; @@ -260,6 +292,7 @@ class ApiProvider { } } + /* Future> login2( String email, String password, apiToken) async { var postData = { @@ -276,6 +309,7 @@ class ApiProvider { throw response.data['msg']; } } + */ /* Future> getProductGrid( diff --git a/lib/objectbox-model.json b/lib/objectbox-model.json index 5188865..0d8c8fd 100644 --- a/lib/objectbox-model.json +++ b/lib/objectbox-model.json @@ -31,17 +31,61 @@ } ], "relations": [] + }, + { + "id": "3:6664528022814238868", + "lastPropertyId": "6:1514784951041121129", + "name": "User", + "properties": [ + { + "id": "1:4611296599843691135", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:3082719507680383784", + "name": "photo", + "type": 9 + }, + { + "id": "3:6869254152255514608", + "name": "login", + "type": 9 + }, + { + "id": "4:3830288742147875657", + "name": "nom", + "type": 9 + }, + { + "id": "5:1856556503877986423", + "name": "prenom", + "type": 9 + }, + { + "id": "6:1514784951041121129", + "name": "id_utilisateur", + "type": 6 + } + ], + "relations": [] } ], - "lastEntityId": "1:2802681814019499133", + "lastEntityId": "3:6664528022814238868", "lastIndexId": "0:0", "lastRelationId": "0:0", "lastSequenceId": "0:0", "modelVersion": 5, "modelVersionParserMinimum": 5, - "retiredEntityUids": [], + "retiredEntityUids": [ + 7401686910042688313 + ], "retiredIndexUids": [], - "retiredPropertyUids": [], + "retiredPropertyUids": [ + 402019719780433349, + 2876428622751679696 + ], "retiredRelationUids": [], "version": 1 } \ No newline at end of file diff --git a/lib/objectbox.dart b/lib/objectbox.dart index 1d139cb..c4e2fbe 100644 --- a/lib/objectbox.dart +++ b/lib/objectbox.dart @@ -1,3 +1,4 @@ +import 'box_user.dart'; import 'model.dart'; import 'objectbox.g.dart'; // created by `flutter pub run build_runner build` @@ -11,13 +12,24 @@ class ObjectBox { /// A Box of notes. late final Box noteBox; + /// A Box of user. + late final Box userBox; + ObjectBox._create(this.store) { noteBox = Box(store); + userBox = Box(store); // Add some demo data if the box is empty. if (noteBox.isEmpty()) { _putDemoData(); } + + userBox.removeAll(); + + // Add some demo data if the box is empty. + if (userBox.isEmpty()) { + _putUserAdminData(); + } } /// Create an instance of ObjectBox to use throughout the app. @@ -36,6 +48,17 @@ class ObjectBox { store.runInTransactionAsync(TxMode.write, _putNotesInTx, demoNotes); } + String getUserAvatar(int id_utilisateur) { + final query = + userBox.query(User_.id_utilisateur.equals(id_utilisateur)).build(); + var p = query.findFirst(); + return p!.photo; + } + + void _putUserAdminData() { + //addUSer(0, 'root', 'admim', 'admin', ''); + } + Stream> getNotes() { // Query for all notes, sorted by their date. // https://docs.objectbox.io/queries @@ -69,4 +92,22 @@ class ObjectBox { // here. To keep it simple, this example just puts a single object. store.box().put(Note(text)); } + + Future addUSer(int _id_utilisateur, String _login, String _nom, + String _prenom, String _photo) => + store.runInTransactionAsync( + TxMode.write, + _addUserInTx, + User( + id_utilisateur: _id_utilisateur, + login: _login, + nom: _nom, + prenom: _prenom, + photo: _photo)); + + static void _addUserInTx(Store store, _User) { + // Perform ObjectBox operations that take longer than a few milliseconds + // here. To keep it simple, this example just puts a single object. + store.box().put(_User); + } } diff --git a/lib/objectbox.g.dart b/lib/objectbox.g.dart index aca6092..d7f60cc 100644 --- a/lib/objectbox.g.dart +++ b/lib/objectbox.g.dart @@ -14,6 +14,7 @@ import 'package:objectbox/internal.dart'; // generated code can access "internal import 'package:objectbox/objectbox.dart'; import 'package:objectbox_flutter_libs/objectbox_flutter_libs.dart'; +import 'box_user.dart'; import 'model.dart'; export 'package:objectbox/objectbox.dart'; // so that callers only have to import this file @@ -47,6 +48,45 @@ final _entities = [ flags: 0) ], relations: [], + backlinks: []), + ModelEntity( + id: const IdUid(3, 6664528022814238868), + name: 'User', + lastPropertyId: const IdUid(6, 1514784951041121129), + flags: 0, + properties: [ + ModelProperty( + id: const IdUid(1, 4611296599843691135), + name: 'id', + type: 6, + flags: 1), + ModelProperty( + id: const IdUid(2, 3082719507680383784), + name: 'photo', + type: 9, + flags: 0), + ModelProperty( + id: const IdUid(3, 6869254152255514608), + name: 'login', + type: 9, + flags: 0), + ModelProperty( + id: const IdUid(4, 3830288742147875657), + name: 'nom', + type: 9, + flags: 0), + ModelProperty( + id: const IdUid(5, 1856556503877986423), + name: 'prenom', + type: 9, + flags: 0), + ModelProperty( + id: const IdUid(6, 1514784951041121129), + name: 'id_utilisateur', + type: 6, + flags: 0) + ], + relations: [], backlinks: []) ]; @@ -70,13 +110,13 @@ Future openStore( ModelDefinition getObjectBoxModel() { final model = ModelInfo( entities: _entities, - lastEntityId: const IdUid(1, 2802681814019499133), + lastEntityId: const IdUid(3, 6664528022814238868), lastIndexId: const IdUid(0, 0), lastRelationId: const IdUid(0, 0), lastSequenceId: const IdUid(0, 0), - retiredEntityUids: const [], + retiredEntityUids: const [7401686910042688313], retiredIndexUids: const [], - retiredPropertyUids: const [], + retiredPropertyUids: const [402019719780433349, 2876428622751679696], retiredRelationUids: const [], modelVersion: 5, modelVersionParserMinimum: 5, @@ -116,6 +156,48 @@ ModelDefinition getObjectBoxModel() { date: DateTime.fromMillisecondsSinceEpoch( const fb.Int64Reader().vTableGet(buffer, rootOffset, 10, 0))); + return object; + }), + User: EntityDefinition( + model: _entities[1], + toOneRelations: (User object) => [], + toManyRelations: (User object) => {}, + getId: (User object) => object.id, + setId: (User object, int id) { + object.id = id; + }, + objectToFB: (User object, fb.Builder fbb) { + final photoOffset = fbb.writeString(object.photo); + final loginOffset = fbb.writeString(object.login); + final nomOffset = fbb.writeString(object.nom); + final prenomOffset = fbb.writeString(object.prenom); + fbb.startTable(7); + fbb.addInt64(0, object.id); + fbb.addOffset(1, photoOffset); + fbb.addOffset(2, loginOffset); + fbb.addOffset(3, nomOffset); + fbb.addOffset(4, prenomOffset); + fbb.addInt64(5, object.id_utilisateur); + fbb.finish(fbb.endTable()); + return object.id; + }, + objectFromFB: (Store store, ByteData fbData) { + final buffer = fb.BufferContext(fbData); + final rootOffset = buffer.derefObject(0); + + final object = User( + id: const fb.Int64Reader().vTableGet(buffer, rootOffset, 4, 0), + id_utilisateur: + const fb.Int64Reader().vTableGet(buffer, rootOffset, 14, 0), + login: const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 8, ''), + nom: const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 10, ''), + prenom: const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 12, ''), + photo: const fb.StringReader(asciiOptimization: true) + .vTableGet(buffer, rootOffset, 6, '')); + return object; }) }; @@ -137,3 +219,25 @@ class Note_ { /// see [Note.date] static final date = QueryIntegerProperty(_entities[0].properties[3]); } + +/// [User] entity fields to define ObjectBox queries. +class User_ { + /// see [User.id] + static final id = QueryIntegerProperty(_entities[1].properties[0]); + + /// see [User.photo] + static final photo = QueryStringProperty(_entities[1].properties[1]); + + /// see [User.login] + static final login = QueryStringProperty(_entities[1].properties[2]); + + /// see [User.nom] + static final nom = QueryStringProperty(_entities[1].properties[3]); + + /// see [User.prenom] + static final prenom = QueryStringProperty(_entities[1].properties[4]); + + /// see [User.id_utilisateur] + static final id_utilisateur = + QueryIntegerProperty(_entities[1].properties[5]); +} diff --git a/lib/service/local_storage.dart b/lib/old/local_storage.dart similarity index 97% rename from lib/service/local_storage.dart rename to lib/old/local_storage.dart index 3c5fe6a..fd7cfca 100644 --- a/lib/service/local_storage.dart +++ b/lib/old/local_storage.dart @@ -3,8 +3,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:mobdr/model/login.dart'; -import 'package:mobdr/model/user_profile.dart'; +import 'package:mobdr/old/login.dart'; +import 'package:mobdr/old/user_profile.dart'; class LocalStorage { // static Future get securePref async => diff --git a/lib/model/login.dart b/lib/old/login.dart similarity index 100% rename from lib/model/login.dart rename to lib/old/login.dart diff --git a/lib/model/user_profile.dart b/lib/old/user_profile.dart similarity index 100% rename from lib/model/user_profile.dart rename to lib/old/user_profile.dart diff --git a/lib/service/shared_prefs.dart b/lib/service/shared_prefs.dart new file mode 100644 index 0000000..8a2fe52 --- /dev/null +++ b/lib/service/shared_prefs.dart @@ -0,0 +1,91 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +class SharedPrefs { + static late SharedPreferences _sharedPrefs; + + factory SharedPrefs() => SharedPrefs._internal(); + + SharedPrefs._internal(); + + Future init() async { + _sharedPrefs = await SharedPreferences.getInstance(); + } + + /// get/set id_utilisateur + int get id_utilisateur => _sharedPrefs.getInt('key_id_utilisateur') ?? 0; + + set id_utilisateur(int value) { + _sharedPrefs.setInt('key_id_utilisateur', value); + } + + /// get/set expire + int get expire => _sharedPrefs.getInt('key_expire') ?? 0; + + set expire(int value) { + _sharedPrefs.setInt('key_expire', value); + } + + /// get/set email + String get email => _sharedPrefs.getString('key_email') ?? ""; + + set email(String value) { + _sharedPrefs.setString('key_email', value); + } + + /// get/set guid + String get guid => _sharedPrefs.getString('key_guid') ?? ""; + + set guid(String value) { + _sharedPrefs.setString('key_guid', value); + } + + /// get/set langage + String get langage => _sharedPrefs.getString('key_langage') ?? ""; + + set langage(String value) { + _sharedPrefs.setString('key_langage', value); + } + + /// get/set last_traduction + String get last_traduction => + _sharedPrefs.getString('key_last_traduction') ?? ""; + + set last_traduction(String value) { + _sharedPrefs.setString('key_last_traduction', value); + } + + /// get/set login + String get login => _sharedPrefs.getString('key_login') ?? ""; + + set login(String value) { + _sharedPrefs.setString('key_login', value); + } + + /// get/set nom + String get nom => _sharedPrefs.getString('key_nom') ?? ""; + + set nom(String value) { + _sharedPrefs.setString('key_nom', value); + } + + /// get/set prenom + String get prenom => _sharedPrefs.getString('key_prenom') ?? ""; + + set prenom(String value) { + _sharedPrefs.setString('key_prenom', value); + } + + /// get/set version + String get version => _sharedPrefs.getString('key_version') ?? ""; + + set version(String value) { + _sharedPrefs.setString('key_version', value); + } + + /// get/set photo + String get photo => _sharedPrefs.getString('key_photo') ?? ""; + + set photo(String value) { + _sharedPrefs.setString('key_photo', value); + } +} diff --git a/lib/ui/account/account_information/account_information.dart b/lib/ui/account/account_information/account_information.dart index d90a5c3..796a791 100644 --- a/lib/ui/account/account_information/account_information.dart +++ b/lib/ui/account/account_information/account_information.dart @@ -1,5 +1,6 @@ import 'package:mobdr/config/constant.dart'; import 'package:mobdr/config/global_style.dart'; +import 'package:mobdr/service/shared_prefs.dart'; import 'package:mobdr/ui/account/account_information/edit_email.dart'; import 'package:mobdr/ui/account/account_information/edit_name.dart'; import 'package:mobdr/ui/account/account_information/edit_phone_number.dart'; @@ -16,7 +17,6 @@ class AccountInformationPage extends StatefulWidget { class _AccountInformationPageState extends State { // initialize reusable widget final _reusableWidget = ReusableWidget(); - @override void initState() { super.initState(); @@ -63,7 +63,7 @@ class _AccountInformationPageState extends State { children: [ Expanded( child: Text( - 'Robert Steven', + "${SharedPrefs().prenom} ${SharedPrefs().nom}", style: GlobalStyle.accountInformationValue, ), ), diff --git a/lib/ui/account/tab_account.dart b/lib/ui/account/tab_account.dart index 13a2edf..259191d 100644 --- a/lib/ui/account/tab_account.dart +++ b/lib/ui/account/tab_account.dart @@ -5,6 +5,7 @@ we used AutomaticKeepAliveClientMixin to keep the state when moving from 1 navba import 'package:mobdr/config/constant.dart'; import 'package:mobdr/config/global_style.dart'; +import 'package:mobdr/service/shared_prefs.dart'; import 'package:mobdr/ui/account/about.dart'; import 'package:mobdr/ui/account/account_information/account_information.dart'; import 'package:mobdr/ui/account/last_seen_product.dart'; @@ -17,9 +18,10 @@ import 'package:mobdr/ui/account/terms_conditions.dart'; import 'package:mobdr/ui/general/chat_us.dart'; import 'package:mobdr/ui/general/notification.dart'; import 'package:mobdr/ui/reusable/reusable_widget.dart'; -import 'package:mobdr/ui/reusable/cache_image_network.dart'; import 'package:flutter/material.dart'; import 'package:mobdr/ui/authentication/signin.dart'; +import 'dart:convert'; +import 'package:mobdr/main.dart'; class TabAccountPage extends StatefulWidget { @override @@ -31,6 +33,11 @@ class _TabAccountPageState extends State // initialize reusable widget final _reusableWidget = ReusableWidget(); + Image imageFromBase64String() { + String base64String = objectbox.getUserAvatar(SharedPrefs().id_utilisateur); + return Image.memory(base64Decode(base64String)); + } + @override bool get wantKeepAlive => true; @@ -157,11 +164,7 @@ class _TabAccountPageState extends State radius: profilePictureSize - 4, child: Hero( tag: 'profilePicture', - child: ClipOval( - child: buildCacheNetworkImage( - width: profilePictureSize - 4, - height: profilePictureSize - 4, - url: GLOBAL_URL + '/user/avatar.png')), + child: ClipOval(child: imageFromBase64String()), ), ), ), @@ -174,7 +177,7 @@ class _TabAccountPageState extends State child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text('Robert Steven', + Text("${SharedPrefs().prenom} ${SharedPrefs().nom}", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), SizedBox( diff --git a/lib/ui/home/tab_home.dart b/lib/ui/home/tab_home.dart index 64c3dba..6c41e65 100644 --- a/lib/ui/home/tab_home.dart +++ b/lib/ui/home/tab_home.dart @@ -7,6 +7,7 @@ import 'package:mobdr/config/constant.dart'; import 'package:mobdr/cubit/language/language_cubit.dart'; import 'package:mobdr/cubit/language/app_localizations.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:mobdr/service/shared_prefs.dart'; import 'package:mobdr/config/global_style.dart'; import 'package:mobdr/model/wishlist_model.dart'; import 'package:mobdr/ui/general/chat_us.dart'; @@ -16,7 +17,6 @@ import 'package:mobdr/ui/reusable/reusable_widget.dart'; import 'package:mobdr/ui/reusable/cache_image_network.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:mobdr/service/local_storage.dart'; class TabHomePage extends StatefulWidget { @override @@ -36,21 +36,19 @@ class _TabHomePageState extends State bool get wantKeepAlive => true; String defaultLang = 'en'; - String UserName = ''; late LanguageCubit _languageCubit; @override void initState() { _languageCubit = BlocProvider.of(context); + _getLocale().then((val) { setState(() { defaultLang = val!; }); }); - LocalStorage.getLoginData().then((value) => UserName = value!.prenom); - super.initState(); } @@ -84,8 +82,7 @@ class _TabHomePageState extends State elevation: GlobalStyle.appBarElevation, title: Text( AppLocalizations.of(context)!.translate('i18n_hello')! + - ', ' + - UserName, + ', ${SharedPrefs().prenom}', style: GlobalStyle.appBarTitle, ), backgroundColor: GlobalStyle.appBarBackgroundColor,