Skip to content

Commit

Permalink
leave some comment
Browse files Browse the repository at this point in the history
finished import wallet by privateKey in HomePage
  • Loading branch information
Yii committed May 1, 2022
1 parent b30f8d9 commit 23858d7
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 129 deletions.
40 changes: 24 additions & 16 deletions lib/core/provider/shared_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';

Expand All @@ -8,7 +9,6 @@ import '../../pages/begin/data/models/wallet_info.dart';
import '../../pages/begin/data/states/begin_state.dart';
import '../../pages/begin/data/states/load_coin_state.dart';
import '../../pages/begin/data/states/load_nft_state.dart';
import '../../pages/begin/presentation/pages/home_page.dart';
import '../util/constant.dart';
import '../util/web3/abi/erc20.g.dart';
import '../util/web3/abi/stream_chicken_2.g.dart';
Expand Down Expand Up @@ -51,10 +51,13 @@ class WalletConnectedNotifier extends StateNotifier<List<WalletInfo>> {
Future<void> addWallet(WalletInfo walletInfo) async {
final bool haveSameWallet = state.where((element) => element.address == walletInfo.address).isNotEmpty;
if (haveSameWallet) {
Fluttertoast.showToast(msg: '此錢包已匯入過囉');
return;
}

// 檢查是不是已經有從Metamask連結過的錢包,有的話其他只能透過私鑰匯入
final bool alreadyHaveEthWallet = state.where((element) => element.isFromMetamask).isNotEmpty;
if (alreadyHaveEthWallet) {
if (alreadyHaveEthWallet && walletInfo.isFromMetamask) {
return;
}

Expand All @@ -65,7 +68,7 @@ class WalletConnectedNotifier extends StateNotifier<List<WalletInfo>> {
final double doubleAmount = etherAmount.getValueInUnit(EtherUnit.ether);
walletInfo.etherAmount = doubleAmount;

state.add(walletInfo);
state = [...state, walletInfo];
}

void removeWallet(String walletAddress) {
Expand All @@ -77,7 +80,7 @@ class WalletConnectedNotifier extends StateNotifier<List<WalletInfo>> {
}

/// 刷新錢包的貨幣資料
final loadCoinDataProvider = StateNotifierProvider<LoadCoinDataNotifier, LoadCoinState>((ref) {
final loadCoinDataProvider = StateNotifierProvider.autoDispose<LoadCoinDataNotifier, LoadCoinState>((ref) {
WalletConnectedNotifier walletConnectedNotifier = ref.watch(walletConnectedProvider.notifier);
WalletHelper walletHelper = ref.watch(walletHelperProvider);
String currentWalletAddress = ref.watch(currentWalletProvider)?.address ?? '';
Expand Down Expand Up @@ -126,7 +129,7 @@ class LoadCoinDataNotifier extends StateNotifier<LoadCoinState> {
}

/// 刷新錢包的NFT資料
final loadNFTDataProvider = StateNotifierProvider<LoadNFTDataNotifier, LoadNFTState>((ref) {
final loadNFTDataProvider = StateNotifierProvider.autoDispose<LoadNFTDataNotifier, LoadNFTState>((ref) {
WalletConnectedNotifier walletConnectedNotifier = ref.watch(walletConnectedProvider.notifier);
WalletHelper walletHelper = ref.watch(walletHelperProvider);
String currentWalletAddress = ref.watch(currentWalletProvider)?.address ?? '';
Expand Down Expand Up @@ -245,47 +248,52 @@ Provider<WalletHelper> walletHelperProvider = Provider<WalletHelper>((ref) {
return WalletHelper(ref: ref);
});

/// 匯入錢包、匯入PrivateKey
final importWalletProvider = StateNotifierProvider<ImportWalletNotifier, ConnectWalletState>(
(ref) {
final Web3Client client = ref.read(web3ClientProvider);
final WalletConnectedNotifier walletConnectedNotifier = ref.watch(walletConnectedProvider.notifier);
return ImportWalletNotifier(
client: client,
walletConnectedNotifier: walletConnectedNotifier,
);
},
);

class ImportWalletNotifier extends StateNotifier<ConnectWalletState> {
final Web3Client client;
final WalletConnectedNotifier walletConnectedNotifier;

bool isConnectWallet = false;

ImportWalletNotifier({
required this.client,
required this.walletConnectedNotifier,
}) : super(const ConnectWalletState.init());

void showHomePage(context) {
HomePage.show(context);
}

Future<void> importWallet(String privateKey) async {
Future<void> importWallet({required String privateKey}) async {
state = const ConnectWalletState.loading();

try {
final private = EthPrivateKey.fromHex(privateKey);
final address = await private.extractAddress();
final EthPrivateKey ethPrivateKey = EthPrivateKey.fromHex(privateKey);
final EthereumAddress address = await ethPrivateKey.extractAddress();
isConnectWallet = true;
print('Eth Address: $address');

final WalletInfo wallet = WalletInfo(
final WalletInfo walletInfo = WalletInfo(
address: address.toString(),
importMethod: WalletImportMethod.privateKey,
privateKey: privateKey,
);

state = ConnectWalletState.data(walletInfo: wallet);
walletConnectedNotifier.addWallet(walletInfo);
state = ConnectWalletState.data(walletInfo: walletInfo);
} catch (e) {
isConnectWallet = false;
state = const ConnectWalletState.error(msg: '無法匯入錢包,請再試一次');
const String msg = '無法匯入錢包,請再試一次';

Fluttertoast.showToast(msg: msg);
state = const ConnectWalletState.error(msg: msg);
state = const ConnectWalletState.init();
}
}
}
2 changes: 1 addition & 1 deletion lib/core/util/extension/riverpod_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension AsyncValueUI on AsyncValue {
// isLoading shorthand (AsyncLoading is a subclass of AsycValue)
bool get isLoading => this is AsyncLoading;

// show a snackbar on error only
// 在有錯誤狀態的時候觸發,顯示吐司條
void showMessageOnError() => whenOrNull(
error: (error, _) {
Fluttertoast.showToast(msg: error.toString());
Expand Down
22 changes: 14 additions & 8 deletions lib/pages/begin/domain/providers/begin_provider.dart
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../../../core/provider/shared_provider.dart';
import '../../../../core/util/web3/metamask_wallet_connect_helper.dart';
import '../../../../core/util/web3/wallet_helper.dart';
import '../../data/models/wallet_info.dart';
import '../../data/states/begin_state.dart';
import '../../presentation/pages/home_page.dart';

final StateNotifierProvider<ConnectWalletNotifier, ConnectWalletState> connectWalletProvider =
StateNotifierProvider<ConnectWalletNotifier, ConnectWalletState>(
(ref) {
final MetamaskWalletConnectHelper walletConnectHelper = ref.read(metamaskWalletConnectHelperProvider);
final WalletConnectedNotifier walletConnectedNotifier = ref.watch(walletConnectedProvider.notifier);
final WalletHelper walletHelper = ref.read(walletHelperProvider);

return ConnectWalletNotifier(
walletConnectHelper: walletConnectHelper,
walletConnectedNotifier: walletConnectedNotifier,
walletHelper: walletHelper,
);
},
);

class ConnectWalletNotifier extends StateNotifier<ConnectWalletState> {
final MetamaskWalletConnectHelper walletConnectHelper;
final WalletConnectedNotifier walletConnectedNotifier;
final WalletHelper walletHelper;

bool isConnectWallet = false;

ConnectWalletNotifier({
required this.walletConnectHelper,
required this.walletConnectedNotifier,
required this.walletHelper,
}) : super(const ConnectWalletState.init());

void showHomePage(context) {
HomePage.show(context);
}

Future<void> connectWallet() async {
state = const ConnectWalletState.loading();
isConnectWallet = await walletConnectHelper.initSession();
if (isConnectWallet) {
final String address = walletConnectHelper.getEthereumCredentials().getEthereumAddress().toString();
isConnectWallet = true;

final WalletInfo wallet = WalletInfo(
final WalletInfo walletInfo = WalletInfo(
address: address,
importMethod: WalletImportMethod.metamask,
);

state = ConnectWalletState.data(walletInfo: wallet);
walletConnectedNotifier.addWallet(walletInfo);
state = ConnectWalletState.data(walletInfo: walletInfo);
} else {
state = const ConnectWalletState.error(msg: '無法與錢包連結,請再試一次');
isConnectWallet = false;
const String msg = '無法與錢包連結,請再試一次';

Fluttertoast.showToast(msg: msg);
state = const ConnectWalletState.error(msg: msg);
state = const ConnectWalletState.init();
}
}
Expand Down
26 changes: 16 additions & 10 deletions lib/pages/begin/presentation/dialogs/add_wallet_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class AddWalletDialog extends StatelessWidget {
);
}

final TextEditingController privateKeyEditController = TextEditingController();

/// 匯入方式
final importMethodProvider = StateProvider<WalletImportMethod>((ref) {
return WalletImportMethod.privateKey;
});

final TextEditingController privateKeyEditController = TextEditingController();

/// 取得匯入方式的文字
String getImportMethodText(WalletImportMethod method) {
String text = '';
switch (method) {
Expand All @@ -41,8 +43,10 @@ class AddWalletDialog extends StatelessWidget {
return text;
}

void _onSelectType({required WidgetRef ref}) {}
/// 選擇匯入錢包的方式
void _onSelectImportMethod({required WidgetRef ref}) {}

/// 貼上複製到剪貼簿的文字
Future<void> _onPastePrivateKey() async {
ClipboardData? clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
String copiedtext = clipboardData?.text ?? '';
Expand All @@ -51,6 +55,13 @@ class AddWalletDialog extends StatelessWidget {
TextSelection.fromPosition(TextPosition(offset: privateKeyEditController.text.length));
}

/// 確認送出
void _onConfirm(BuildContext context) {
if (privateKeyEditController.text.isNotEmpty) {
Navigator.maybePop(context, privateKeyEditController.text);
}
}

@override
Widget build(BuildContext context) {
return Dialog(
Expand Down Expand Up @@ -90,7 +101,7 @@ class AddWalletDialog extends StatelessWidget {
final String text = getImportMethodText(method);

return CommonButton(
onPress: () => _onSelectType(ref: ref),
onPress: () => _onSelectImportMethod(ref: ref),
padding: const EdgeInsets.all(16.0),
color: CustomTheme.primaryColor,
child: Text(
Expand All @@ -104,7 +115,6 @@ class AddWalletDialog extends StatelessWidget {
],
),
const SizedBox(height: 36.0),

Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand All @@ -125,11 +135,7 @@ class AddWalletDialog extends StatelessWidget {
CommonTextField(textEditingController: privateKeyEditController),
const SizedBox(height: 24.0),
CommonButton(
onPress: () {
if (privateKeyEditController.text.isNotEmpty) {
Navigator.maybePop(context, privateKeyEditController.text);
}
},
onPress: () => _onConfirm(context),
padding: const EdgeInsets.symmetric(vertical: 20.0),
color: CustomTheme.secondColor,
child: const Text(
Expand Down
44 changes: 12 additions & 32 deletions lib/pages/begin/presentation/pages/begin_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:fluttertoast/fluttertoast.dart';

import '../../../../core/provider/shared_provider.dart';
import '../../../../core/util/theme.dart';
import '../../data/states/begin_state.dart';
import '../../domain/providers/begin_provider.dart';
import '../dialogs/add_wallet_dialog.dart';
import '../widgets/common_button.dart';
Expand All @@ -17,35 +16,23 @@ class BeginPage extends StatelessWidget {
required BuildContext context,
required WidgetRef ref,
}) {
if (ref.read(walletConnectedProvider.notifier).isWalletConnected ||
ref.read(importWalletProvider.notifier).isConnectWallet) {
// 檢查是否已有錢包連結了
if (ref.read(walletConnectedProvider.notifier).isWalletConnected) {
HomePage.show(context);
} else {
Fluttertoast.showToast(msg: '請先連結或匯入錢包');
}
}

void listenConnectWallet(WidgetRef ref) {
ref.listen<ConnectWalletState>(connectWalletProvider, (previous, next) {
if (next is ConnectWalletData) {
ref.read(walletConnectedProvider.notifier).addWallet(next.walletInfo);
} else if (next is ConnectWalletError) {
Fluttertoast.showToast(msg: next.msg);
}
});
}

void listenImportWallet(WidgetRef ref) {
ref.listen<ConnectWalletState>(
importWalletProvider,
(previous, next) {
if (next is ConnectWalletData) {
ref.read(walletConnectedProvider.notifier).addWallet(next.walletInfo);
} else if (next is ConnectWalletError) {
Fluttertoast.showToast(msg: next.msg);
}
},
);
/// 匯入錢包
Future<void> _importWallet({
required BuildContext context,
required WidgetRef ref,
}) async {
final privateKey = await AddWalletDialog.show(context) as String?;
if (privateKey != null) {
ref.read(importWalletProvider.notifier).importWallet(privateKey: privateKey);
}
}

@override
Expand Down Expand Up @@ -99,7 +86,6 @@ class BeginPage extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.end,
children: [
Consumer(builder: (context, ref, _) {
listenConnectWallet(ref);
return CommonButton(
onPress: () => ref.read(connectWalletProvider.notifier).connectWallet(),
color: const Color.fromRGBO(255, 255, 255, 1),
Expand Down Expand Up @@ -137,14 +123,8 @@ class BeginPage extends StatelessWidget {
}),
const SizedBox(height: 18),
Consumer(builder: (context, ref, _) {
listenImportWallet(ref);
return CommonButton(
onPress: () async {
final privateKey = await AddWalletDialog.show(context) as String?;
if (privateKey != null) {
ref.read(importWalletProvider.notifier).importWallet(privateKey);
}
},
onPress: () => _importWallet(context: context, ref: ref),
color: CustomTheme.secondColor,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
Expand Down
Loading

0 comments on commit 23858d7

Please sign in to comment.