isChosenAll<T extends AbsNodeType> function

EChosenAllValues isChosenAll<T extends AbsNodeType>(
  1. TreeType<T> tree, {
  2. bool isThisLazyTree = false,
})

Check if the the tree is chosen all

  • isThisTreeLazy: When this function is used in lazy-load tree, some nodes may contain 0 child because their children haven't been loaded.

Implementation

EChosenAllValues isChosenAll<T extends AbsNodeType>(TreeType<T> tree,
    {bool isThisLazyTree = false}) {
  //? Case 1: This tree is only a leaf
  //? ______
  if (tree.isLeaf) {
    if (tree.data.isUnavailable) {
      return EChosenAllValues.notChosenable;
    } else {
      return tree.data.isChosen == true
          ? EChosenAllValues.chosenAll
          : EChosenAllValues.unchosenAll;
    }
  }

  //? Case 2: This tree can contain some children
  //? ______

  /// Means one of it children has been chosen all
  bool hasChosenAll = false;

  /// Means one of it children has been unchosen all
  bool hasUnchosenAll = false;

  /**
   * ! SPECIAL CASE: If tree is loaded lazily -> if its children haven't been
   * ! loaded, do not need to call [isChosenAll(child)] and directly return
   * ! result [isChosen]
   *
   * - If one of its child is [EChosenAllValues.chosenSome], just return & exit.
   *
   * - Case chosen some: [hasChosenAll && hasUnchosenAll]...
   *
   * - Case all of children are chosen: [hasChosenAll && !hasUnchosenAll]...
   *
   * - Case all of children are not chosen: [!hasChosenAll && hasUnchosenAll]...
   *
   * - Else, return default value [EChosenAllValues.notChosenable]
   */

  // this is lazy tree & its children haven't been loaded
  if (isThisLazyTree && !tree.isChildrenLoadedLazily) {
    if (tree.data.isUnavailable) {
      return EChosenAllValues.notChosenable;
    } else {
      return tree.data.isChosen == true // true
          ? EChosenAllValues.chosenAll
          : EChosenAllValues.unchosenAll; // false (no exist null)
    }
  }

  for (var child in tree.children) {
    var temp = isChosenAll(child, isThisLazyTree: isThisLazyTree);
    switch (temp) {
      case EChosenAllValues.chosenSome:
        return EChosenAllValues.chosenSome;
      case EChosenAllValues.chosenAll:
        hasChosenAll = true;
        break;
      case EChosenAllValues.unchosenAll:
        hasUnchosenAll = true;
        break;
      default:
        break;
    }
  }

  if (hasChosenAll && hasUnchosenAll) {
    return EChosenAllValues.chosenSome;
  } else if (hasChosenAll && !hasUnchosenAll) {
    return EChosenAllValues.chosenAll;
  } else if (!hasChosenAll && hasUnchosenAll) {
    return EChosenAllValues.unchosenAll;
  } else {
    // return default
    return EChosenAllValues.notChosenable;
  }
}