api_utils.dart 2.31 KB
import 'dart:async';
import 'dart:collection';
import 'package:connectycube_sdk/connectycube_sdk.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:universal_io/io.dart';

import 'platform_utils.dart';

void showDialogError(exception, context) {
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("Error"),
          content: Text("Something went wrong $exception"),
          actions: <Widget>[
            TextButton(
              child: const Text("OK"),
              onPressed: () => Navigator.of(context).pop(),
            )
          ],
        );
      });
}

void showDialogMsg(msg, context) {
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("Alert"),
          content: Text(msg),
          actions: <Widget>[
            TextButton(
              child: const Text("OK"),
              onPressed: () => Navigator.of(context).pop(),
            )
          ],
        );
      });
}

class ListItem<T> {
  bool isSelected = false; //Selection property to highlight or not
  T data; //Data of the user
  ListItem(this.data); //Constructor to assign the data
}

Future<Map<int, CubeUser>> getUsersByIds(Set<int> ids) async {
  Completer<Map<int, CubeUser>> completer = Completer();
  Map<int, CubeUser> users = HashMap();
  try {
    var result = await (getAllUsersByIds(ids)
        as FutureOr<PagedResult<CubeUser>>);
    users.addAll(Map.fromIterable(result.items,
        key: (item) => item.id, value: (item) => item));
  } catch (ex) {
    log("exception= $ex");
  }
  completer.complete(users);
  return completer.future;
}


Future<CubeFile> getUploadingImageFuture(FilePickerResult result) async {
  if(kIsWeb){
     return uploadRawFile(result.files.single.bytes!, result.files.single.name, isPublic: true, onProgress: (progress) {
      log("uploadImageFile progress= $progress");
    });
  } else {
    return uploadFile(File(result.files.single.path!), isPublic: true, onProgress: (progress) {
      log("uploadImageFile progress= $progress");
    });
  }
}

refreshBadgeCount(){
  getUnreadMessagesCount().then((result) {
    updateBadgeCount(result['total']);
  });
}