refactor(legacy_auth): migrate link_phone to Riverpod

This commit is contained in:
2026-04-22 23:09:20 +02:00
parent 44c8949c07
commit c84287e803
7 changed files with 210 additions and 145 deletions

View File

@@ -1,46 +1,28 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:legacy_auth/src/features/link_phone/presentation/providers/link_phone_state.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:legacy_auth/src/features/link_phone/presentation/state/link_phone_view_state.dart';
import 'package:sf_localizations/sf_localizations.dart'; import 'package:sf_localizations/sf_localizations.dart';
import 'package:sf_shared/sf_shared.dart'; import 'package:sf_shared/sf_shared.dart';
import 'package:sf_tracking/sf_tracking.dart'; import 'package:sf_tracking/sf_tracking.dart';
final legacyLinkPhoneViewModelProvider = part 'link_phone_controller.g.dart';
NotifierProvider.autoDispose<
LegacyLinkPhoneViewModel,
LegacyLinkPhoneViewState
>(LegacyLinkPhoneViewModel.new);
class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
late final SfTrackingRepository _tracking;
late final TextEditingController phoneNumberController;
late final TextEditingController codeController;
@Riverpod(keepAlive: true)
class LinkPhoneController extends _$LinkPhoneController {
@override @override
LegacyLinkPhoneViewState build() { LinkPhoneState build() => const LinkPhoneState();
_tracking = ref.read(sfTrackingProvider);
phoneNumberController = TextEditingController(); void setPhone(String value) {
phoneNumberController.addListener(_onPhoneNumberChanged); if (value == state.phoneNumber) return;
codeController = TextEditingController();
ref.onDispose(disposeControllers);
return const LegacyLinkPhoneViewState();
}
void _onPhoneNumberChanged() {
state = state.copyWith( state = state.copyWith(
phoneNumber: phoneNumberController.text, phoneNumber: value,
errorMessage: '', errorMessage: '',
codeVerified: false, codeVerified: false,
); );
} }
void updateCountry(String isoCode) { void setIsoCode(String isoCode) {
if (isoCode == state.isoCode) return; if (isoCode == state.isoCode) return;
state = state.copyWith( state = state.copyWith(
isoCode: isoCode, isoCode: isoCode,
@@ -49,9 +31,12 @@ class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
); );
} }
void updateCode(String code) { void setCode(String code) {
codeController.text = code; state = state.copyWith(
state = state.copyWith(errorMessage: '', codeVerified: false); code: code,
errorMessage: '',
codeVerified: false,
);
} }
SfPhoneNumber? _parsePhone() { SfPhoneNumber? _parsePhone() {
@@ -89,12 +74,13 @@ class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
codeVerified: false, codeVerified: false,
); );
final tracking = ref.read(sfTrackingProvider);
try { try {
// Stub: real call is ref.read(legacyAuthRepositoryProvider).requestPhoneCode(phone: parsed.e164) // Stub: real call is ref.read(legacyAuthRepositoryProvider)
// .requestPhoneCode(phone: parsed.e164)
await Future<void>.delayed(const Duration(milliseconds: 500)); await Future<void>.delayed(const Duration(milliseconds: 500));
if (!ref.mounted) return;
unawaited(_tracking.legacyAuthLinkPhoneCodeRequested()); unawaited(tracking.legacyAuthLinkPhoneCodeRequested());
state = state.copyWith( state = state.copyWith(
isLoading: false, isLoading: false,
@@ -102,10 +88,7 @@ class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
sentTo: parsed.format(), sentTo: parsed.format(),
); );
} catch (e) { } catch (e) {
if (!ref.mounted) return; unawaited(tracking.legacyAuthLinkPhoneCodeRequestFailed(e.toString()));
unawaited(_tracking.legacyAuthLinkPhoneCodeRequestFailed(e.toString()));
state = state.copyWith( state = state.copyWith(
isLoading: false, isLoading: false,
errorMessage: I18n.errorGeneric, errorMessage: I18n.errorGeneric,
@@ -119,7 +102,7 @@ class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
final parsed = _parsePhone(); final parsed = _parsePhone();
if (parsed == null) return; if (parsed == null) return;
final code = codeController.text.trim(); final code = state.code.trim();
if (code.isEmpty) { if (code.isEmpty) {
state = state.copyWith( state = state.copyWith(
errorMessage: I18n.errorMessageCodeIsEmpty, errorMessage: I18n.errorMessageCodeIsEmpty,
@@ -134,21 +117,19 @@ class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
codeVerified: false, codeVerified: false,
); );
final tracking = ref.read(sfTrackingProvider);
try { try {
// Stub: real call is ref.read(legacyAuthRepositoryProvider).verifyPhoneCode(phone: parsed.e164, code: code) // Stub: real call is ref.read(legacyAuthRepositoryProvider)
// .verifyPhoneCode(phone: parsed.e164, code: code)
await Future<void>.delayed(const Duration(milliseconds: 500)); await Future<void>.delayed(const Duration(milliseconds: 500));
if (!ref.mounted) return;
unawaited(_tracking.legacyAuthLinkPhoneCodeVerified()); unawaited(tracking.legacyAuthLinkPhoneCodeVerified());
state = state.copyWith(isLoading: false, codeVerified: true); state = state.copyWith(isLoading: false, codeVerified: true);
} catch (e) { } catch (e) {
if (!ref.mounted) return;
unawaited( unawaited(
_tracking.legacyAuthLinkPhoneCodeVerificationFailed(e.toString()), tracking.legacyAuthLinkPhoneCodeVerificationFailed(e.toString()),
); );
state = state.copyWith( state = state.copyWith(
isLoading: false, isLoading: false,
errorMessage: I18n.errorGeneric, errorMessage: I18n.errorGeneric,
@@ -156,10 +137,4 @@ class LegacyLinkPhoneViewModel extends Notifier<LegacyLinkPhoneViewState> {
); );
} }
} }
void disposeControllers() {
phoneNumberController.removeListener(_onPhoneNumberChanged);
phoneNumberController.dispose();
codeController.dispose();
}
} }

View File

@@ -0,0 +1,64 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'link_phone_controller.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
@ProviderFor(LinkPhoneController)
const linkPhoneControllerProvider = LinkPhoneControllerProvider._();
final class LinkPhoneControllerProvider
extends $NotifierProvider<LinkPhoneController, LinkPhoneState> {
const LinkPhoneControllerProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'linkPhoneControllerProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$linkPhoneControllerHash();
@$internal
@override
LinkPhoneController create() => LinkPhoneController();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(LinkPhoneState value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<LinkPhoneState>(value),
);
}
}
String _$linkPhoneControllerHash() =>
r'a405df66fc2723c4ef6271c440fdabca99625c7e';
abstract class _$LinkPhoneController extends $Notifier<LinkPhoneState> {
LinkPhoneState build();
@$mustCallSuper
@override
void runBuild() {
final created = build();
final ref = this.ref as $Ref<LinkPhoneState, LinkPhoneState>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<LinkPhoneState, LinkPhoneState>,
LinkPhoneState,
Object?,
Object?
>;
element.handleValue(ref, created);
}
}

View File

@@ -1,16 +1,17 @@
import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:freezed_annotation/freezed_annotation.dart';
part 'link_phone_view_state.freezed.dart'; part 'link_phone_state.freezed.dart';
@freezed @freezed
abstract class LegacyLinkPhoneViewState with _$LegacyLinkPhoneViewState { abstract class LinkPhoneState with _$LinkPhoneState {
const factory LegacyLinkPhoneViewState({ const factory LinkPhoneState({
@Default('') String phoneNumber, @Default('') String phoneNumber,
@Default('ES') String isoCode, @Default('ES') String isoCode,
@Default('') String code,
@Default('') String sentTo, @Default('') String sentTo,
@Default('') String errorMessage, @Default('') String errorMessage,
@Default(false) bool isLoading, @Default(false) bool isLoading,
@Default(false) bool codeRequested, @Default(false) bool codeRequested,
@Default(false) bool codeVerified, @Default(false) bool codeVerified,
}) = _LegacyLinkPhoneViewState; }) = _LinkPhoneState;
} }

View File

@@ -3,7 +3,7 @@
// ignore_for_file: type=lint // 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 // 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 'link_phone_view_state.dart'; part of 'link_phone_state.dart';
// ************************************************************************** // **************************************************************************
// FreezedGenerator // FreezedGenerator
@@ -12,40 +12,40 @@ part of 'link_phone_view_state.dart';
// dart format off // dart format off
T _$identity<T>(T value) => value; T _$identity<T>(T value) => value;
/// @nodoc /// @nodoc
mixin _$LegacyLinkPhoneViewState { mixin _$LinkPhoneState {
String get phoneNumber; String get isoCode; String get sentTo; String get errorMessage; bool get isLoading; bool get codeRequested; bool get codeVerified; String get phoneNumber; String get isoCode; String get code; String get sentTo; String get errorMessage; bool get isLoading; bool get codeRequested; bool get codeVerified;
/// Create a copy of LegacyLinkPhoneViewState /// Create a copy of LinkPhoneState
/// with the given fields replaced by the non-null parameter values. /// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false) @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline') @pragma('vm:prefer-inline')
$LegacyLinkPhoneViewStateCopyWith<LegacyLinkPhoneViewState> get copyWith => _$LegacyLinkPhoneViewStateCopyWithImpl<LegacyLinkPhoneViewState>(this as LegacyLinkPhoneViewState, _$identity); $LinkPhoneStateCopyWith<LinkPhoneState> get copyWith => _$LinkPhoneStateCopyWithImpl<LinkPhoneState>(this as LinkPhoneState, _$identity);
@override @override
bool operator ==(Object other) { bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is LegacyLinkPhoneViewState&&(identical(other.phoneNumber, phoneNumber) || other.phoneNumber == phoneNumber)&&(identical(other.isoCode, isoCode) || other.isoCode == isoCode)&&(identical(other.sentTo, sentTo) || other.sentTo == sentTo)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage)&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.codeRequested, codeRequested) || other.codeRequested == codeRequested)&&(identical(other.codeVerified, codeVerified) || other.codeVerified == codeVerified)); return identical(this, other) || (other.runtimeType == runtimeType&&other is LinkPhoneState&&(identical(other.phoneNumber, phoneNumber) || other.phoneNumber == phoneNumber)&&(identical(other.isoCode, isoCode) || other.isoCode == isoCode)&&(identical(other.code, code) || other.code == code)&&(identical(other.sentTo, sentTo) || other.sentTo == sentTo)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage)&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.codeRequested, codeRequested) || other.codeRequested == codeRequested)&&(identical(other.codeVerified, codeVerified) || other.codeVerified == codeVerified));
} }
@override @override
int get hashCode => Object.hash(runtimeType,phoneNumber,isoCode,sentTo,errorMessage,isLoading,codeRequested,codeVerified); int get hashCode => Object.hash(runtimeType,phoneNumber,isoCode,code,sentTo,errorMessage,isLoading,codeRequested,codeVerified);
@override @override
String toString() { String toString() {
return 'LegacyLinkPhoneViewState(phoneNumber: $phoneNumber, isoCode: $isoCode, sentTo: $sentTo, errorMessage: $errorMessage, isLoading: $isLoading, codeRequested: $codeRequested, codeVerified: $codeVerified)'; return 'LinkPhoneState(phoneNumber: $phoneNumber, isoCode: $isoCode, code: $code, sentTo: $sentTo, errorMessage: $errorMessage, isLoading: $isLoading, codeRequested: $codeRequested, codeVerified: $codeVerified)';
} }
} }
/// @nodoc /// @nodoc
abstract mixin class $LegacyLinkPhoneViewStateCopyWith<$Res> { abstract mixin class $LinkPhoneStateCopyWith<$Res> {
factory $LegacyLinkPhoneViewStateCopyWith(LegacyLinkPhoneViewState value, $Res Function(LegacyLinkPhoneViewState) _then) = _$LegacyLinkPhoneViewStateCopyWithImpl; factory $LinkPhoneStateCopyWith(LinkPhoneState value, $Res Function(LinkPhoneState) _then) = _$LinkPhoneStateCopyWithImpl;
@useResult @useResult
$Res call({ $Res call({
String phoneNumber, String isoCode, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified String phoneNumber, String isoCode, String code, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified
}); });
@@ -53,19 +53,20 @@ $Res call({
} }
/// @nodoc /// @nodoc
class _$LegacyLinkPhoneViewStateCopyWithImpl<$Res> class _$LinkPhoneStateCopyWithImpl<$Res>
implements $LegacyLinkPhoneViewStateCopyWith<$Res> { implements $LinkPhoneStateCopyWith<$Res> {
_$LegacyLinkPhoneViewStateCopyWithImpl(this._self, this._then); _$LinkPhoneStateCopyWithImpl(this._self, this._then);
final LegacyLinkPhoneViewState _self; final LinkPhoneState _self;
final $Res Function(LegacyLinkPhoneViewState) _then; final $Res Function(LinkPhoneState) _then;
/// Create a copy of LegacyLinkPhoneViewState /// Create a copy of LinkPhoneState
/// with the given fields replaced by the non-null parameter values. /// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? phoneNumber = null,Object? isoCode = null,Object? sentTo = null,Object? errorMessage = null,Object? isLoading = null,Object? codeRequested = null,Object? codeVerified = null,}) { @pragma('vm:prefer-inline') @override $Res call({Object? phoneNumber = null,Object? isoCode = null,Object? code = null,Object? sentTo = null,Object? errorMessage = null,Object? isLoading = null,Object? codeRequested = null,Object? codeVerified = null,}) {
return _then(_self.copyWith( return _then(_self.copyWith(
phoneNumber: null == phoneNumber ? _self.phoneNumber : phoneNumber // ignore: cast_nullable_to_non_nullable phoneNumber: null == phoneNumber ? _self.phoneNumber : phoneNumber // ignore: cast_nullable_to_non_nullable
as String,isoCode: null == isoCode ? _self.isoCode : isoCode // ignore: cast_nullable_to_non_nullable as String,isoCode: null == isoCode ? _self.isoCode : isoCode // ignore: cast_nullable_to_non_nullable
as String,code: null == code ? _self.code : code // ignore: cast_nullable_to_non_nullable
as String,sentTo: null == sentTo ? _self.sentTo : sentTo // ignore: cast_nullable_to_non_nullable as String,sentTo: null == sentTo ? _self.sentTo : sentTo // ignore: cast_nullable_to_non_nullable
as String,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable as String,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable
as String,isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable as String,isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable
@@ -78,8 +79,8 @@ as bool,
} }
/// Adds pattern-matching-related methods to [LegacyLinkPhoneViewState]. /// Adds pattern-matching-related methods to [LinkPhoneState].
extension LegacyLinkPhoneViewStatePatterns on LegacyLinkPhoneViewState { extension LinkPhoneStatePatterns on LinkPhoneState {
/// A variant of `map` that fallback to returning `orElse`. /// A variant of `map` that fallback to returning `orElse`.
/// ///
/// It is equivalent to doing: /// It is equivalent to doing:
@@ -92,10 +93,10 @@ extension LegacyLinkPhoneViewStatePatterns on LegacyLinkPhoneViewState {
/// } /// }
/// ``` /// ```
@optionalTypeArgs TResult maybeMap<TResult extends Object?>(TResult Function( _LegacyLinkPhoneViewState value)? $default,{required TResult orElse(),}){ @optionalTypeArgs TResult maybeMap<TResult extends Object?>(TResult Function( _LinkPhoneState value)? $default,{required TResult orElse(),}){
final _that = this; final _that = this;
switch (_that) { switch (_that) {
case _LegacyLinkPhoneViewState() when $default != null: case _LinkPhoneState() when $default != null:
return $default(_that);case _: return $default(_that);case _:
return orElse(); return orElse();
@@ -114,10 +115,10 @@ return $default(_that);case _:
/// } /// }
/// ``` /// ```
@optionalTypeArgs TResult map<TResult extends Object?>(TResult Function( _LegacyLinkPhoneViewState value) $default,){ @optionalTypeArgs TResult map<TResult extends Object?>(TResult Function( _LinkPhoneState value) $default,){
final _that = this; final _that = this;
switch (_that) { switch (_that) {
case _LegacyLinkPhoneViewState(): case _LinkPhoneState():
return $default(_that);case _: return $default(_that);case _:
throw StateError('Unexpected subclass'); throw StateError('Unexpected subclass');
@@ -135,10 +136,10 @@ return $default(_that);case _:
/// } /// }
/// ``` /// ```
@optionalTypeArgs TResult? mapOrNull<TResult extends Object?>(TResult? Function( _LegacyLinkPhoneViewState value)? $default,){ @optionalTypeArgs TResult? mapOrNull<TResult extends Object?>(TResult? Function( _LinkPhoneState value)? $default,){
final _that = this; final _that = this;
switch (_that) { switch (_that) {
case _LegacyLinkPhoneViewState() when $default != null: case _LinkPhoneState() when $default != null:
return $default(_that);case _: return $default(_that);case _:
return null; return null;
@@ -156,10 +157,10 @@ return $default(_that);case _:
/// } /// }
/// ``` /// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String phoneNumber, String isoCode, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified)? $default,{required TResult orElse(),}) {final _that = this; @optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String phoneNumber, String isoCode, String code, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) { switch (_that) {
case _LegacyLinkPhoneViewState() when $default != null: case _LinkPhoneState() when $default != null:
return $default(_that.phoneNumber,_that.isoCode,_that.sentTo,_that.errorMessage,_that.isLoading,_that.codeRequested,_that.codeVerified);case _: return $default(_that.phoneNumber,_that.isoCode,_that.code,_that.sentTo,_that.errorMessage,_that.isLoading,_that.codeRequested,_that.codeVerified);case _:
return orElse(); return orElse();
} }
@@ -177,10 +178,10 @@ return $default(_that.phoneNumber,_that.isoCode,_that.sentTo,_that.errorMessage,
/// } /// }
/// ``` /// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String phoneNumber, String isoCode, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified) $default,) {final _that = this; @optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String phoneNumber, String isoCode, String code, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified) $default,) {final _that = this;
switch (_that) { switch (_that) {
case _LegacyLinkPhoneViewState(): case _LinkPhoneState():
return $default(_that.phoneNumber,_that.isoCode,_that.sentTo,_that.errorMessage,_that.isLoading,_that.codeRequested,_that.codeVerified);case _: return $default(_that.phoneNumber,_that.isoCode,_that.code,_that.sentTo,_that.errorMessage,_that.isLoading,_that.codeRequested,_that.codeVerified);case _:
throw StateError('Unexpected subclass'); throw StateError('Unexpected subclass');
} }
@@ -197,10 +198,10 @@ return $default(_that.phoneNumber,_that.isoCode,_that.sentTo,_that.errorMessage,
/// } /// }
/// ``` /// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String phoneNumber, String isoCode, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified)? $default,) {final _that = this; @optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String phoneNumber, String isoCode, String code, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified)? $default,) {final _that = this;
switch (_that) { switch (_that) {
case _LegacyLinkPhoneViewState() when $default != null: case _LinkPhoneState() when $default != null:
return $default(_that.phoneNumber,_that.isoCode,_that.sentTo,_that.errorMessage,_that.isLoading,_that.codeRequested,_that.codeVerified);case _: return $default(_that.phoneNumber,_that.isoCode,_that.code,_that.sentTo,_that.errorMessage,_that.isLoading,_that.codeRequested,_that.codeVerified);case _:
return null; return null;
} }
@@ -211,49 +212,50 @@ return $default(_that.phoneNumber,_that.isoCode,_that.sentTo,_that.errorMessage,
/// @nodoc /// @nodoc
class _LegacyLinkPhoneViewState implements LegacyLinkPhoneViewState { class _LinkPhoneState implements LinkPhoneState {
const _LegacyLinkPhoneViewState({this.phoneNumber = '', this.isoCode = 'ES', this.sentTo = '', this.errorMessage = '', this.isLoading = false, this.codeRequested = false, this.codeVerified = false}); const _LinkPhoneState({this.phoneNumber = '', this.isoCode = 'ES', this.code = '', this.sentTo = '', this.errorMessage = '', this.isLoading = false, this.codeRequested = false, this.codeVerified = false});
@override@JsonKey() final String phoneNumber; @override@JsonKey() final String phoneNumber;
@override@JsonKey() final String isoCode; @override@JsonKey() final String isoCode;
@override@JsonKey() final String code;
@override@JsonKey() final String sentTo; @override@JsonKey() final String sentTo;
@override@JsonKey() final String errorMessage; @override@JsonKey() final String errorMessage;
@override@JsonKey() final bool isLoading; @override@JsonKey() final bool isLoading;
@override@JsonKey() final bool codeRequested; @override@JsonKey() final bool codeRequested;
@override@JsonKey() final bool codeVerified; @override@JsonKey() final bool codeVerified;
/// Create a copy of LegacyLinkPhoneViewState /// Create a copy of LinkPhoneState
/// with the given fields replaced by the non-null parameter values. /// with the given fields replaced by the non-null parameter values.
@override @JsonKey(includeFromJson: false, includeToJson: false) @override @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline') @pragma('vm:prefer-inline')
_$LegacyLinkPhoneViewStateCopyWith<_LegacyLinkPhoneViewState> get copyWith => __$LegacyLinkPhoneViewStateCopyWithImpl<_LegacyLinkPhoneViewState>(this, _$identity); _$LinkPhoneStateCopyWith<_LinkPhoneState> get copyWith => __$LinkPhoneStateCopyWithImpl<_LinkPhoneState>(this, _$identity);
@override @override
bool operator ==(Object other) { bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _LegacyLinkPhoneViewState&&(identical(other.phoneNumber, phoneNumber) || other.phoneNumber == phoneNumber)&&(identical(other.isoCode, isoCode) || other.isoCode == isoCode)&&(identical(other.sentTo, sentTo) || other.sentTo == sentTo)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage)&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.codeRequested, codeRequested) || other.codeRequested == codeRequested)&&(identical(other.codeVerified, codeVerified) || other.codeVerified == codeVerified)); return identical(this, other) || (other.runtimeType == runtimeType&&other is _LinkPhoneState&&(identical(other.phoneNumber, phoneNumber) || other.phoneNumber == phoneNumber)&&(identical(other.isoCode, isoCode) || other.isoCode == isoCode)&&(identical(other.code, code) || other.code == code)&&(identical(other.sentTo, sentTo) || other.sentTo == sentTo)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage)&&(identical(other.isLoading, isLoading) || other.isLoading == isLoading)&&(identical(other.codeRequested, codeRequested) || other.codeRequested == codeRequested)&&(identical(other.codeVerified, codeVerified) || other.codeVerified == codeVerified));
} }
@override @override
int get hashCode => Object.hash(runtimeType,phoneNumber,isoCode,sentTo,errorMessage,isLoading,codeRequested,codeVerified); int get hashCode => Object.hash(runtimeType,phoneNumber,isoCode,code,sentTo,errorMessage,isLoading,codeRequested,codeVerified);
@override @override
String toString() { String toString() {
return 'LegacyLinkPhoneViewState(phoneNumber: $phoneNumber, isoCode: $isoCode, sentTo: $sentTo, errorMessage: $errorMessage, isLoading: $isLoading, codeRequested: $codeRequested, codeVerified: $codeVerified)'; return 'LinkPhoneState(phoneNumber: $phoneNumber, isoCode: $isoCode, code: $code, sentTo: $sentTo, errorMessage: $errorMessage, isLoading: $isLoading, codeRequested: $codeRequested, codeVerified: $codeVerified)';
} }
} }
/// @nodoc /// @nodoc
abstract mixin class _$LegacyLinkPhoneViewStateCopyWith<$Res> implements $LegacyLinkPhoneViewStateCopyWith<$Res> { abstract mixin class _$LinkPhoneStateCopyWith<$Res> implements $LinkPhoneStateCopyWith<$Res> {
factory _$LegacyLinkPhoneViewStateCopyWith(_LegacyLinkPhoneViewState value, $Res Function(_LegacyLinkPhoneViewState) _then) = __$LegacyLinkPhoneViewStateCopyWithImpl; factory _$LinkPhoneStateCopyWith(_LinkPhoneState value, $Res Function(_LinkPhoneState) _then) = __$LinkPhoneStateCopyWithImpl;
@override @useResult @override @useResult
$Res call({ $Res call({
String phoneNumber, String isoCode, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified String phoneNumber, String isoCode, String code, String sentTo, String errorMessage, bool isLoading, bool codeRequested, bool codeVerified
}); });
@@ -261,19 +263,20 @@ $Res call({
} }
/// @nodoc /// @nodoc
class __$LegacyLinkPhoneViewStateCopyWithImpl<$Res> class __$LinkPhoneStateCopyWithImpl<$Res>
implements _$LegacyLinkPhoneViewStateCopyWith<$Res> { implements _$LinkPhoneStateCopyWith<$Res> {
__$LegacyLinkPhoneViewStateCopyWithImpl(this._self, this._then); __$LinkPhoneStateCopyWithImpl(this._self, this._then);
final _LegacyLinkPhoneViewState _self; final _LinkPhoneState _self;
final $Res Function(_LegacyLinkPhoneViewState) _then; final $Res Function(_LinkPhoneState) _then;
/// Create a copy of LegacyLinkPhoneViewState /// Create a copy of LinkPhoneState
/// with the given fields replaced by the non-null parameter values. /// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? phoneNumber = null,Object? isoCode = null,Object? sentTo = null,Object? errorMessage = null,Object? isLoading = null,Object? codeRequested = null,Object? codeVerified = null,}) { @override @pragma('vm:prefer-inline') $Res call({Object? phoneNumber = null,Object? isoCode = null,Object? code = null,Object? sentTo = null,Object? errorMessage = null,Object? isLoading = null,Object? codeRequested = null,Object? codeVerified = null,}) {
return _then(_LegacyLinkPhoneViewState( return _then(_LinkPhoneState(
phoneNumber: null == phoneNumber ? _self.phoneNumber : phoneNumber // ignore: cast_nullable_to_non_nullable phoneNumber: null == phoneNumber ? _self.phoneNumber : phoneNumber // ignore: cast_nullable_to_non_nullable
as String,isoCode: null == isoCode ? _self.isoCode : isoCode // ignore: cast_nullable_to_non_nullable as String,isoCode: null == isoCode ? _self.isoCode : isoCode // ignore: cast_nullable_to_non_nullable
as String,code: null == code ? _self.code : code // ignore: cast_nullable_to_non_nullable
as String,sentTo: null == sentTo ? _self.sentTo : sentTo // ignore: cast_nullable_to_non_nullable as String,sentTo: null == sentTo ? _self.sentTo : sentTo // ignore: cast_nullable_to_non_nullable
as String,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable as String,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable
as String,isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable as String,isLoading: null == isLoading ? _self.isLoading : isLoading // ignore: cast_nullable_to_non_nullable

View File

@@ -1,12 +1,12 @@
import 'package:legacy_auth/src/features/link_phone/presentation/state/link_phone_view_model.dart';
import 'package:legacy_theme/legacy_theme.dart';
import 'package:design_system/design_system.dart'; import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:legacy_auth/src/features/link_phone/presentation/providers/link_phone_controller.dart';
import 'package:legacy_theme/legacy_theme.dart';
import 'package:navigation/navigation.dart'; import 'package:navigation/navigation.dart';
import 'package:sf_localizations/sf_localizations.dart'; import 'package:sf_localizations/sf_localizations.dart';
class LegacyRequestLinkPhoneScreen extends ConsumerWidget { class LegacyRequestLinkPhoneScreen extends ConsumerStatefulWidget {
final NavigationContract navigationContract; final NavigationContract navigationContract;
const LegacyRequestLinkPhoneScreen({ const LegacyRequestLinkPhoneScreen({
@@ -15,10 +15,39 @@ class LegacyRequestLinkPhoneScreen extends ConsumerWidget {
}); });
@override @override
Widget build(BuildContext context, WidgetRef ref) { ConsumerState<LegacyRequestLinkPhoneScreen> createState() =>
_LegacyRequestLinkPhoneScreenState();
}
final viewModel = ref.read(legacyLinkPhoneViewModelProvider.notifier); class _LegacyRequestLinkPhoneScreenState
final viewState = ref.watch(legacyLinkPhoneViewModelProvider); extends ConsumerState<LegacyRequestLinkPhoneScreen> {
late final TextEditingController _phoneController;
@override
void initState() {
super.initState();
final initial = ref.read(linkPhoneControllerProvider).phoneNumber;
_phoneController = TextEditingController(text: initial);
_phoneController.addListener(_onPhoneChanged);
}
@override
void dispose() {
_phoneController.removeListener(_onPhoneChanged);
_phoneController.dispose();
super.dispose();
}
void _onPhoneChanged() {
ref.read(linkPhoneControllerProvider.notifier).setPhone(
_phoneController.text,
);
}
@override
Widget build(BuildContext context) {
final viewState = ref.watch(linkPhoneControllerProvider);
final notifier = ref.read(linkPhoneControllerProvider.notifier);
return Scaffold( return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface, backgroundColor: Theme.of(context).colorScheme.surface,
@@ -43,7 +72,6 @@ class LegacyRequestLinkPhoneScreen extends ConsumerWidget {
style: const TextStyle(fontSize: 16, letterSpacing: 0), style: const TextStyle(fontSize: 16, letterSpacing: 0),
), ),
const SizedBox(height: 48), const SizedBox(height: 48),
Column( Column(
spacing: 8, spacing: 8,
children: [ children: [
@@ -62,12 +90,12 @@ class LegacyRequestLinkPhoneScreen extends ConsumerWidget {
initialSelection: viewState.isoCode, initialSelection: viewState.isoCode,
onChanged: (country) { onChanged: (country) {
final code = country.code; final code = country.code;
if (code != null) viewModel.updateCountry(code); if (code != null) notifier.setIsoCode(code);
}, },
), ),
Expanded( Expanded(
child: CustomTextField( child: CustomTextField(
controller: viewModel.phoneNumberController, controller: _phoneController,
hint: context.translate(I18n.phoneNumber), hint: context.translate(I18n.phoneNumber),
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
), ),
@@ -76,9 +104,7 @@ class LegacyRequestLinkPhoneScreen extends ConsumerWidget {
), ),
], ],
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
if (viewState.errorMessage.isNotEmpty) ...[ if (viewState.errorMessage.isNotEmpty) ...[
const SizedBox(height: 4), const SizedBox(height: 4),
Text( Text(
@@ -90,15 +116,13 @@ class LegacyRequestLinkPhoneScreen extends ConsumerWidget {
), ),
), ),
], ],
const SizedBox(height: 24), const SizedBox(height: 24),
PrimaryButton( PrimaryButton(
onPressed: () async { onPressed: () async {
await viewModel.requestCode(); await notifier.requestCode();
final updatedState = ref.read(legacyLinkPhoneViewModelProvider); final updated = ref.read(linkPhoneControllerProvider);
if (updatedState.errorMessage.isEmpty) { if (updated.codeRequested) {
navigationContract.pushTo(AppRoutes.phoneCode); widget.navigationContract.pushTo(AppRoutes.phoneCode);
} }
}, },
text: context.translate(I18n.next), text: context.translate(I18n.next),

View File

@@ -1,10 +1,10 @@
import 'package:legacy_auth/src/features/link_phone/presentation/state/link_phone_view_model.dart';
import 'package:legacy_theme/legacy_theme.dart';
import 'package:legacy_auth/src/features/link_phone/presentation/widgets/link_phone_code_input.dart';
import 'package:design_system/design_system.dart'; import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:navigation/navigation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:legacy_auth/src/features/link_phone/presentation/providers/link_phone_controller.dart';
import 'package:legacy_auth/src/features/link_phone/presentation/widgets/link_phone_code_input.dart';
import 'package:legacy_theme/legacy_theme.dart';
import 'package:navigation/navigation.dart';
import 'package:sf_localizations/sf_localizations.dart'; import 'package:sf_localizations/sf_localizations.dart';
class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget { class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget {
@@ -17,9 +17,8 @@ class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget {
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final viewState = ref.watch(linkPhoneControllerProvider);
final viewModel = ref.read(legacyLinkPhoneViewModelProvider.notifier); final notifier = ref.read(linkPhoneControllerProvider.notifier);
final viewState = ref.watch(legacyLinkPhoneViewModelProvider);
return Scaffold( return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface, backgroundColor: Theme.of(context).colorScheme.surface,
@@ -34,7 +33,10 @@ class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget {
children: [ children: [
Text( Text(
context.translate(I18n.connect), context.translate(I18n.connect),
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30), style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
),
), ),
const SizedBox(height: 24), const SizedBox(height: 24),
Text.rich( Text.rich(
@@ -55,15 +57,13 @@ class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget {
const SizedBox(height: 48), const SizedBox(height: 48),
Text( Text(
context.translate(I18n.enterCodeHere), context.translate(I18n.enterCodeHere),
style: TextStyle(fontSize: 16), style: const TextStyle(fontSize: 16),
), ),
const SizedBox(height: 24), const SizedBox(height: 24),
LegacyLinkPhoneCodeInput( LegacyLinkPhoneCodeInput(
length: 6, length: 6,
onCodeChanged: viewModel.updateCode, onCodeChanged: notifier.setCode,
), ),
if (viewState.errorMessage.isNotEmpty) ...[ if (viewState.errorMessage.isNotEmpty) ...[
const SizedBox(height: 8), const SizedBox(height: 8),
Text( Text(
@@ -81,12 +81,9 @@ class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget {
children: [ children: [
PrimaryButton( PrimaryButton(
onPressed: () async { onPressed: () async {
await viewModel.verifyCode(); await notifier.verifyCode();
final updatedState = ref.read( final updated = ref.read(linkPhoneControllerProvider);
legacyLinkPhoneViewModelProvider, if (updated.codeVerified) {
);
if (updatedState.codeVerified) {
navigationContract.pushTo(AppRoutes.legacyLogin); navigationContract.pushTo(AppRoutes.legacyLogin);
} }
}, },
@@ -96,7 +93,7 @@ class LegacyVerifyLinkPhoneCodeScreen extends ConsumerWidget {
const SizedBox(height: 24), const SizedBox(height: 24),
Text( Text(
context.translate(I18n.didNotReceiveIt), context.translate(I18n.didNotReceiveIt),
style: TextStyle( style: const TextStyle(
fontSize: 18, fontSize: 18,
letterSpacing: 0, letterSpacing: 0,
height: 1.5, height: 1.5,

View File

@@ -38,6 +38,7 @@ dependencies:
get_it: ^9.0.5 get_it: ^9.0.5
go_router: ^17.0.0 go_router: ^17.0.0
flutter_riverpod: ^3.0.3 flutter_riverpod: ^3.0.3
riverpod_annotation: ^3.0.3
freezed_annotation: ^3.1.0 freezed_annotation: ^3.1.0
freezed: ^3.2.3 freezed: ^3.2.3
dio: ^5.9.2 dio: ^5.9.2