Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix(canvas): request local network before connect to device #1647

Merged
merged 9 commits into from
May 15, 2024
14 changes: 14 additions & 0 deletions lib/screen/home/home_navigation_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import 'dart:async';
import 'dart:io';

import 'package:after_layout/after_layout.dart';
import 'package:autonomy_flutter/common/injector.dart';
Expand Down Expand Up @@ -42,6 +43,7 @@ import 'package:autonomy_flutter/util/au_icons.dart';
import 'package:autonomy_flutter/util/constants.dart';
import 'package:autonomy_flutter/util/dio_util.dart';
import 'package:autonomy_flutter/util/inapp_notifications.dart';
import 'package:autonomy_flutter/util/local_network_helper.dart';
import 'package:autonomy_flutter/util/log.dart';
import 'package:autonomy_flutter/util/style.dart';
import 'package:autonomy_flutter/util/ui_helper.dart';
Expand Down Expand Up @@ -318,6 +320,18 @@ class HomeNavigationPageState extends State<HomeNavigationPage>

unawaited(injector<CanvasClientService>().init());
unawaited(_syncArtist());

unawaited(_requestLocalNetworkPermission());
}

Future<void> _requestLocalNetworkPermission() async {
if (Platform.isIOS) {
final didRequest = _configurationService.didGetLocalNetworkPermission();
if (!didRequest) {
final result = await LocalNetworkHelper.requestLocalNetworkPermission();
await _configurationService.setDidGetLocalNetworkPermission(result);
}
}
}

Future<void> _syncArtist() async {
Expand Down
1 change: 0 additions & 1 deletion lib/service/canvas_client_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class CanvasClientService {
..device = (DeviceInfo()
..deviceId = _deviceId
..deviceName = _deviceName);

final response = await stub.connect(
request,
options: _callOptions,
Expand Down
15 changes: 15 additions & 0 deletions lib/service/configuration_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import 'package:uuid/uuid.dart';
//ignore_for_file: constant_identifier_names

abstract class ConfigurationService {
bool didGetLocalNetworkPermission();

Future<void> setDidGetLocalNetworkPermission(bool value);

Future<void> setRecordOwners(List<String> owners, {bool override = false});

List<String> getRecordOwners();
Expand Down Expand Up @@ -277,6 +281,8 @@ abstract class ConfigurationService {
}

class ConfigurationServiceImpl implements ConfigurationService {
static const String keyDidGetLocalNetWorkPermission =
'did_get_local_network_permission';
static const String keyRecordOwners = 'yoko_ono_record_owners';
static const String KEY_HAS_MERCHANDISE_SUPPORT_INDEX_ID =
'has_merchandise_support';
Expand Down Expand Up @@ -1221,6 +1227,15 @@ class ConfigurationServiceImpl implements ConfigurationService {
await _preferences.setStringList(keyRecordOwners, currentOwners.toList());
}
}

@override
bool didGetLocalNetworkPermission() =>
_preferences.getBool(keyDidGetLocalNetWorkPermission) ?? false;

@override
Future<void> setDidGetLocalNetworkPermission(bool value) async {
await _preferences.setBool(keyDidGetLocalNetWorkPermission, value);
}
}

enum ConflictAction {
Expand Down
21 changes: 21 additions & 0 deletions lib/util/local_network_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:io';

import 'package:autonomy_flutter/util/log.dart';
import 'package:network_info_plus/network_info_plus.dart';
phuocbitmark marked this conversation as resolved.
Show resolved Hide resolved
phuocbitmark marked this conversation as resolved.
Show resolved Hide resolved

class LocalNetworkHelper {
static Future<bool> requestLocalNetworkPermission() async {
bool isGranted = false;
try {
final wifiIp = await NetworkInfo().getWifiIP();
phuocbitmark marked this conversation as resolved.
Show resolved Hide resolved
phuocbitmark marked this conversation as resolved.
Show resolved Hide resolved
log.info('[LocalNetworkHelper] wifiIp: $wifiIp');
await Socket.connect(wifiIp, 80,
timeout: const Duration(milliseconds: 100));
isGranted = true;
} catch (e) {
log.info('[LocalNetworkHelper] requestLocalNetworkPermission Error: $e');
isGranted = false;
}
return isGranted;
}
}
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
network_info_plus:
dependency: "direct main"
description:
name: network_info_plus
sha256: "5bd4b86e28fed5ed4e6ac7764133c031dfb7d3f46aa2a81b46f55038aa78ecc0"
url: "https://pub.dev"
source: hosted
version: "5.0.3"
network_info_plus_platform_interface:
dependency: transitive
description:
name: network_info_plus_platform_interface
sha256: "2e193d61d3072ac17824638793d3b89c6d581ce90c11604f4ca87311b42f2706"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
phuocbitmark marked this conversation as resolved.
Show resolved Hide resolved
nft_collection:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ dependencies:
image_gallery_saver: ^2.0.3
hand_signature: ^3.0.1
flutter_pdfview: ^1.3.2
network_info_plus: ^5.0.3
dependency_overrides:
intl: 0.18.0

Expand Down
Loading