Changed test server connection

This commit is contained in:
Juan Gilsanz Polo
2024-02-26 22:42:13 +01:00
parent 75d025fdfc
commit 4bac98e573
12 changed files with 106 additions and 17 deletions

View File

@@ -172,7 +172,7 @@ final bookmarksRequestLoadMoreProvider =
);
typedef BookmarksRequestLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$bookmarksHash() => r'71282b9487a2e9d4a17973af7fb3da7c0171f8d0';
String _$bookmarksHash() => r'a2bd6f3226b5a1b9c19b536b31122fb5b449ff6a';
/// See also [Bookmarks].
@ProviderFor(Bookmarks)

View File

@@ -174,7 +174,7 @@ final fetchSearchBookmarksLoadMoreProvider =
);
typedef FetchSearchBookmarksLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$searchBookmarksHash() => r'6e8eea2bf9c19b33f626e64ae74210d9b374065f';
String _$searchBookmarksHash() => r'5a5bc02d87a3eff3976a3bc6991c2208f4232fb4';
/// See also [SearchBookmarks].
@ProviderFor(SearchBookmarks)

View File

@@ -123,12 +123,12 @@ class _ModalContent extends ConsumerWidget {
style: ButtonStyle(
foregroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded
? const MaterialStatePropertyAll(Colors.green)
: provider.checkBookmarkLoadStatus == LoadStatus.loaded
: provider.checkBookmarkLoadStatus == LoadStatus.error
? const MaterialStatePropertyAll(Colors.red)
: null,
backgroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded
? MaterialStatePropertyAll(Colors.green.withOpacity(0.15))
: provider.checkBookmarkLoadStatus == LoadStatus.loaded
: provider.checkBookmarkLoadStatus == LoadStatus.error
? MaterialStatePropertyAll(Colors.red.withOpacity(0.15))
: null,
),

View File

@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:linkdy/constants/enums.dart';
class ConnectModel {
bool isConnecting;
int method;
@@ -12,6 +14,7 @@ class ConnectModel {
final TextEditingController tokenController;
String? tokenError;
bool validValues;
LoadStatus? testConnection;
ConnectModel({
this.isConnecting = false,
@@ -25,5 +28,6 @@ class ConnectModel {
required this.tokenController,
this.tokenError,
this.validValues = true,
this.testConnection,
});
}

View File

@@ -19,6 +19,8 @@ import 'package:linkdy/constants/regexp.dart';
part 'connect.provider.g.dart';
const uuid = Uuid();
@riverpod
class Connect extends _$Connect {
@override
@@ -39,11 +41,13 @@ class Connect extends _$Connect {
}
void setConnectionMethod(int method) {
state.testConnection = null;
state.method = method;
ref.notifyListeners();
}
void validateIpDomain(String value) {
state.testConnection = null;
if (Regexps.ipAddress.hasMatch(value) || Regexps.domain.hasMatch(value)) {
state.ipDomainError = null;
} else {
@@ -54,6 +58,7 @@ class Connect extends _$Connect {
}
void validatePort(String value) {
state.testConnection = null;
if (value != '') {
if (int.tryParse(value) != null && int.parse(value) <= 65535) {
state.portError = null;
@@ -66,6 +71,7 @@ class Connect extends _$Connect {
}
void validatePath(String value) {
state.testConnection = null;
if (value == "") {
state.pathError = null;
} else {
@@ -80,6 +86,7 @@ class Connect extends _$Connect {
}
void validateToken(String value) {
state.testConnection = null;
if (value != "") {
state.tokenError = null;
} else {
@@ -97,17 +104,32 @@ class Connect extends _$Connect {
state.pathError == null &&
state.portError == null;
}
void testConnection() async {
state.testConnection = LoadStatus.loading;
ref.notifyListeners();
final connectionString =
"${ConnectionMethod.values[state.method].name}://${state.ipDomainController.text}${state.portController.text != '' ? ':${state.portController.text}' : ""}${state.pathController.text}";
final result = await testServerReachability(connectionString);
if (result == true) {
state.testConnection = LoadStatus.loaded;
} else {
state.testConnection = LoadStatus.error;
}
ref.notifyListeners();
}
}
@riverpod
FutureOr<bool> connectToServer(ConnectToServerRef ref) async {
const uuid = Uuid();
final serverInstance = ServerInstance(
id: uuid.v4(),
method: ConnectionMethod.values[ref.watch(connectProvider).method].name,
ipDomain: ref.watch(connectProvider).ipDomainController.text,
token: ref.watch(connectProvider).tokenController.text,
path: ref.watch(connectProvider).pathController.text,
);
final apiClient = ApiClientService(

View File

@@ -6,7 +6,7 @@ part of 'connect.provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$connectToServerHash() => r'370f2b67b9d20818d94e4d9b6ffb57c44aa18494';
String _$connectToServerHash() => r'a5225cddee0824ab2e6393da9d5c305e212bbfba';
/// See also [connectToServer].
@ProviderFor(connectToServer)
@@ -21,7 +21,7 @@ final connectToServerProvider = AutoDisposeFutureProvider<bool>.internal(
);
typedef ConnectToServerRef = AutoDisposeFutureProviderRef<bool>;
String _$connectHash() => r'576283d8c543cdb4b1ae22c99cc63a23e4ff2981';
String _$connectHash() => r'5382c9c95131bf263662799f30d2c131a548a8c5';
/// See also [Connect].
@ProviderFor(Connect)

View File

@@ -7,12 +7,11 @@ import 'package:linkdy/screens/onboarding/provider/connect.provider.dart';
import 'package:linkdy/screens/onboarding/provider/onboarding.provider.dart';
import 'package:linkdy/widgets/section_label.dart';
import 'package:linkdy/utils/open_url.dart';
import 'package:linkdy/constants/enums.dart';
import 'package:linkdy/i18n/strings.g.dart';
class OnboardingConnect extends ConsumerWidget {
const OnboardingConnect({Key? key}) : super(key: key);
const OnboardingConnect({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
@@ -205,10 +204,47 @@ class _ConnectForm extends ConsumerWidget {
obscureText: true,
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: ref.watch(connectProvider).validValues == true ? () => openUrl(connectionString) : null,
icon: const Icon(Icons.open_in_browser_rounded),
label: Text(t.onboarding.testConnectionUrl),
ElevatedButton(
onPressed: provider.testConnection == null ? () => ref.read(connectProvider.notifier).testConnection() : null,
style: ButtonStyle(
foregroundColor: provider.testConnection == LoadStatus.loaded
? const MaterialStatePropertyAll(Colors.green)
: provider.testConnection == LoadStatus.error
? const MaterialStatePropertyAll(Colors.red)
: null,
backgroundColor: provider.testConnection == LoadStatus.loaded
? MaterialStatePropertyAll(Colors.green.withOpacity(0.15))
: provider.testConnection == LoadStatus.error
? MaterialStatePropertyAll(Colors.red.withOpacity(0.15))
: null,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (provider.testConnection == null) Text(t.bookmarks.addBookmark.validateUrl),
if (provider.testConnection == LoadStatus.loading) ...[
const SizedBox(
width: 12,
height: 12,
child: CircularProgressIndicator(
strokeWidth: 2,
),
),
const SizedBox(width: 8),
Text(t.onboarding.testingConnection),
],
if (provider.testConnection == LoadStatus.loaded) ...[
const Icon(Icons.check_circle_rounded),
const SizedBox(width: 8),
Text(t.onboarding.connectionServerEstablished),
],
if (provider.testConnection == LoadStatus.error) ...[
const Icon(Icons.error_rounded),
const SizedBox(width: 8),
Text(t.onboarding.connectionServerFailed),
],
],
),
),
const SizedBox(height: 16),
],

View File

@@ -206,7 +206,7 @@ final tagBookmarksRequestLoadMoreProvider =
);
typedef TagBookmarksRequestLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$tagBookmarksHash() => r'ba10c98bdd9e359e885ee8edf56b99cdfcf2eb10';
String _$tagBookmarksHash() => r'5a183019775e04aa10f28c1492993f09445867a7';
/// See also [TagBookmarks].
@ProviderFor(TagBookmarks)