Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3.  
  4. import 'package:http/http.dart' as http;
  5. import '../helpers/api.dart';
  6. import '../models/movie_model.dart';
  7. export '../models/movie_model.dart';
  8. import '../models/movie_video_model.dart';
  9.  
  10. class MoviesProvider {
  11. String _url = Api.url;
  12. bool _loading = false;
  13. int _currentPage = 1;
  14. int _pages = 1;
  15. List<Movie> _movies = List();
  16. final _moviesStream = StreamController<List<Movie>>();
  17.  
  18. Function(List<Movie>) get moviesSink => _moviesStream.sink.add;
  19. Stream<List<Movie>> get moviesStream => _moviesStream.stream;
  20.  
  21. void dispose() {
  22. _moviesStream?.close();
  23. }
  24.  
  25. Future<List<Movie>> _process(String endpoint) async {
  26. try {
  27. final resp = await http.get(_url + endpoint);
  28.  
  29. if (resp.statusCode != 200) {
  30. throw Exception('Failed to load');
  31. }
  32.  
  33. final data = json.decode(resp.body);
  34.  
  35. final movies = Movies.fromJsonList(data);
  36.  
  37. return movies.items;
  38. } catch (e) {
  39. throw Exception('Failed to load');
  40. }
  41. }
  42.  
  43. Future<bool> index() async {
  44. try {
  45. if (_currentPage <= _pages) {
  46. _loading = !_loading;
  47. final resp = await http.get(_url + 'movies?page=$_currentPage');
  48.  
  49. if (resp.statusCode != 200) {
  50. _moviesStream.addError('Failed to load');
  51. throw Exception('Failed to load');
  52. }
  53.  
  54. final data = json.decode(resp.body);
  55. final movies = Movies.fromJsonList(data['data']);
  56. _pages = data['last_page'];
  57. _currentPage++;
  58.  
  59.  
  60. _movies.addAll(movies.items);
  61. moviesSink(_movies);
  62. _loading = !_loading;
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. } catch (e) {
  68. _moviesStream.addError('Failed to load');
  69. throw Exception('Failed to load');
  70. }
  71. }
  72.  
  73. Future<List<Movie>> latest() async {
  74. return _process('movies/latest');
  75. }
  76.  
  77. Future<List<Movie>> recommended() async {
  78. return _process('movies/recommended');
  79. }
  80.  
  81. Future<List<Movie>> popular() async {
  82. return _process('movies/popular');
  83. }
  84.  
  85. Future<List<Movie>> recents() async {
  86. return _process('movies/recents');
  87. }
  88.  
  89. Future<List<Movie>> kids() async {
  90. return _process('movies/kids');
  91. }
  92.  
  93. Future<List<Movie>> genre(int id) async {
  94. return _process('genres/show/' + id.toString());
  95. }
  96.  
  97. Future<List<Movie>> relateds(id) async {
  98. return _process('movies/relateds/' + id.toString());
  99. }
  100.  
  101. Future<Movie> show(String id) async {
  102. try {
  103. final resp = await http.get(_url + 'movies/show/' + id);
  104.  
  105. if (resp.statusCode != 200) {
  106. throw Exception('Failed to load');
  107. }
  108.  
  109. final data = json.decode(resp.body);
  110.  
  111. final movie = Movie.fromJson(data);
  112.  
  113. return movie;
  114. } catch (e) {
  115. throw Exception('Failed to load');
  116. }
  117. }
  118.  
  119. Future<List<MovieVideo>> videos(String id) async {
  120. try {
  121. final resp = await http.get(_url + 'movies/videos/' + id);
  122.  
  123. if (resp.statusCode != 200) {
  124. throw Exception('Failed to load');
  125. }
  126.  
  127. final data = json.decode(resp.body);
  128.  
  129. final videos = MovieVideos.fromJsonList(data);
  130.  
  131. return videos.items;
  132. } catch (e) {
  133. throw Exception('Failed to load');
  134. }
  135. }
  136.  
  137. void view(id) {
  138. http.get(_url + 'movies/view/' + id);
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement