cake_wallet/lib/buy/onramper/onramper_buy_provider.dart
Konstantin Ullrich d972363417
Add additional Buy Provider (#1071)
* CW-466 Add Buy Options Page

* CW-466 Add Buy Options

* CW-466 Add Default Buy Provider to Other Settings

* CW-466 Onramper is working from Buy Options

* CW-466 Onramper is working from Buy Options

* CW-466 Translation improvements

* CW-466 Add Onramper & Robinhood Logos

* CW-466 Implement Robinhood Flow

* CW-466 Fix Robinhood Flow

* CW-466 Add RH-Secrets

* CW-466 Have RH Translation in English only

* Add missing URI details

* CW-466 Implement default Buy Provider

* CW-466 Fix Padding Buy Provider Options

* CW-466 Fix Bitcoin and Litecoin Signatures

* CW-466 Fix Error Message

* CW-466 Resolve requested changes

* Add exception handler to robinhood API calls

* CW-466 Fix Theming

---------

Co-authored-by: Justin Ehrenhofer <justin.ehrenhofer@gmail.com>
Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
2023-09-14 22:14:49 +03:00

84 lines
2.9 KiB
Dart

import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/themes/extensions/cake_text_theme.dart';
import 'package:cake_wallet/utils/device_info.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class OnRamperBuyProvider {
OnRamperBuyProvider({required SettingsStore settingsStore, required WalletBase wallet})
: this._settingsStore = settingsStore,
this._wallet = wallet;
final SettingsStore _settingsStore;
final WalletBase _wallet;
static const _baseUrl = 'buy.onramper.com';
String get _apiKey => secrets.onramperApiKey;
String get _normalizeCryptoCurrency {
switch (_wallet.currency) {
case CryptoCurrency.ltc:
return "LTC_LITECOIN";
case CryptoCurrency.xmr:
return "XMR_MONERO";
default:
return _wallet.currency.title;
}
}
String getColorStr(Color color) {
return color.value.toRadixString(16).replaceAll(RegExp(r'^ff'), "");
}
Uri requestUrl(BuildContext context) {
String primaryColor,
secondaryColor,
primaryTextColor,
secondaryTextColor,
containerColor,
cardColor;
primaryColor = getColorStr(Theme.of(context).primaryColor);
secondaryColor = getColorStr(Theme.of(context).colorScheme.background);
primaryTextColor = getColorStr(Theme.of(context).extension<CakeTextTheme>()!.titleColor);
secondaryTextColor =
getColorStr(Theme.of(context).extension<CakeTextTheme>()!.secondaryTextColor);
containerColor = getColorStr(Theme.of(context).colorScheme.background);
cardColor = getColorStr(Theme.of(context).cardColor);
if (_settingsStore.currentTheme.title == S.current.high_contrast_theme) {
cardColor = getColorStr(Colors.white);
}
final networkName = _wallet.currency.fullName?.toUpperCase().replaceAll(" ", "");
return Uri.https(_baseUrl, '', <String, dynamic>{
'apiKey': _apiKey,
'defaultCrypto': _normalizeCryptoCurrency,
'networkWallets': '${networkName}:${_wallet.walletAddresses.address}',
'supportSell': "false",
'supportSwap': "false",
'primaryColor': primaryColor,
'secondaryColor': secondaryColor,
'primaryTextColor': primaryTextColor,
'secondaryTextColor': secondaryTextColor,
'containerColor': containerColor,
'cardColor': cardColor
});
}
Future<void> launchProvider(BuildContext context) async {
final uri = requestUrl(context);
if (DeviceInfo.instance.isMobile) {
Navigator.of(context).pushNamed(Routes.webViewPage, arguments: [S.of(context).buy, uri]);
} else {
await launchUrl(uri);
}
}
}