| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter_secure_storage/flutter_secure_storage.dart';
- import 'package:json_annotation/json_annotation.dart';
- import 'package:e2ee_chat/common/global.dart';
- import 'user.dart';
- class Profile {
- Profile({
- this.theme = 0xFF2196F3, this.user, this.isLogin = false, this.isLogout = false,
- this.locale = const Locale("zh", "CN")
- });
- int theme;
- User? user;
- bool isLogin;
- bool isLogout;
- Locale locale;
- String? get username => user?.username;
- Future<String?> get token async {
- if (user == null) return null;
- final storage = FlutterSecureStorage();
- return await storage.read(key: _tokenKey);
- }
- setToken(String? token) async {
- final storage = FlutterSecureStorage();
- return await storage.write(key: _tokenKey, value: token);
- }
- String get _tokenKey => "e2ee_chat token of $username";
- factory Profile.fromJson(Map<String, dynamic> json) {
- return Profile(
- theme: json["theme"],
- user: User.fromJson(jsonDecode(json["user"])),
- isLogin: json["isLogin"],
- isLogout: json["isLogout"],
- locale: Locale(json["lang"], json["country"])
- );
- }
- Map<String, dynamic> toJson() => {
- "theme": theme,
- "user": jsonEncode(user?.toJson()),
- "isLogin": isLogin,
- "isLogout": isLogout,
- "lang": locale.languageCode,
- "country": locale.countryCode
- };
- }
|