mirror of
				https://gitlab.com/foxixus/neomovies_mobile.git
				synced 2025-10-29 11:58:50 +05:00 
			
		
		
		
	- Fix authorization issues by improving error handling for unverified accounts - Enable auto-login after successful email verification - Fix poster fetching to use NeoMovies API instead of TMDB directly - Add missing video player models (VideoQuality, AudioTrack, Subtitle, PlayerSettings) - Add video_player and chewie dependencies for native video playback - Update Movie model to use API images endpoint for better CDN control Resolves authentication and image loading issues.
		
			
				
	
	
		
			145 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			145 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:http/http.dart' as http;
 | |
| import 'package:neomovies_mobile/data/models/movie.dart';
 | |
| import 'package:neomovies_mobile/data/models/favorite.dart';
 | |
| import 'package:neomovies_mobile/data/models/reaction.dart';
 | |
| import 'package:neomovies_mobile/data/models/auth_response.dart';
 | |
| import 'package:neomovies_mobile/data/models/user.dart';
 | |
| import 'package:neomovies_mobile/data/api/neomovies_api_client.dart'; // новый клиент
 | |
| import 'package:neomovies_mobile/data/exceptions/auth_exceptions.dart';
 | |
| 
 | |
| class ApiClient {
 | |
|   final NeoMoviesApiClient _neoClient;
 | |
| 
 | |
|   ApiClient(http.Client client)
 | |
|       : _neoClient = NeoMoviesApiClient(client);
 | |
| 
 | |
|   // ---- Movies ----
 | |
|   Future<List<Movie>> getPopularMovies({int page = 1}) {
 | |
|     return _neoClient.getPopularMovies(page: page);
 | |
|   }
 | |
| 
 | |
|   Future<List<Movie>> getTopRatedMovies({int page = 1}) {
 | |
|     return _neoClient.getTopRatedMovies(page: page);
 | |
|   }
 | |
| 
 | |
|   Future<List<Movie>> getUpcomingMovies({int page = 1}) {
 | |
|     return _neoClient.getUpcomingMovies(page: page);
 | |
|   }
 | |
| 
 | |
|   Future<Movie> getMovieById(String id) {
 | |
|     return _neoClient.getMovieById(id);
 | |
|   }
 | |
| 
 | |
|   Future<Movie> getTvById(String id) {
 | |
|     return _neoClient.getTvShowById(id);
 | |
|   }
 | |
| 
 | |
|   // ---- Search ----
 | |
|   Future<List<Movie>> searchMovies(String query, {int page = 1}) {
 | |
|     return _neoClient.search(query, page: page);
 | |
|   }
 | |
| 
 | |
|   // ---- Favorites ----
 | |
|   Future<List<Favorite>> getFavorites() {
 | |
|     return _neoClient.getFavorites();
 | |
|   }
 | |
| 
 | |
|   Future<void> addFavorite(
 | |
|     String mediaId,
 | |
|     String mediaType,
 | |
|     String title,
 | |
|     String posterPath,
 | |
|   ) {
 | |
|     return _neoClient.addFavorite(
 | |
|       mediaId: mediaId,
 | |
|       mediaType: mediaType,
 | |
|       title: title,
 | |
|       posterPath: posterPath,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   Future<void> removeFavorite(String mediaId, {String mediaType = 'movie'}) {
 | |
|     return _neoClient.removeFavorite(mediaId, mediaType: mediaType);
 | |
|   }
 | |
| 
 | |
|   Future<bool> checkIsFavorite(String mediaId, {String mediaType = 'movie'}) {
 | |
|     return _neoClient.checkIsFavorite(mediaId, mediaType: mediaType);
 | |
|   }
 | |
| 
 | |
|   // ---- Reactions ----
 | |
|   Future<Map<String, int>> getReactionCounts(
 | |
|       String mediaType, String mediaId) {
 | |
|     return _neoClient.getReactionCounts(
 | |
|       mediaType: mediaType,
 | |
|       mediaId: mediaId,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   Future<void> setReaction(
 | |
|       String mediaType, String mediaId, String reactionType) {
 | |
|     return _neoClient.setReaction(
 | |
|       mediaType: mediaType,
 | |
|       mediaId: mediaId,
 | |
|       reactionType: reactionType,
 | |
|     );
 | |
|   }
 | |
| 
 | |
|   Future<List<UserReaction>> getMyReactions() {
 | |
|     return _neoClient.getMyReactions();
 | |
|   }
 | |
| 
 | |
|   // Get single user reaction for specific media
 | |
|   Future<UserReaction?> getMyReaction(String mediaType, String mediaId) async {
 | |
|     final reactions = await _neoClient.getMyReactions();
 | |
|     try {
 | |
|       return reactions.firstWhere(
 | |
|         (r) => r.mediaType == mediaType && r.mediaId == mediaId,
 | |
|       );
 | |
|     } catch (e) {
 | |
|       return null; // No reaction found
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   // ---- External IDs (IMDb) ----
 | |
|   Future<String?> getImdbId(String mediaId, String mediaType) async {
 | |
|     // This would need to be implemented in NeoMoviesApiClient
 | |
|     // For now, return null or implement a stub
 | |
|     // TODO: Add getExternalIds endpoint to backend
 | |
|     return null;
 | |
|   }
 | |
| 
 | |
|   // ---- Auth ----
 | |
|   Future<void> register(String name, String email, String password) {
 | |
|     return _neoClient.register(
 | |
|       name: name,
 | |
|       email: email,
 | |
|       password: password,
 | |
|     ).then((_) {}); // старый код ничего не возвращал
 | |
|   }
 | |
| 
 | |
|   Future<AuthResponse> login(String email, String password) async {
 | |
|     try {
 | |
|       return await _neoClient.login(email: email, password: password);
 | |
|     } catch (e) {
 | |
|       final errorMessage = e.toString();
 | |
|       if (errorMessage.contains('Account not activated') || 
 | |
|           errorMessage.contains('not verified') ||
 | |
|           errorMessage.contains('Please verify your email')) {
 | |
|         throw UnverifiedAccountException(email, message: errorMessage);
 | |
|       }
 | |
|       rethrow;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   Future<AuthResponse> verify(String email, String code) {
 | |
|     return _neoClient.verifyEmail(email: email, code: code);
 | |
|   }
 | |
| 
 | |
|   Future<void> resendCode(String email) {
 | |
|     return _neoClient.resendVerificationCode(email);
 | |
|   }
 | |
| 
 | |
|   Future<void> deleteAccount() {
 | |
|     return _neoClient.deleteAccount();
 | |
|   }
 | |
| } |