feat(proj): init
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:please_pay_me/data/models/json.dart';
|
||||
|
||||
class AuthUser {
|
||||
const AuthUser({
|
||||
required this.userId,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
this.username,
|
||||
this.photoUrl,
|
||||
});
|
||||
|
||||
factory AuthUser.fromJson(Map<String, dynamic> json) {
|
||||
return AuthUser(
|
||||
userId: asInt(json['user_id']),
|
||||
firstName: asStringOrNull(json['first_name']),
|
||||
lastName: asStringOrNull(json['last_name']),
|
||||
username: asStringOrNull(json['username']),
|
||||
photoUrl: asStringOrNull(json['photo_url']),
|
||||
);
|
||||
}
|
||||
|
||||
static AuthUser? tryDecode(String? raw) {
|
||||
if (raw == null || raw.isEmpty) return null;
|
||||
try {
|
||||
return AuthUser.fromJson(asMap(jsonDecode(raw)));
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final int userId;
|
||||
final String? firstName;
|
||||
final String? lastName;
|
||||
final String? username;
|
||||
final String? photoUrl;
|
||||
|
||||
String get displayName {
|
||||
final full = [firstName, lastName].whereType<String>().join(' ').trim();
|
||||
if (full.isNotEmpty) return full;
|
||||
if (username != null) return '@$username';
|
||||
return 'Пользователь $userId';
|
||||
}
|
||||
|
||||
String get handle => username != null ? '@$username' : 'id $userId';
|
||||
|
||||
String get initials {
|
||||
final source = firstName?.trim().isNotEmpty == true
|
||||
? firstName!.trim()
|
||||
: username?.trim() ?? '';
|
||||
if (source.isEmpty) return '';
|
||||
return source.substring(0, 1);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'user_id': userId,
|
||||
'first_name': firstName,
|
||||
'last_name': lastName,
|
||||
'username': username,
|
||||
'photo_url': photoUrl,
|
||||
};
|
||||
|
||||
String encode() => jsonEncode(toJson());
|
||||
}
|
||||
|
||||
class AuthSession {
|
||||
const AuthSession({required this.accessToken, required this.user});
|
||||
|
||||
factory AuthSession.fromJson(Map<String, dynamic> json) {
|
||||
return AuthSession(
|
||||
accessToken: asString(json['access_token']),
|
||||
user: AuthUser.fromJson(asMap(json['user'])),
|
||||
);
|
||||
}
|
||||
|
||||
final String accessToken;
|
||||
final AuthUser user;
|
||||
}
|
||||
Reference in New Issue
Block a user