Fakultas Ilmu Komputer UI

kegiatan_repository.dart 1.51 KB
Newer Older
1
import 'package:bisaGo/flavor/flavor.dart';
2
3
4
5
6
import 'package:bisaGo/model/kegiatan.dart';
import 'package:bisaGo/network/network_interface.dart';

abstract class BaseKegiatanRepository {
  Future<KegiatanList> fetchKegiatan(String placeId);
7
  Future<List<String>> fetchImages(String placeId, int id);
8
9
10
11
12
13
14
15
16
17
}

class KegiatanRepository implements BaseKegiatanRepository {
  final NetworkInterface _network = NetworkInterface();

  @override
  Future<KegiatanList> fetchKegiatan(String placeId) async {
    final url = '/informasi-fasilitas/lokasi/list-kegiatan/$placeId';
    final response = await _network.get(url: url, isLogin: false);
    final data = response.values.toList().reversed.toList();
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    var allKegiatan = [];

    for (var _index = 0; _index < data.length; _index++) {
      var kegiatan = KegiatanModel.fromJson(data[_index]);
      kegiatan.image = await fetchImages(kegiatan.placeId, kegiatan.id);
      allKegiatan.add(kegiatan);
    }
    allKegiatan = allKegiatan.cast<KegiatanModel>();
    return KegiatanList(allKegiatan);
  }

  @override
  Future<List<String>> fetchImages(String placeId, int id) async {
    final url = '/informasi-fasilitas/lokasi/list-foto-kegiatan/$placeId/$id';
    final response = await _network.get(url: url, isLogin: false);
    var allImages = [];

    for (var _index=0; _index < response.length; _index++) {
      var fotoUrl = ApiFlavor.getBaseUrl() + response.values.toList()[_index]['foto'];
      allImages.add(fotoUrl);
    }
    allImages = allImages.cast<String>();
    return allImages;
41
42
  }
}