more refactor

This commit is contained in:
Christien Rioux 2024-01-08 21:37:08 -05:00
parent ba4ef05a28
commit b83aa3a64b
39 changed files with 722 additions and 514 deletions

View file

@ -1,49 +0,0 @@
import 'package:awesome_extensions/awesome_extensions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../tools/tools.dart';
class ProfileWidget extends ConsumerWidget {
const ProfileWidget({
required this.name,
this.pronouns,
super.key,
});
final String name;
final String? pronouns;
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
final textTheme = theme.textTheme;
return DecoratedBox(
decoration: ShapeDecoration(
color: scale.primaryScale.border,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(16))),
child: Column(children: [
Text(
name,
style: textTheme.headlineSmall,
textAlign: TextAlign.left,
).paddingAll(4),
if (pronouns != null && pronouns!.isNotEmpty)
Text(pronouns!, style: textTheme.bodyMedium).paddingLTRB(4, 0, 4, 4),
]),
);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(StringProperty('name', name))
..add(StringProperty('pronouns', pronouns));
}
}

View file

@ -1,3 +0,0 @@
export 'local_account.dart';
export 'preferences.dart';
export 'user_login.dart';

View file

@ -1,42 +0,0 @@
import 'package:change_case/change_case.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'preferences.freezed.dart';
part 'preferences.g.dart';
// Lock preference changes how frequently the messenger locks its
// interface and requires the identitySecretKey to be entered (pin/password/etc)
@freezed
class LockPreference with _$LockPreference {
const factory LockPreference({
required int inactivityLockSecs,
required bool lockWhenSwitching,
required bool lockWithSystemLock,
}) = _LockPreference;
factory LockPreference.fromJson(dynamic json) =>
_$LockPreferenceFromJson(json as Map<String, dynamic>);
}
// Theme supports multiple translations
enum LanguagePreference {
englishUS;
factory LanguagePreference.fromJson(dynamic j) =>
LanguagePreference.values.byName((j as String).toCamelCase());
String toJson() => name.toPascalCase();
}
// Preferences are stored in a table locally and globally affect all
// accounts imported/added and the app in general
@freezed
class Preferences with _$Preferences {
const factory Preferences({
required ThemePreferences themePreferences,
required LanguagePreference language,
required LockPreference locking,
}) = _Preferences;
factory Preferences.fromJson(dynamic json) =>
_$PreferencesFromJson(json as Map<String, dynamic>);
}

View file

