Skip to content
Snippets Groups Projects
duniter_indexer.dart 13.95 KiB
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
import 'package:gecko/models/queries_indexer.dart';
import 'package:gecko/models/wallet_data.dart';
import 'package:gecko/providers/cesium_plus.dart';
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:gecko/providers/wallet_options.dart';
import 'package:gecko/providers/wallets_profiles.dart';
import 'package:gecko/screens/wallet_view.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:provider/provider.dart';

class DuniterIndexer with ChangeNotifier {
  Map<String, String?> walletNameIndexer = {};
  String? fetchMoreCursor;
  Map? pageInfo;
  int nPage = 1;
  int nRepositories = 20;
  List? transBC;
  List listIndexerEndpoints = [];
  bool isLoadingIndexer = false;

  void reload() {
    notifyListeners();
  }

  Future<bool> checkIndexerEndpoint(String endpoint) async {
    isLoadingIndexer = true;
    notifyListeners();
    final _client = HttpClient();
    _client.connectionTimeout = const Duration(milliseconds: 4000);
    try {
      final request = await _client.postUrl(Uri.parse('$endpoint/v1/graphql'));
      final response = await request.close();
      if (response.statusCode != 200) {
        log.d('INDEXER IS OFFILINE');
        indexerEndpoint = '';
        isLoadingIndexer = false;
        notifyListeners();
        return false;
      } else {
        indexerEndpoint = endpoint;
        await configBox.put('indexerEndpoint', endpoint);
        // await configBox.put('customEndpoint', endpoint);
        isLoadingIndexer = false;
        notifyListeners();
        final cache = HiveStore();
        cache.reset();
        return true;
      }
    } catch (e) {
      log.d('INDEXER IS OFFILINE');
      indexerEndpoint = '';
      isLoadingIndexer = false;
      notifyListeners();
      return false;
    }
  }

  // Future checkIndexerEndpointBackground() async {
  //   final oldEndpoint = indexerEndpoint;
  //   while (true) {
  //     await Future.delayed(const Duration(seconds: 30));
  //     final isValid = await checkIndexerEndpoint(oldEndpoint);
  //     if (!isValid) {