init
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
import '../../../core/constants/app_defaults.dart';
|
||||
|
||||
part 'user_profile.g.dart';
|
||||
|
||||
enum AvatarSource {
|
||||
asset,
|
||||
localFile,
|
||||
}
|
||||
|
||||
@collection
|
||||
class UserProfile {
|
||||
UserProfile();
|
||||
|
||||
Id id = AppDefaults.singletonId;
|
||||
|
||||
@Index(unique: true, replace: true)
|
||||
late String profileKey;
|
||||
|
||||
@Index(type: IndexType.value)
|
||||
late DateTime updatedAt;
|
||||
|
||||
late DateTime createdAt;
|
||||
late String nickname;
|
||||
String? avatarLocalPath;
|
||||
late String avatarAssetPath;
|
||||
|
||||
@enumerated
|
||||
AvatarSource avatarSource = AvatarSource.asset;
|
||||
|
||||
factory UserProfile.createDefault({DateTime? now, Random? random}) {
|
||||
final current = now ?? DateTime.now();
|
||||
final generator = random ?? Random();
|
||||
final suffix = AppDefaults.nicknameRandomMin +
|
||||
generator.nextInt(
|
||||
AppDefaults.nicknameRandomMax - AppDefaults.nicknameRandomMin + 1,
|
||||
);
|
||||
|
||||
return UserProfile()
|
||||
..profileKey = 'local_user_profile'
|
||||
..createdAt = current
|
||||
..updatedAt = current
|
||||
..nickname = '弓箭手_$suffix'
|
||||
..avatarAssetPath = AppDefaults.defaultAvatarAssetPath
|
||||
..avatarSource = AvatarSource.asset;
|
||||
}
|
||||
|
||||
String get displayAvatarPath =>
|
||||
avatarSource == AvatarSource.localFile &&
|
||||
avatarLocalPath != null &&
|
||||
avatarLocalPath!.trim().isNotEmpty
|
||||
? avatarLocalPath!
|
||||
: avatarAssetPath;
|
||||
|
||||
UserProfile copyWith({
|
||||
String? nickname,
|
||||
String? avatarLocalPath,
|
||||
String? avatarAssetPath,
|
||||
AvatarSource? avatarSource,
|
||||
DateTime? updatedAt,
|
||||
}) {
|
||||
return UserProfile()
|
||||
..id = id
|
||||
..profileKey = profileKey
|
||||
..createdAt = createdAt
|
||||
..updatedAt = updatedAt ?? DateTime.now()
|
||||
..nickname = nickname ?? this.nickname
|
||||
..avatarLocalPath = avatarLocalPath ?? this.avatarLocalPath
|
||||
..avatarAssetPath = avatarAssetPath ?? this.avatarAssetPath
|
||||
..avatarSource = avatarSource ?? this.avatarSource;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user