@ -1,396 +0,0 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'preferences.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
LockPreference _$LockPreferenceFromJson(Map<String, dynamic> json) {
return _LockPreference.fromJson(json);
}
/// @nodoc
mixin _$LockPreference {
int get inactivityLockSecs => throw _privateConstructorUsedError;
bool get lockWhenSwitching => throw _privateConstructorUsedError;
bool get lockWithSystemLock => throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$LockPreferenceCopyWith<LockPreference> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $LockPreferenceCopyWith<$Res> {
factory $LockPreferenceCopyWith(
LockPreference value, $Res Function(LockPreference) then) =
_$LockPreferenceCopyWithImpl<$Res, LockPreference>;
@useResult
$Res call(
{int inactivityLockSecs,
bool lockWhenSwitching,
bool lockWithSystemLock});
}
/// @nodoc
class _$LockPreferenceCopyWithImpl<$Res, $Val extends LockPreference>
implements $LockPreferenceCopyWith<$Res> {
_$LockPreferenceCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
@pragma('vm:prefer-inline')
@override
$Res call({
Object? inactivityLockSecs = null,
Object? lockWhenSwitching = null,
Object? lockWithSystemLock = null,
}) {
return _then(_value.copyWith(
inactivityLockSecs: null == inactivityLockSecs
? _value.inactivityLockSecs
: inactivityLockSecs // ignore: cast_nullable_to_non_nullable
as int,
lockWhenSwitching: null == lockWhenSwitching
? _value.lockWhenSwitching
: lockWhenSwitching // ignore: cast_nullable_to_non_nullable
as bool,
lockWithSystemLock: null == lockWithSystemLock
? _value.lockWithSystemLock
: lockWithSystemLock // ignore: cast_nullable_to_non_nullable
as bool,
) as $Val);
}
}
/// @nodoc
abstract class _$$LockPreferenceImplCopyWith<$Res>
implements $LockPreferenceCopyWith<$Res> {
factory _$$LockPreferenceImplCopyWith(_$LockPreferenceImpl value,
$Res Function(_$LockPreferenceImpl) then) =
__$$LockPreferenceImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{int inactivityLockSecs,
bool lockWhenSwitching,
bool lockWithSystemLock});
}
/// @nodoc
class __$$LockPreferenceImplCopyWithImpl<$Res>
extends _$LockPreferenceCopyWithImpl<$Res, _$LockPreferenceImpl>
implements _$$LockPreferenceImplCopyWith<$Res> {
__$$LockPreferenceImplCopyWithImpl(
_$LockPreferenceImpl _value, $Res Function(_$LockPreferenceImpl) _then)
: super(_value, _then);
@pragma('vm:prefer-inline')
@override
$Res call({
Object? inactivityLockSecs = null,
Object? lockWhenSwitching = null,
Object? lockWithSystemLock = null,
}) {
return _then(_$LockPreferenceImpl(
inactivityLockSecs: null == inactivityLockSecs
? _value.inactivityLockSecs
: inactivityLockSecs // ignore: cast_nullable_to_non_nullable
as int,
lockWhenSwitching: null == lockWhenSwitching
? _value.lockWhenSwitching
: lockWhenSwitching // ignore: cast_nullable_to_non_nullable
as bool,
lockWithSystemLock: null == lockWithSystemLock
? _value.lockWithSystemLock
: lockWithSystemLock // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}
/// @nodoc
@JsonSerializable()
class _$LockPreferenceImpl implements _LockPreference {
const _$LockPreferenceImpl(
{required this.inactivityLockSecs,
required this.lockWhenSwitching,
required this.lockWithSystemLock});
factory _$LockPreferenceImpl.fromJson(Map<String, dynamic> json) =>
_$$LockPreferenceImplFromJson(json);
@override
final int inactivityLockSecs;
@override
final bool lockWhenSwitching;
@override
final bool lockWithSystemLock;
@override
String toString() {
return 'LockPreference(inactivityLockSecs: $inactivityLockSecs, lockWhenSwitching: $lockWhenSwitching, lockWithSystemLock: $lockWithSystemLock)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$LockPreferenceImpl &&
(identical(other.inactivityLockSecs, inactivityLockSecs) ||
other.inactivityLockSecs == inactivityLockSecs) &&
(identical(other.lockWhenSwitching, lockWhenSwitching) ||
other.lockWhenSwitching == lockWhenSwitching) &&
(identical(other.lockWithSystemLock, lockWithSystemLock) ||
other.lockWithSystemLock == lockWithSystemLock));
}
@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(
runtimeType, inactivityLockSecs, lockWhenSwitching, lockWithSystemLock);
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$LockPreferenceImplCopyWith<_$LockPreferenceImpl> get copyWith =>
__$$LockPreferenceImplCopyWithImpl<_$LockPreferenceImpl>(
this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$LockPreferenceImplToJson(
this,
);
}
}
abstract class _LockPreference implements LockPreference {
const factory _LockPreference(
{required final int inactivityLockSecs,
required final bool lockWhenSwitching,
required final bool lockWithSystemLock}) = _$LockPreferenceImpl;
factory _LockPreference.fromJson(Map<String, dynamic> json) =
_$LockPreferenceImpl.fromJson;
@override
int get inactivityLockSecs;
@override
bool get lockWhenSwitching;
@override
bool get lockWithSystemLock;
@override
@JsonKey(ignore: true)
_$$LockPreferenceImplCopyWith<_$LockPreferenceImpl> get copyWith =>
throw _privateConstructorUsedError;
}
Preferences _$PreferencesFromJson(Map<String, dynamic> json) {
return _Preferences.fromJson(json);
}
/// @nodoc
mixin _$Preferences {
ThemePreferences get themePreferences => throw _privateConstructorUsedError;
LanguagePreference get language => throw _privateConstructorUsedError;
LockPreference get locking => throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$PreferencesCopyWith<Preferences> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $PreferencesCopyWith<$Res> {
factory $PreferencesCopyWith(
Preferences value, $Res Function(Preferences) then) =
_$PreferencesCopyWithImpl<$Res, Preferences>;
@useResult
$Res call(
{ThemePreferences themePreferences,
LanguagePreference language,
LockPreference locking});
$LockPreferenceCopyWith<$Res> get locking;
}
/// @nodoc
class _$PreferencesCopyWithImpl<$Res, $Val extends Preferences>
implements $PreferencesCopyWith<$Res> {
_$PreferencesCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
@pragma('vm:prefer-inline')
@override
$Res call({
Object? themePreferences = freezed,
Object? language = null,
Object? locking = null,
}) {
return _then(_value.copyWith(
themePreferences: freezed == themePreferences
? _value.themePreferences
: themePreferences // ignore: cast_nullable_to_non_nullable
as ThemePreferences,
language: null == language
? _value.language
: language // ignore: cast_nullable_to_non_nullable
as LanguagePreference,
locking: null == locking
? _value.locking
: locking // ignore: cast_nullable_to_non_nullable
as LockPreference,
) as $Val);
}
@override
@pragma('vm:prefer-inline')
$LockPreferenceCopyWith<$Res> get locking {
return $LockPreferenceCopyWith<$Res>(_value.locking, (value) {
return _then(_value.copyWith(locking: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$PreferencesImplCopyWith<$Res>
implements $PreferencesCopyWith<$Res> {
factory _$$PreferencesImplCopyWith(
_$PreferencesImpl value, $Res Function(_$PreferencesImpl) then) =
__$$PreferencesImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{ThemePreferences themePreferences,
LanguagePreference language,
LockPreference locking});
@override
$LockPreferenceCopyWith<$Res> get locking;
}
/// @nodoc
class __$$PreferencesImplCopyWithImpl<$Res>
extends _$PreferencesCopyWithImpl<$Res, _$PreferencesImpl>
implements _$$PreferencesImplCopyWith<$Res> {
__$$PreferencesImplCopyWithImpl(
_$PreferencesImpl _value, $Res Function(_$PreferencesImpl) _then)
: super(_value, _then);
@pragma('vm:prefer-inline')
@override
$Res call({
Object? themePreferences = freezed,
Object? language = null,
Object? locking = null,
}) {
return _then(_$PreferencesImpl(
themePreferences: freezed == themePreferences
? _value.themePreferences
: themePreferences // ignore: cast_nullable_to_non_nullable
as ThemePreferences,
language: null == language
? _value.language
: language // ignore: cast_nullable_to_non_nullable
as LanguagePreference,
locking: null == locking
? _value.locking
: locking // ignore: cast_nullable_to_non_nullable
as LockPreference,
));
}
}
/// @nodoc
@JsonSerializable()
class _$PreferencesImpl implements _Preferences {
const _$PreferencesImpl(
{required this.themePreferences,
required this.language,
required this.locking});
factory _$PreferencesImpl.fromJson(Map<String, dynamic> json) =>
_$$PreferencesImplFromJson(json);
@override
final ThemePreferences themePreferences;
@override
final LanguagePreference language;
@override
final LockPreference locking;
@override
String toString() {
return 'Preferences(themePreferences: $themePreferences, language: $language, locking: $locking)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$PreferencesImpl &&
const DeepCollectionEquality()
.equals(other.themePreferences, themePreferences) &&
(identical(other.language, language) ||
other.language == language) &&
(identical(other.locking, locking) || other.locking == locking));
}
@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(runtimeType,
const DeepCollectionEquality().hash(themePreferences), language, locking);
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$PreferencesImplCopyWith<_$PreferencesImpl> get copyWith =>
__$$PreferencesImplCopyWithImpl<_$PreferencesImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$PreferencesImplToJson(
this,
);
}
}
abstract class _Preferences implements Preferences {
const factory _Preferences(
{required final ThemePreferences themePreferences,
required final LanguagePreference language,
required final LockPreference locking}) = _$PreferencesImpl;
factory _Preferences.fromJson(Map<String, dynamic> json) =
_$PreferencesImpl.fromJson;
@override
ThemePreferences get themePreferences;
@override
LanguagePreference get language;
@override
LockPreference get locking;
@override
@JsonKey(ignore: true)
_$$PreferencesImplCopyWith<_$PreferencesImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View file

@ -1,147 +0,0 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../entities/local_account.dart';
import '../../entities/user_login.dart';
import '../../proto/proto.dart' as proto;
import '../../../packages/veilid_support/veilid_support.dart';
import '../../local_accounts/local_accounts.dart';
import 'logins.dart';
part 'account.g.dart';
enum AccountInfoStatus {
noAccount,
accountInvalid,
accountLocked,
accountReady,
}
@immutable
class AccountInfo {
const AccountInfo({
required this.status,
required this.active,
this.account,
});
final AccountInfoStatus status;
final bool active;
final proto.Account? account;
}
/// Get an account from the identity key and if it is logged in and we
/// have its secret available, return the account record contents
@riverpod
Future<AccountInfo> fetchAccountInfo(FetchAccountInfoRef ref,
{required TypedKey accountMasterRecordKey}) async {
// Get which local account we want to fetch the profile for
final localAccount = await ref.watch(
fetchLocalAccountProvider(accountMasterRecordKey: accountMasterRecordKey)
.future);
if (localAccount == null) {
// Local account does not exist
return const AccountInfo(
status: AccountInfoStatus.noAccount, active: false);
}
// See if we've logged into this account or if it is locked
final activeUserLogin = await ref.watch(loginsProvider.future
.select((value) async => (await value).activeUserLogin));
final active = activeUserLogin == accountMasterRecordKey;
final login = await ref.watch(
fetchLoginProvider(accountMasterRecordKey: accountMasterRecordKey)
.future);
if (login == null) {
// Account was locked
return AccountInfo(status: AccountInfoStatus.accountLocked, active: active);
}
xxx login should open this key and leave it open, logout should close it
// Pull the account DHT key, decode it and return it
final pool = await DHTRecordPool.instance();
final account = await (await pool.openOwned(
login.accountRecordInfo.accountRecord,
parent: localAccount.identityMaster.identityRecordKey))
.scope((accountRec) => accountRec.getProtobuf(proto.Account.fromBuffer));
if (account == null) {
// Account could not be read or decrypted from DHT
ref.invalidateSelf();
return AccountInfo(
status: AccountInfoStatus.accountInvalid, active: active);
}
// Got account, decrypted and decoded
return AccountInfo(
status: AccountInfoStatus.accountReady, active: active, account: account);
}
@immutable
class ActiveAccountInfo {
const ActiveAccountInfo({
required this.localAccount,
required this.userLogin,
required this.account,
});
//
KeyPair getConversationWriter() {
final identityKey = localAccount.identityMaster.identityPublicKey;
final identitySecret = userLogin.identitySecret;
return KeyPair(key: identityKey, secret: identitySecret.value);
}
//
final LocalAccount localAccount;
final UserLogin userLogin;
final proto.Account account;
}
/// Get the active account info
@riverpod
Future<ActiveAccountInfo?> fetchActiveAccountInfo(
FetchActiveAccountInfoRef ref) async {
// See if we've logged into this account or if it is locked
final activeUserLogin = await ref.watch(loginsProvider.future
.select((value) async => (await value).activeUserLogin));
if (activeUserLogin == null) {
return null;
}
// Get the user login
final userLogin = await ref.watch(
fetchLoginProvider(accountMasterRecordKey: activeUserLogin).future);
if (userLogin == null) {
// Account was locked
return null;
}
// Get which local account we want to fetch the profile for
final localAccount = await ref.watch(
fetchLocalAccountProvider(accountMasterRecordKey: activeUserLogin)
.future);
if (localAccount == null) {
// Local account does not exist
return null;
}
// Pull the account DHT key, decode it and return it
final pool = await DHTRecordPool.instance();
final account = await (await pool.openOwned(
userLogin.accountRecordInfo.accountRecord,
parent: localAccount.identityMaster.identityRecordKey))
.scope((accountRec) => accountRec.getProtobuf(proto.Account.fromBuffer));
if (account == null) {
ref.invalidateSelf();
return null;
}
// Got account, decrypted and decoded
return ActiveAccountInfo(
localAccount: localAccount,
userLogin: userLogin,
account: account,
);
}