Skip to content

Commit

Permalink
chore: use devnet only on dev environments. Remove apiUrl from enviro…
Browse files Browse the repository at this point in the history
…nment state (#89)
  • Loading branch information
d-reader-luka authored May 6, 2024
1 parent ebca06b commit 3286213
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:d_reader_flutter/config/config.dart';
import 'package:d_reader_flutter/features/auction_house/domain/models/listing.dart';
import 'package:d_reader_flutter/features/auction_house/presentation/providers/auction_house_providers.dart';
import 'package:d_reader_flutter/shared/domain/models/either.dart';
import 'package:d_reader_flutter/shared/domain/models/pagination/pagination_args.dart';
import 'package:d_reader_flutter/shared/domain/models/pagination/pagination_notifier.dart';
import 'package:d_reader_flutter/shared/domain/models/pagination/pagination_state.dart';
import 'package:d_reader_flutter/shared/domain/providers/environment/environment_notifier.dart';
import 'package:d_reader_flutter/shared/domain/providers/socket_provider.dart';
import 'package:d_reader_flutter/shared/exceptions/exceptions.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -31,8 +31,7 @@ class ListingsPaginationNotifier

@override
void init({Function()? onInit}) {
final socket =
ref.read(socketProvider(ref.read(environmentProvider).apiUrl)).socket;
final socket = ref.read(socketProvider(Config.apiUrl)).socket;
socket.connect();
ref.onDispose(() {
socket.close();
Expand Down
2 changes: 1 addition & 1 deletion lib/features/nft/presentation/screens/nft_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ class NftDetails extends ConsumerWidget {
.showSnackBar(
const SnackBar(
content: Text(
"NFT address copied to clipboard",
"Token address copied to clipboard",
),
),
),
Expand Down
39 changes: 19 additions & 20 deletions lib/features/settings/presentation/screens/root.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:d_reader_flutter/config/config.dart';
import 'package:d_reader_flutter/constants/routes.dart';
import 'package:d_reader_flutter/features/user/domain/models/user.dart';
import 'package:d_reader_flutter/shared/domain/providers/environment/environment_notifier.dart';
import 'package:d_reader_flutter/shared/utils/url_utils.dart';
import 'package:d_reader_flutter/shared/utils/screen_navigation.dart';
Expand Down Expand Up @@ -83,25 +82,25 @@ class SettingsRootView extends StatelessWidget {
);
},
),
Consumer(
builder: (context, ref, child) {
final userRole = ref.watch(environmentProvider).user?.role;
return userRole == UserRole.tester.name ||
ref.watch(environmentProvider).apiUrl.contains('dev')
? SettingsCommonListTile(
leadingPath:
'${Config.settingsAssetsPath}/light/network.svg',
title: 'Change Network',
onTap: () {
nextScreenPush(
context: context,
path: RoutePath.changeNetwork,
);
},
)
: const SizedBox();
},
),
// Consumer(
// builder: (context, ref, child) {
// final userRole = ref.watch(environmentProvider).user?.role;
// return userRole == UserRole.tester.name ||
// ref.watch(environmentProvider).apiUrl.contains('dev')
// ? SettingsCommonListTile(
// leadingPath:
// '${Config.settingsAssetsPath}/light/network.svg',
// title: 'Change Network',
// onTap: () {
// nextScreenPush(
// context: context,
// path: RoutePath.changeNetwork,
// );
// },
// )
// : const SizedBox();
// },
// ),
SettingsCommonListTile(
leadingPath: '${Config.settingsAssetsPath}/light/bun_bun.svg',
title: 'About dReader',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ final registerWalletToSocketEvents = Provider(
(ref) {
final socket = ref
.read(
socketProvider(
ref.read(environmentProvider).apiUrl,
),
socketProvider(Config.apiUrl),
)
.socket;
ref.onDispose(() {
Expand Down
12 changes: 7 additions & 5 deletions lib/shared/domain/providers/dio_network_service_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:d_reader_flutter/config/config.dart';
import 'package:d_reader_flutter/features/authentication/presentation/providers/auth_providers.dart';
import 'package:d_reader_flutter/routing/router.dart';
import 'package:d_reader_flutter/shared/data/remote/dio_network_service.dart';
Expand All @@ -21,15 +22,14 @@ final networkServiceProvider = Provider<DioNetworkService>(
(ref) {
final Dio dio = Dio(
BaseOptions(
baseUrl: ref.watch(environmentProvider).apiUrl,
baseUrl: Config.apiUrl,
),
)..interceptors.addAll(
[
InterceptorsWrapper(
onRequest: (options, handler) {
if (!options.path.contains('google')) {
// Add the access token to the request header
// TODO add this at service level
options.headers['Authorization'] =
ref.watch(environmentProvider).jwtToken;
}
Expand All @@ -48,9 +48,11 @@ final networkServiceProvider = Provider<DioNetworkService>(
QueuedInterceptorsWrapper(
onError: (error, handler) async {
if (error.response?.statusCode == 401) {
final Dio refreshTokenDio = Dio(BaseOptions(
baseUrl: ref.watch(environmentProvider).apiUrl,
));
final Dio refreshTokenDio = Dio(
BaseOptions(
baseUrl: Config.apiUrl,
),
);
String? newAccessToken = await refreshTokenDio
.patch<String?>(
'/auth/user/refresh-token/${ref.read(environmentProvider).refreshToken}')
Expand Down
14 changes: 4 additions & 10 deletions lib/shared/domain/providers/environment/environment_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:d_reader_flutter/config/config.dart';
import 'package:d_reader_flutter/shared/data/local/local_store.dart';
import 'package:d_reader_flutter/features/user/domain/models/user.dart';
import 'package:d_reader_flutter/shared/domain/providers/environment/state/environment_state.dart';
import 'package:flutter/services.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:solana/solana.dart';
Expand All @@ -27,11 +28,9 @@ class Environment extends _$Environment {
EnvironmentState build() {
state = EnvironmentState.empty();
final localStore = LocalStore.instance;
String selectedNetwork = localStore.get(
'last-network',
defaultValue: null,
) ??
SolanaCluster.mainnet.value;
const bool isProd = appFlavor != null && appFlavor == 'prod';
final String selectedNetwork =
isProd ? SolanaCluster.mainnet.value : SolanaCluster.devnet.value;
final localStoreData = localStore.get(
selectedNetwork == SolanaCluster.mainnet.value
? 'prod-network'
Expand All @@ -42,7 +41,6 @@ class Environment extends _$Environment {
if (localStoreData == null) {
return EnvironmentState(
solanaCluster: selectedNetwork,
apiUrl: Config.apiUrl,
);
}

Expand All @@ -53,7 +51,6 @@ class Environment extends _$Environment {
? jsonDecode(networkData['walletAuthTokenMap'])
: null;
return state.copyWith(
apiUrl: networkData['apiUrl'],
authToken: networkData['authToken'],
jwtToken: networkData['jwtToken'],
refreshToken: networkData['refreshToken'],
Expand Down Expand Up @@ -82,7 +79,6 @@ class Environment extends _$Environment {
final localStore = LocalStore.instance;

state = state.copyWith(
apiUrl: input.apiUrl,
authToken: input.authToken,
jwtToken: input.jwtToken,
refreshToken: input.refreshToken,
Expand Down Expand Up @@ -121,7 +117,6 @@ class Environment extends _$Environment {
Ed25519HDPublicKey? publicKey,
}) {
state = state.copyWithNullables(
apiUrl: apiUrl,
solanaCluster: cluster,
publicKey: publicKey,
);
Expand All @@ -130,7 +125,6 @@ class Environment extends _$Environment {

void onLogout() {
state = state.copyWithNullables(
apiUrl: state.apiUrl,
solanaCluster: state.solanaCluster,
authToken: state.authToken,
walletAuthTokenMap: state.walletAuthTokenMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import 'package:solana/solana.dart';

class EnvironmentState {
final UserModel? user;
final String apiUrl, solanaCluster;
final String solanaCluster;
final String? authToken, jwtToken, refreshToken;
@Deprecated('walletAuthTokenMap should be used')
final Map<String, WalletData>? wallets;
final Map<String, String>? walletAuthTokenMap;
Ed25519HDPublicKey? publicKey;

EnvironmentState({
this.apiUrl = Config.apiUrl,
required this.solanaCluster,
this.user,
this.authToken,
Expand All @@ -31,7 +30,6 @@ class EnvironmentState {

EnvironmentState copyWith({
UserModel? user,
String? apiUrl,
String? authToken,
String? jwtToken,
String? refreshToken,
Expand All @@ -42,7 +40,6 @@ class EnvironmentState {
}) {
return EnvironmentState(
user: user ?? this.user,
apiUrl: apiUrl ?? this.apiUrl,
authToken: authToken ?? this.authToken,
jwtToken: jwtToken ?? this.jwtToken,
refreshToken: refreshToken ?? this.refreshToken,
Expand All @@ -55,7 +52,6 @@ class EnvironmentState {

EnvironmentState copyWithNullables({
UserModel? user,
required String apiUrl,
String? authToken,
String? jwtToken,
String? refreshToken,
Expand All @@ -66,7 +62,6 @@ class EnvironmentState {
}) {
return EnvironmentState(
user: user,
apiUrl: apiUrl,
authToken: authToken,
jwtToken: jwtToken,
refreshToken: refreshToken,
Expand All @@ -79,7 +74,6 @@ class EnvironmentState {

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['apiUrl'] = apiUrl;
data['authToken'] = authToken;
data['jwtToken'] = jwtToken;
data['refreshToken'] = refreshToken;
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/domain/providers/solana/solana_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class SolanaNotifier extends _$SolanaNotifier {
signer: publicKey,
authToken: result.authToken,
jwtToken: ref.read(environmentProvider).jwtToken ?? '',
apiUrl: ref.read(environmentProvider).apiUrl,
apiUrl: Config.apiUrl,
);
}
ref.invalidate(registerWalletToSocketEvents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ class SolanaTransactionNotifier extends _$SolanaTransactionNotifier {
AppException(
identifier: 'SolanaTransactionNotifier.list',
statusCode: 500,
message: 'Failed to list nft.',
message: 'Failed to list.',
),
);
}
Expand Down

0 comments on commit 3286213

Please sign in to comment.