From c5960f8376ed8368048aa011fc073945f799538f Mon Sep 17 00:00:00 2001
From: poka <poka@p2p.legal>
Date: Sat, 16 Jul 2022 21:22:12 +0200
Subject: [PATCH] can set custom indexer endpoint

---
 lib/providers/duniter_indexer.dart |  5 +++--
 lib/screens/settings.dart          | 25 ++++++++++++++++++-------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/lib/providers/duniter_indexer.dart b/lib/providers/duniter_indexer.dart
index 62aadbee..b3d079c8 100644
--- a/lib/providers/duniter_indexer.dart
+++ b/lib/providers/duniter_indexer.dart
@@ -86,10 +86,11 @@ class DuniterIndexer with ChangeNotifier {
     // _listEndpoints.shuffle();
 
     log.d(listIndexerEndpoints);
+    listIndexerEndpoints.add('Personnalisé');
 
     if (configBox.containsKey('customIndexer')) {
-      // return configBox.get('customIndexer');
-      listIndexerEndpoints.insert(0, configBox.get('customIndexer'));
+      return configBox.get('customIndexer');
+      // listIndexerEndpoints.insert(0, configBox.get('customIndexer'));
     }
 
     if (configBox.containsKey('indexerEndpoint')) {
diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart
index 80ff60ea..09e0e3db 100644
--- a/lib/screens/settings.dart
+++ b/lib/screens/settings.dart
@@ -213,7 +213,7 @@ class SettingsScreen extends StatelessWidget {
 
     String? selectedIndexerEndpoint;
     if (configBox.containsKey('customIndexer')) {
-      selectedIndexerEndpoint = '';
+      selectedIndexerEndpoint = 'Personnalisé';
     } else {
       selectedIndexerEndpoint = indexerEndpoint;
     }
@@ -222,7 +222,7 @@ class SettingsScreen extends StatelessWidget {
       selectedIndexerEndpoint = _indexer.listIndexerEndpoints[0];
     }
 
-    TextEditingController _endpointController = TextEditingController(
+    TextEditingController _indexerEndpointController = TextEditingController(
         text: configBox.containsKey('customIndexer')
             ? configBox.get('customIndexer')
             : 'https://');
@@ -274,10 +274,21 @@ class SettingsScreen extends StatelessWidget {
                           ),
                           onPressed: selectedIndexerEndpoint != indexerEndpoint
                               ? () async {
-                                  log.d(
-                                      'connection to indexer $selectedIndexerEndpoint');
-                                  await _indexer.checkIndexerEndpoint(
-                                      selectedIndexerEndpoint!);
+                                  final finalEndpoint =
+                                      selectedIndexerEndpoint == 'Personnalisé'
+                                          ? _indexerEndpointController.text
+                                          : selectedIndexerEndpoint!;
+
+                                  if (selectedIndexerEndpoint ==
+                                      'Personnalisé') {
+                                    configBox.put('customIndexer',
+                                        _indexerEndpointController.text);
+                                  } else {
+                                    configBox.delete('customIndexer');
+                                  }
+                                  log.d('connection to indexer $finalEndpoint');
+                                  await _indexer
+                                      .checkIndexerEndpoint(finalEndpoint);
                                 }
                               : null);
                     }),
@@ -293,7 +304,7 @@ class SettingsScreen extends StatelessWidget {
             width: 200,
             height: 50,
             child: TextField(
-              controller: _endpointController,
+              controller: _indexerEndpointController,
               autocorrect: false,
             ),
           ),
-- 
GitLab