init method

  1. @override
Future<void> init()
override

Implementation

@override
Future<void> init() async {
  if (initialized.value) {
    return;
  }

  String? platform;
  switch (platformUtils.instance.getPlatformType()) {
    case PlatformType.desktop:
      platform = 'Desktop';
      break;
    case PlatformType.mobile:
      if (Platform.isIOS) {
        platform = 'iOS';
      } else if (Platform.isAndroid) {
        platform = 'Android';
      } else {
        platform = 'Mobile';
      }
      break;
    case PlatformType.web:
      platform = 'Injected';
      break;
    default:
      platform = null;
  }

  LoggerUtil.logger.i('Fetching wallet listings. Platform: $platform');
  _listings = await fetchListings(
    endpoint: '/w3m/v1/get${platform}Listings',
    referer: referer,
    // params: params,
  );

  if (excludedWalletState == ExcludedWalletState.list) {
    // If we are excluding all wallets, take out the excluded listings, if they exist
    if (excludedWalletIds != null) {
      _listings = filterExcludedListings(
        listings: _listings,
      );
    }
  } else if (excludedWalletState == ExcludedWalletState.all &&
      recommendedWalletIds != null) {
    // Filter down to only the included
    _listings = _listings
        .where(
          (listing) => recommendedWalletIds!.contains(
            listing.id,
          ),
        )
        .toList();
  } else {
    // If we are excluding all wallets and have no recommended wallets,
    // return an empty list
    _walletList = [];
    itemList.value = [];
    return;
  }
  _walletList.clear();

  for (Listing item in _listings) {
    bool installed = await urlUtils.instance.isInstalled(item.mobile.native);
    if (installed) {
      LoggerUtil.logger.i('Wallet ${item.name} installed: $installed');
    }
    _walletList.add(
      GridListItemModel<WalletData>(
        title: item.name,
        id: item.id,
        description: installed ? 'Installed' : null,
        image: getWalletImageUrl(
          imageId: item.imageId,
        ),
        data: WalletData(
          listing: item,
          installed: installed,
        ),
      ),
    );
  }

  // Sort the installed wallets to the top
  if (recommendedWalletIds != null) {
    _walletList.sort((a, b) {
      if ((a.data.installed && !b.data.installed) ||
          recommendedWalletIds!.contains(a.id)) {
        LoggerUtil.logger.i('Sorting ${a.title} to the top. ID: ${a.id}');
        return -1;
      } else if ((recommendedWalletIds!.contains(a.id) &&
              recommendedWalletIds!.contains(b.id)) ||
          (a.data.installed == b.data.installed)) {
        return 0;
      } else {
        return 1;
      }
    });
  } else {
    _walletList.sort((a, b) {
      if (a.data.installed && !b.data.installed) {
        LoggerUtil.logger.v('Sorting ${a.title} to the top. ID: ${a.id}');
        return -1;
      } else if (a.data.installed == b.data.installed) {
        return 0;
      } else {
        return 1;
      }
    });
  }

  itemList.value = _walletList;
  initialized.value = true;
}