-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1824 from ardriveapp/PE-6623-navigate-to-drive-ex…
…plorer-before-wallet-balance-and-io-tokens-fetch-completion PE-6709: navigate to drive explorer before wallet balance and io tokens fetch completion
- Loading branch information
Showing
22 changed files
with
473 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:ardrive/authentication/ardrive_auth.dart'; | ||
import 'package:ardrive/user/user.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
part 'user_balance_event.dart'; | ||
part 'user_balance_state.dart'; | ||
|
||
class UserBalanceBloc extends Bloc<UserBalanceEvent, UserBalanceState> { | ||
final ArDriveAuth _auth; | ||
StreamSubscription<User?>? _userSubscription; | ||
|
||
UserBalanceBloc({required ArDriveAuth auth}) | ||
: _auth = auth, | ||
super(UserBalanceInitial()) { | ||
on<GetUserBalance>(_onGetUserBalance); | ||
on<RefreshUserBalance>(_onRefreshUserBalance); | ||
} | ||
|
||
Future<void> _onGetUserBalance( | ||
GetUserBalance event, Emitter<UserBalanceState> emit) async { | ||
await _cancelSubscription(); | ||
|
||
final user = _auth.currentUser; | ||
_emitUserBalanceState(user, emit); | ||
|
||
_userSubscription = _auth.onAuthStateChanged().listen((user) { | ||
if (isClosed) return; | ||
_emitUserBalanceState(user, emit); | ||
if (user?.ioTokens != null) { | ||
_cancelSubscription(); | ||
} | ||
}); | ||
|
||
await _userSubscription?.asFuture(); | ||
} | ||
|
||
Future<void> _onRefreshUserBalance( | ||
RefreshUserBalance event, Emitter<UserBalanceState> emit) async { | ||
if (isClosed) return; | ||
|
||
await _cancelSubscription(); | ||
|
||
emit(UserBalanceLoadingIOTokens( | ||
arBalance: _auth.currentUser.walletBalance, | ||
errorFetchingIOTokens: false, | ||
)); | ||
|
||
_auth.refreshBalance(); | ||
|
||
_userSubscription = _auth.onAuthStateChanged().listen((user) { | ||
if (isClosed) return; | ||
_emitUserBalanceState(user, emit); | ||
if (user?.ioTokens != null) { | ||
_cancelSubscription(); | ||
} | ||
}); | ||
|
||
await _userSubscription?.asFuture(); | ||
} | ||
|
||
Future<void> _cancelSubscription() async { | ||
if (_userSubscription != null) { | ||
await _userSubscription!.cancel(); | ||
_userSubscription = null; | ||
} | ||
} | ||
|
||
void _emitUserBalanceState(User? user, Emitter<UserBalanceState> emit) { | ||
if (user == null) return; | ||
|
||
if (user.ioTokens == null && !user.errorFetchingIOTokens) { | ||
emit(UserBalanceLoadingIOTokens( | ||
arBalance: user.walletBalance, | ||
errorFetchingIOTokens: user.errorFetchingIOTokens, | ||
)); | ||
} else { | ||
emit(UserBalanceLoaded( | ||
arBalance: user.walletBalance, | ||
ioTokens: user.ioTokens, | ||
errorFetchingIOTokens: user.errorFetchingIOTokens, | ||
)); | ||
} | ||
} | ||
|
||
@override | ||
Future<void> close() async { | ||
await _cancelSubscription(); | ||
return super.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
part of 'user_balance_bloc.dart'; | ||
|
||
sealed class UserBalanceEvent extends Equatable { | ||
const UserBalanceEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
final class GetUserBalance extends UserBalanceEvent {} | ||
|
||
final class RefreshUserBalance extends UserBalanceEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
part of 'user_balance_bloc.dart'; | ||
|
||
sealed class UserBalanceState extends Equatable { | ||
const UserBalanceState(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
final class UserBalanceInitial extends UserBalanceState {} | ||
|
||
final class UserBalanceLoaded extends UserBalanceState { | ||
final BigInt arBalance; | ||
final String? ioTokens; | ||
final bool errorFetchingIOTokens; | ||
|
||
const UserBalanceLoaded({ | ||
required this.arBalance, | ||
required this.ioTokens, | ||
required this.errorFetchingIOTokens, | ||
}); | ||
} | ||
|
||
final class UserBalanceLoadingIOTokens extends UserBalanceLoaded { | ||
const UserBalanceLoadingIOTokens({ | ||
required super.arBalance, | ||
super.ioTokens, | ||
super.errorFetchingIOTokens = false, | ||
}); | ||
} |
Oops, something went wrong.