CAKE-192 | parse_address_from_domain.dart fixed and split on 2 files (current and parse_address_from_domain_alert.dart), then moved to the entities directory; used one channel for native calls; removed unneeded spaces; fixed handle of unstoppable domain error in the MainActivity.java

This commit is contained in:
OleksandrSobol 2021-07-07 16:50:55 +03:00
parent bb219e4da2
commit f995342491
8 changed files with 97 additions and 100 deletions

View file

@ -21,7 +21,6 @@ import java.security.SecureRandom;
public class MainActivity extends FlutterFragmentActivity {
final String UTILS_CHANNEL = "com.cake_wallet/native_utils";
final String UNSTOPPABLE_DOMAIN_CHANNEL = "com.cakewallet.cake_wallet/unstoppable-domain";
final int UNSTOPPABLE_DOMAIN_MIN_VERSION_SDK = 24;
@Override
@ -33,12 +32,6 @@ public class MainActivity extends FlutterFragmentActivity {
UTILS_CHANNEL);
utilsChannel.setMethodCallHandler(this::handle);
MethodChannel unstoppableDomainChannel =
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),
UNSTOPPABLE_DOMAIN_CHANNEL);
unstoppableDomainChannel.setMethodCallHandler(this::handle);
}
private void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
@ -80,7 +73,8 @@ public class MainActivity extends FlutterFragmentActivity {
String address = resolution.getAddress(domain, ticker);
handler.post(() -> result.success(address));
} catch (Exception e) {
handler.post(() -> result.error("INVALID DOMAIN", e.getMessage(), null));
System.out.println("Expected Address, but got " + e.getMessage());
handler.post(() -> result.success(""));
}
});
}

View file

@ -5,7 +5,7 @@ import UnstoppableDomainsResolution
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
lazy var resolution : Resolution? = {
return try? Resolution()
return try? Resolution()
}()
override func application(
@ -75,45 +75,32 @@ import UnstoppableDomainsResolution
}
result(secRandom(count: count))
case "getUnstoppableDomainAddress":
guard let args = call.arguments as? Dictionary<String, String>,
let domain = args["domain"],
let ticker = args["ticker"],
let resolution = self?.resolution else {
result(nil)
return
}
resolution.addr(domain: domain, ticker: ticker) { addrResult in
var address : String = ""
switch addrResult {
case .success(let returnValue):
address = returnValue
case .failure(let error):
print("Expected Address, but got \(error)")
}
result(address)
}
default:
result(FlutterMethodNotImplemented)
}
})
let unstoppableDomainChannel = FlutterMethodChannel(name: "com.cakewallet.cake_wallet/unstoppable-domain", binaryMessenger: controller.binaryMessenger)
unstoppableDomainChannel.setMethodCallHandler({ [weak self]
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
switch call.method {
case "getUnstoppableDomainAddress":
guard let args = call.arguments as? Dictionary<String, String>,
let domain = args["domain"],
let ticker = args["ticker"] else {
result(nil)
return
}
guard let resolution = self?.resolution else {
result(nil)
return
}
resolution.addr(domain: domain, ticker: ticker) { addrResult in
var address : String = ""
switch addrResult {
case .success(let returnValue):
address = returnValue
case .failure(let error):
print("Expected Address, but got \(error)")
}
result(address)
}
default:
result(FlutterMethodNotImplemented)
}
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

View file

@ -0,0 +1,52 @@
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
import 'package:cake_wallet/src/screens/send/widgets/parse_address_from_domain_alert.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/generated/i18n.dart';
const topLevelDomain = 'crypto';
Future<String> parseAddressFromDomain(
BuildContext context, String domain, String ticker) async {
try {
final domainParts = domain.split('.');
final name = domainParts.last;
if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) {
return domain;
}
if (name.contains(topLevelDomain)) {
final address = await fetchUnstoppableDomainAddress(domain, ticker);
if (address?.isEmpty ?? true) {
return domain;
}
showAddressAlert(
context,
S.of(context).address_detected,
S.of(context).address_from_domain(domain));
return address;
}
final record = await OpenaliasRecord.fetchAddressAndName(
OpenaliasRecord.formatDomainName(domain));
if (record == null || record.address.contains(domain)) {
return domain;
}
showAddressAlert(
context,
S.of(context).openalias_alert_title,
S.of(context).openalias_alert_content(domain));
return record.address;
} catch (e) {
print(e.toString());
}
return domain;
}

View file

@ -1,9 +1,9 @@
import 'package:flutter/services.dart';
const channel = MethodChannel('com.cakewallet.cake_wallet/unstoppable-domain');
const channel = MethodChannel('com.cake_wallet/native_utils');
Future<String> fetchUnstoppableDomainAddress(String domain, String ticker) async {
String address;
var address = '';
try {
address = await channel.invokeMethod(

View file

@ -1,7 +1,7 @@
import 'dart:ui';
import 'package:cake_wallet/entities/sync_status.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:cake_wallet/src/screens/send/parse_address_from_domain.dart';
import 'package:cake_wallet/entities/parse_address_from_domain.dart';
import 'package:cake_wallet/src/widgets/standard_checkbox.dart';
import 'package:dotted_border/dotted_border.dart';
import 'package:flutter/cupertino.dart';

View file

@ -1,53 +0,0 @@
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/generated/i18n.dart';
const topLevelDomain = 'crypto';
Future<String> parseAddressFromDomain(
BuildContext context, String domain, String ticker) async {
try {
final name = domain.split('.').last;
if (name.contains(topLevelDomain)) {
final address = await fetchUnstoppableDomainAddress(domain, ticker);
if (address.isNotEmpty) {
showAddressAlert(
context,
S.of(context).address_detected,
S.of(context).address_from_domain(domain));
return address;
}
} else if (name.isNotEmpty) {
final record = await OpenaliasRecord.fetchAddressAndName(
OpenaliasRecord.formatDomainName(domain));
if (record.name != null && record.name != domain) {
showAddressAlert(
context,
S.of(context).openalias_alert_title,
S.of(context).openalias_alert_content(domain));
return record.address;
}
}
} catch (e) {
print(e.toString());
}
return domain;
}
void showAddressAlert(BuildContext context, String title, String content) async {
await showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: title,
alertContent: content,
buttonText: S.of(context).ok,
buttonAction: () => Navigator.of(context).pop());
});
}

View file

@ -1,6 +1,6 @@
import 'dart:ui';
import 'package:cake_wallet/entities/transaction_priority.dart';
import 'package:cake_wallet/src/screens/send/parse_address_from_domain.dart';
import 'package:cake_wallet/entities/parse_address_from_domain.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/src/widgets/keyboard_done_button.dart';
import 'package:cake_wallet/src/widgets/picker.dart';

View file

@ -0,0 +1,17 @@
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/generated/i18n.dart';
void showAddressAlert(BuildContext context, String title, String content) async {
await showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: title,
alertContent: content,
buttonText: S.of(context).ok,
buttonAction: () => Navigator.of(context).pop());
});
}