updateDataTableRows method Null safety

dynamic updateDataTableRows()

Implementation

updateDataTableRows() {
  if (sortColumnIdx == null) sortIsAscending = true;

  cows = cows!.where((cow) => cowDatas?.firstWhereOrNull((element) => element.cow == cow) != null).toList();
  if (sortIsAscending) {
    // 오름차순 정렬
    cows!.sort((a, b) {
      int result = 0;
      CowData? cowDataA = cowDatas?.firstWhereOrNull((element) => element.cow == a);
      CowData? cowDataB = cowDatas?.firstWhereOrNull((element) => element.cow == b);

      if (cowDataA == null || cowDataB == null) return 0;

      switch (sortColumnIdx) {
        case 0:
          result = a.getDayAfterBirthFromDateTime(selectedDateTime).compareTo(b.getDayAfterBirthFromDateTime(selectedDateTime));
          break;
        case 1:
          result = cowDataA.data.first.cowDataHour.first.lactation.value.compareTo(cowDataB.data.first.cowDataHour.first.lactation.value);
          break;
        case 2:
          result = cowDataA.data.first.cowDataHour.first.eating.value.compareTo(cowDataB.data.first.cowDataHour.first.eating.value);
          break;
        case 3:
          result = cowDataA.data.first.cowDataHour.first.ruminant.value.compareTo(cowDataB.data.first.cowDataHour.first.ruminant.value);
          break;
        // case 4:
        //   result = cowDataA.data.first.cowDataHour.first.drinking.value.compareTo(cowDataB.data.first.cowDataHour.first.drinking.value);
        //   break;
        case 4:
          result = cowDataA.data.first.cowDataHour.first.heading.value.compareTo(cowDataB.data.first.cowDataHour.first.heading.value);
          break;
        case 5:
          result = cowDataA.data.first.cowDataHour.first.sitting.value.compareTo(cowDataB.data.first.cowDataHour.first.sitting.value);
          break;
        case 6:
          result = cowDataA.data.first.cowDataHour.first.standing.value.compareTo(cowDataB.data.first.cowDataHour.first.standing.value);
          break;
        case 7:
          result = cowDataA.data.first.cowDataHour.first.cough.value.compareTo(cowDataB.data.first.cowDataHour.first.cough.value);
          break;
        case 8:
          result = cowDataA.data.first.cowDataHour.first.activity.value.compareTo(cowDataB.data.first.cowDataHour.first.activity.value);
          break;
        case 9:
          result = cowDataA.data.first.cowDataHour.first.digestive.value.compareTo(cowDataB.data.first.cowDataHour.first.digestive.value);
          break;
        case 10:
          result = cowDataA.data.first.cowDataHour.first.respiratory.value.compareTo(cowDataB.data.first.cowDataHour.first.respiratory.value);
          break;
        case 11:
          result = cowDataA.data.first.cowDataHour.first.bcs.value.compareTo(cowDataB.data.first.cowDataHour.first.bcs.value);
          break;
        default:
        // 기본 정렬상태, 소 아이디 오름차순으로 정렬
          result = int.parse(a.id.substring(4)).compareTo(int.parse(b.id.substring(4)));
          break;
      }
      return result;
    });
  } else {
    // 내림차순 정렬
    cows!.sort((a, b) {
      int result = 0;
      CowData? cowDataA = cowDatas?.firstWhereOrNull((element) => element.cow == a);
      CowData? cowDataB = cowDatas?.firstWhereOrNull((element) => element.cow == b);

      if (cowDataA == null || cowDataB == null) return 1;

      switch (sortColumnIdx) {
        case 0:
          result = b.getDayAfterBirthFromDateTime(selectedDateTime).compareTo(a.getDayAfterBirthFromDateTime(selectedDateTime));
          break;
        case 1:
          result = cowDataB.data.first.cowDataHour.first.lactation.value.compareTo(cowDataA.data.first.cowDataHour.first.lactation.value);
          break;
        case 2:
          result = cowDataB.data.first.cowDataHour.first.eating.value.compareTo(cowDataA.data.first.cowDataHour.first.eating.value);
          break;
        case 3:
          result = cowDataB.data.first.cowDataHour.first.ruminant.value.compareTo(cowDataA.data.first.cowDataHour.first.ruminant.value);
          break;
        // case 4:
        //   result = cowDataB.data.first.cowDataHour.first.drinking.value.compareTo(cowDataA.data.first.cowDataHour.first.drinking.value);
        //   break;
        case 4:
          result = cowDataB.data.first.cowDataHour.first.heading.value.compareTo(cowDataA.data.first.cowDataHour.first.heading.value);
          break;
        case 5:
          result = cowDataB.data.first.cowDataHour.first.sitting.value.compareTo(cowDataA.data.first.cowDataHour.first.sitting.value);
          break;
        case 6:
          result = cowDataB.data.first.cowDataHour.first.standing.value.compareTo(cowDataA.data.first.cowDataHour.first.standing.value);
          break;
        case 7:
          result = cowDataB.data.first.cowDataHour.first.cough.value.compareTo(cowDataA.data.first.cowDataHour.first.cough.value);
          break;
        case 8:
          result = cowDataB.data.first.cowDataHour.first.activity.value.compareTo(cowDataA.data.first.cowDataHour.first.activity.value);
          break;
        case 9:
          result = cowDataB.data.first.cowDataHour.first.digestive.value.compareTo(cowDataA.data.first.cowDataHour.first.digestive.value);
          break;
        case 10:
          result = cowDataB.data.first.cowDataHour.first.respiratory.value.compareTo(cowDataA.data.first.cowDataHour.first.respiratory.value);
          break;
        case 11:
          result = cowDataB.data.first.cowDataHour.first.bcs.value.compareTo(cowDataA.data.first.cowDataHour.first.bcs.value);
          break;
        default:
        // 기본 정렬상태, 소 아이디 오름차순으로 정렬
          result = a.id.compareTo(b.id);
          break;
      }
      return result;
    });
  }

  dataTableRows = cows!.mapIndexed((Cow cow, i) {
    CowData? cowData = cowDatas?.firstWhereOrNull((element) => element.cow == cow);
    if (cowData == null) cowData = CowData.fromEmpty(cow);
    Color highlightedColor = CustomColors.grapefruit.withOpacity(0.4);

    DataTableRow dataTableRow = DataTableRow(
      id: "${cow.id}",
      cells: [
        DataTableCell(
          id: "cowManagementNumber",
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(cow.cowStatus.icon, width: 24, height: 24),
              Container(width: 4),
              Text("${cowData.cow?.managementNumber.toString()}", textAlign: TextAlign.center),
            ],
          ),
          onTap: () {
            LogManager().addLog("${cow.id} ${cow.managementNumber} 돋보기 버튼)", action: LogConstants.buttonTap, screen: runtimeType.toString());
            Navigator.of(ApiService().context, rootNavigator: true).pushNamed(CowDetailScreen.routeName, arguments: CowDetailScreenArguments(cow: cowData!.cow!, selectedDate: selectedDateTime));
          },
        ),
        DataTableCell(id: "dayAfterBirth", text: cowData.cow?.getDayAfterBirthStrFromDateTime(selectedDateTime)),
        DataTableCell(
          id: "lactation",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.lactation) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 2,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.lactation.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.lactation),
                    size: Size(60, 16),
                    progressColor: CustomColors.light_gold,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(
          id: "eating",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.eating) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 0,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.eating.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.eating),
                    size: Size(60, 16),
                    progressColor: CustomColors.emerald,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(
          id: "ruminant",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.ruminant) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 0,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.ruminant.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.ruminant),
                    size: Size(60, 16),
                    progressColor: CustomColors.light_teal,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        // DataTableCell(
        //   id: "drinking",
        //   child: Container(
        //     margin: EdgeInsets.symmetric(horizontal: 6),
        //     child: RotatedBox(
        //       quarterTurns: 0,
        //       child: LinearProgressBar(
        //         value: cowData.data.first.cowDataHour.first.drinking.value,
        //         maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.drinking),
        //         size: Size(100, 16),
        //         progressColor: CustomColors.sky,
        //         backgroundColor: CustomColors.white,
        //       ),
        //     ),
        //   ),
        // ),
        DataTableCell(
          id: "heading",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.heading) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 0,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.heading.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.heading),
                    size: Size(60, 16),
                    progressColor: CustomColors.peachy_pink,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(
          id: "sitting",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.sitting) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 2,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.sitting.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.sitting),
                    size: Size(60, 16),
                    progressColor: CustomColors.lighter_purple,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(
          id: "standing",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.standing) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 0,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.standing.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.standing),
                    size: Size(60, 16),
                    progressColor: CustomColors.very_light_purple,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(
          id: "cough",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.cough) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 0,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.cough.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.cough),
                    size: Size(60, 16),
                    progressColor: CustomColors.sky,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(
          id: "activity",
          child: Container(
            color: Helper().isCowDataHighlighted(cowData.data.first.cowDataHour.first.activity) ? highlightedColor : Colors.transparent,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(width: 0),
                RotatedBox(
                  quarterTurns: 0,
                  child: LinearProgressBar(
                    value: cowData.data.first.cowDataHour.first.activity.value,
                    maxValue: Helper().getMaxValueFromCowData(cowDatas: cowDatas, action: ActionType.activity),
                    size: Size(60, 16),
                    progressColor: CustomColors.dodger_blue,
                    backgroundColor: CustomColors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
        DataTableCell(id: "digestive", text: cowData.data.first.cowDataHour.first.digestive.value.toInt() == -1 ? "-" : cowData.data.first.cowDataHour.first.digestive.value.toInt().toString()),
        DataTableCell(
            id: "respiratory", text: cowData.data.first.cowDataHour.first.respiratory.value.toInt() == -1 ? "-" : cowData.data.first.cowDataHour.first.respiratory.value.toInt().toString()),
        DataTableCell(id: "bcs", text: BCSType.good.codeFromIndex(cowData.data.first.cowDataHour.first.bcs.value.toInt())),
      ],
      selected: cows!.firstWhereOrNull((element) => element.id == cow.id) != null ? cows!.firstWhereOrNull((element) => element.id == cow.id)!.isSelected : false,
      onSelectChanged: (bool? selected) => onSelectChanged(selected, cows!.firstWhereOrNull((element) => element.id == cow.id)),
    );

    return dataTableRow;
  }).toList();

  cows!.where((cow) => cowDatas?.firstWhereOrNull((element) => element.cow == cow) != null).mapIndexed((Cow cow, i) {
    CowData? cowData = cowDatas?.firstWhereOrNull((element) => element.cow == cow);
    if (cowData == null) cowData = CowData.fromEmpty(cow);

    if (cow.isSelected == true) {
      dataTableRows!.insert(
        i + 1,
        DataTableRow(
          id: "expanded",
          cells: [
            DataTableCell(
              id: "expanded",
              child: CowCurrentStatusDataTableExpandedRow(
                columns: CustomDataTableType.cowCurrentStatus.columns,
                cowData: cowData,
                backgroundColor: CustomColors.emerald.withOpacity(0.05),
                values: [
                  0,
                  0,
                  cowData.data.first.cowDataHour.first.lactation.value,
                  cowData.data.first.cowDataHour.first.eating.value,
                  cowData.data.first.cowDataHour.first.ruminant.value,
                  // cowData.data.first.cowDataHour.first.drinking.value,
                  cowData.data.first.cowDataHour.first.heading.value,
                  cowData.data.first.cowDataHour.first.sitting.value,
                  cowData.data.first.cowDataHour.first.standing.value,
                  cowData.data.first.cowDataHour.first.cough.value,
                  cowData.data.first.cowDataHour.first.activity.value,
                  cowData.data.first.cowDataHour.first.digestive.value,
                  cowData.data.first.cowDataHour.first.respiratory.value,
                  cowData.data.first.cowDataHour.first.bcs.value,
                ],
              ),
            ),
            ...List.generate(CustomDataTableType.cowCurrentStatus.columns.length - 1, (index) => DataTableCell(id: '', child: Container()))
          ],
        ),
      );
    }
    return cow;
  }).toList();
}