Fakultas Ilmu Komputer UI

program_bloc.dart 4.15 KB
Newer Older
1
2
3
4
5
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
6
import 'package:home_industry/Pages/Program/model/program.dart';
7
8
9
import 'package:home_industry/Pages/Program/repositories/program_repository.dart';
import 'package:stream_transform/stream_transform.dart';

Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
10
import '../program_page.dart';
11
12
13
14
15
16
import './bloc.dart';

class ProgramBloc extends Bloc<ProgramEvent, ProgramState> {
  final ProgramRepository programRepository;

  ProgramBloc({@required this.programRepository})
17
      : assert(programRepository != null);
18
19
20
21
22
23
24

  @override
  ProgramState get initialState => const InitialListProgramState(page: 0);

  bool _hasReachedMax(ProgramState state) =>
      state is ListProgramsLoaded && state.hasReachedMax;

25
26
27
  bool _hasReachedMaxSorted(ProgramState state) =>
      state is ListProgramsSortedLoaded && state.hasReachedMax;

28
29
  @override
  Stream<ProgramState> mapEventToState(
30
    ProgramEvent event,
31
32
33
34
  ) async* {
    final currentState = state;
    if ((event is FetchProgram) && !_hasReachedMax(currentState)) {
      try {
35
        final programs =
36
            await programRepository.fetchProgram(page: currentState.page + 1);
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        if (currentState is InitialListProgramState) {
          yield ListProgramsLoaded(
              programs: programs.results,
              hasReachedMax: programs.next == null,
              page: currentState.page + 1);
          return;
        }
        if (currentState is ListProgramsLoaded) {
          yield ListProgramsLoaded(
              programs: currentState.programs + programs.results,
              hasReachedMax: programs.next == null,
              page: currentState.page + 1);
          return;
        }
51
52
53
54
55
56
57
        if (currentState is ListProgramsSortedLoaded) {
          yield ListProgramsLoaded(
              programs: currentState.programs,
              hasReachedMax: programs.next == null,
              page: 0);
          return;
        }
58
59
60
61
62
      } on DioError catch (e) {
        yield ListProgramsError(error: e.response.data.toString());
      } catch (e) {
        yield ListProgramsError(error: e.toString());
      }
63
64
    } else if ((event is FetchProgramByStatus) && !_hasReachedMaxSorted(currentState)) {
      try {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
65
        final programs =
66
67
        await programRepository.fetchProgram(page: currentState.page + 1);
        if (currentState is ListProgramsLoaded || currentState is InitialListProgramState) {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
68
          final sort = sortPrograms(programs.results);
69
          yield ListProgramsSortedLoaded(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
70
              programs: sort,
71
72
73
74
              hasReachedMax: programs.next == null,
              page: currentState.page + 1);
          return;
        } else if (currentState is ListProgramsSortedLoaded) {
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
75
          final sort = sortPrograms(programs.results);
76
          yield ListProgramsSortedLoaded(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
77
              programs: currentState.programs + sort,
78
79
80
81
82
83
84
85
86
              hasReachedMax: programs.next == null,
              page: currentState.page + 1);
          return;
        }
      } on DioError catch (e) {
        yield ListProgramsError(error: e.response.data.toString());
      } catch (e) {
        yield ListProgramsError(error: e.toString());
      }
87
88
89
90
    }
  }

  @override
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
91
92
  Stream<Transition<ProgramEvent, ProgramState>> transformEvents(
      Stream<ProgramEvent> events,
93
      TransitionFunction<ProgramEvent, ProgramState> transitionFn) {
94
    return super.transformEvents(
95
96
97
      events.debounce(
        const Duration(milliseconds: 500),
      ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
98
      transitionFn,
99
    );
100
  }
101
}
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

List<Program> sortPrograms(List programs) {
  final programFinished = [];
  final programInProgress = [];
  final programNotStarted = [];
  for (var i = 0; i < programs.length; i++) {
    final program = programs.elementAt(i);
    if (program.endDateTime != null &&
        program.endDateTime.
        compareTo(CustomizableDateTime.current) < 1) {
        programFinished.add(program);
    } else if (program.startDateTime != null &&
        CustomizableDateTime.current.
        compareTo(program.startDateTime) < 1) {
      programNotStarted.add(program);
    } else {
      programInProgress.add(program);
    }
  }
  return [...programNotStarted, ...programInProgress, ...programFinished];
}