Fakultas Ilmu Komputer UI

LokasiResponseBloc.dart 1.95 KB
Newer Older
1
2
3
4
5
6
7
import 'dart:async';

import 'package:ppl_disabilitas/model/lokasi.dart';
import 'package:ppl_disabilitas/network/data/network_model.dart';
import 'package:ppl_disabilitas/repository/LokasiRepository.dart';

class LokasiResponseBloc {
8
  StreamController _recentSearchController;
9
10
11
  LokasiRepository _lokasiRepository;
  StreamController _lokasiListController;

12
13
14
15
16
17
18
19
20
  StreamSink<NetworkModel<LokasiListResponse>> get recentSearchSink =>
      _recentSearchController.sink;
  Stream<NetworkModel<LokasiListResponse>> get recentSearchStream =>
      _recentSearchController.stream;

  StreamSink<NetworkModel<LokasiListResponse>> get lokasiListSink =>
      _lokasiListController.sink;
  Stream<NetworkModel<LokasiListResponse>> get lokasiListStream =>
      _lokasiListController.stream;
21
22

  LokasiResponseBloc() {
23
24
25
    _lokasiListController =
        StreamController<NetworkModel<LokasiListResponse>>();
    _recentSearchController = StreamController<NetworkModel<LokasiListResponse>>();
26
27
    _lokasiRepository = LokasiRepository();
    fetchLokasiList();
28
    fetchRecentSearch();
29
  }
30

31
32
33
  fetchLokasiList() async {
    lokasiListSink.add(NetworkModel.loading('Getting Locations'));
    try {
34
35
      LokasiListResponse lokasiListResponse =
          await _lokasiRepository.fetchLokasi();
36
37
38
39
40
41
      lokasiListSink.add(NetworkModel.completed(lokasiListResponse));
    } catch (e) {
      lokasiListSink.add(NetworkModel.error(e.toString()));
    }
  }

42
43
44
45
46
47
48
49
50
51
52
53
54
55
  fetchRecentSearch() async {
    recentSearchSink.add(NetworkModel.loading('Getting Recent Search'));
    try {
      LokasiListResponse recentSearchData = await _lokasiRepository.fetchRecentSearch();
      recentSearchSink.add(NetworkModel.completed(recentSearchData));
    } catch (e) {
      recentSearchSink.add(NetworkModel.error(e.toString()));
    }
  }

  saveRecentSearch(Lokasi search) async {
    await _lokasiRepository.saveRecentSearch(search);
  }

56
  dispose() {
57
    _recentSearchController?.close();
58
59
    _lokasiListController?.close();
  }
60
}