diff --git a/aait/mobile/group-3/blog_app/lib/core/util/bookmark_preferences.dart b/aait/mobile/group-3/blog_app/lib/core/util/bookmark_preferences.dart index 5048d5b3b..44d50f2a5 100644 --- a/aait/mobile/group-3/blog_app/lib/core/util/bookmark_preferences.dart +++ b/aait/mobile/group-3/blog_app/lib/core/util/bookmark_preferences.dart @@ -1,8 +1,46 @@ +import 'dart:convert'; +import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; import '../../features/article/domain/entity/article.dart'; import '../../features/article/domain/use_case/get_article_by_id.dart'; +import '../error/exception.dart'; +import 'constants.dart'; + +Future getArticleById(String id) async { + String baseUrl = getBaseUrl(); + try { + String token = await getStoredToken(); + + var request = http.Request('GET', Uri.parse('$baseUrl/article/$id')); + request.headers['Authorization'] = 'Bearer $token'; + + http.StreamedResponse response = await request.send(); + + if (response.statusCode == 200) { + final http.Response result = await http.Response.fromStream(response); + + final jsonResponse = jsonDecode(result.body); + return jsonEncode(jsonResponse["data"]); + + } else { + final result = await http.Response.fromStream(response); + throw ServerException( + statusCode: result.statusCode, + message: "Failed to get the article"); + } + } catch (e) { + throw ServerException( + statusCode: 500, message: "Failed to get the article$e"); + } + } + + Future getStoredToken() async { + final SharedPreferences pref = await SharedPreferences.getInstance(); + final String token = pref.getString(cachedToken)!; + return token; + } class BookmarkPreferences { static SharedPreferences? _preferences; @@ -37,15 +75,19 @@ class BookmarkPreferences { return _preferences?.getStringList(userId) ?? []; } - static Future> getAllBookmarkedModels(String userId)async { + static Future getAllBookmarked(String userId)async { final articleIds = _preferences?.getStringList(userId) ?? []; - List
articles = []; + List articles = []; for (String id in articleIds) { - final article = await usecase!(id); - article.fold((l) => null, (value) => articles.add(value)); + final article = await getArticleById(id); + final articleMap = jsonDecode(article); + if (articleMap != null) + articles.add(article); } - - return articles; + final finalResult = jsonEncode(articles); + print("===============I accomplished my mission==============="); + print(finalResult); + return finalResult; } } diff --git a/aait/mobile/group-3/blog_app/lib/features/article/data/data_sources/article_remote_datasource.dart b/aait/mobile/group-3/blog_app/lib/features/article/data/data_sources/article_remote_datasource.dart index fe5fbc5db..639387a88 100644 --- a/aait/mobile/group-3/blog_app/lib/features/article/data/data_sources/article_remote_datasource.dart +++ b/aait/mobile/group-3/blog_app/lib/features/article/data/data_sources/article_remote_datasource.dart @@ -47,7 +47,6 @@ class ArticleRemoteDataSourceImpl implements ArticleRemoteDataSource { photoFile = pngImage; mimeType = lookupMimeType(photoFile.path)!; } - var request = http.MultipartRequest('POST', Uri.parse('$baseUrl/article')); @@ -307,4 +306,4 @@ class ArticleRemoteDataSourceImpl implements ArticleRemoteDataSource { final String token = pref.getString(cachedToken)!; return token; } -} + } diff --git a/aait/mobile/group-3/blog_app/lib/features/article/presentation/bloc/article_bloc.dart b/aait/mobile/group-3/blog_app/lib/features/article/presentation/bloc/article_bloc.dart index e3e44f00b..4bd6eca89 100644 --- a/aait/mobile/group-3/blog_app/lib/features/article/presentation/bloc/article_bloc.dart +++ b/aait/mobile/group-3/blog_app/lib/features/article/presentation/bloc/article_bloc.dart @@ -3,6 +3,8 @@ import 'dart:async'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import '../../domain/entity/getArticlesEntity.dart'; +import '../../domain/use_case/get_tags.dart'; import '../../domain/entity/getArticlesEntity.dart'; import '../../domain/use_case/get_tags.dart'; import '../../../../core/use_case/usecase.dart'; diff --git a/aait/mobile/group-3/blog_app/lib/features/article/presentation/screen/article_reading.dart b/aait/mobile/group-3/blog_app/lib/features/article/presentation/screen/article_reading.dart index 9c3c52ee9..a5b1e829b 100644 --- a/aait/mobile/group-3/blog_app/lib/features/article/presentation/screen/article_reading.dart +++ b/aait/mobile/group-3/blog_app/lib/features/article/presentation/screen/article_reading.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:top_snackbar_flutter/custom_snack_bar.dart'; +import 'package:top_snackbar_flutter/custom_snack_bar.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:go_router/go_router.dart'; import 'package:top_snackbar_flutter/top_snack_bar.dart'; diff --git a/aait/mobile/group-3/blog_app/lib/features/profile/data/data_sources/profile_local_data_source.dart b/aait/mobile/group-3/blog_app/lib/features/profile/data/data_sources/profile_local_data_source.dart index a1c2601bb..845e6744c 100644 --- a/aait/mobile/group-3/blog_app/lib/features/profile/data/data_sources/profile_local_data_source.dart +++ b/aait/mobile/group-3/blog_app/lib/features/profile/data/data_sources/profile_local_data_source.dart @@ -1,11 +1,12 @@ import 'dart:convert'; +import 'package:blog_app/core/util/bookmark_preferences.dart'; import 'package:blog_app/features/profile/data/models/article_model.dart'; import 'package:blog_app/features/profile/domain/entity/article.dart'; import 'package:shared_preferences/shared_preferences.dart'; abstract class ProfileLocalDataSource { - Future> getBookmarkArticles(); + Future> getBookmarkArticles(String userId); } const CACHED_ARTICLE_LIST = 'CACHED_ARTICLE_LIST'; @@ -16,16 +17,23 @@ class ProfileLocalDataSourceImpl implements ProfileLocalDataSource { ProfileLocalDataSourceImpl({required this.sharedPreferences}); @override - Future> getBookmarkArticles() async { + Future> getBookmarkArticles(String userId) async { //for testing purpose final dummy = _getDummyArticles(); final jsonVal = jsonEncode(dummy); sharedPreferences.setString(CACHED_ARTICLE_LIST, jsonVal); /////////////////////////////////////////////////////////////////// - final jsonValues = await sharedPreferences.getString(CACHED_ARTICLE_LIST); + final jsonValues = await BookmarkPreferences.getAllBookmarked(userId); if (jsonValues != null) { final List jsonList = jsonDecode(jsonValues); - final List
convertedList = jsonList.map
((e) => ArticleModel.fromJson(e)) + print("111$jsonList"); + final List
convertedList = jsonList.map
((e) { + print("222$e"); + final art = ArticleModel.fromJson(jsonDecode(e)); + print("333$art"); + return art; + + } ) .toList(); return Future.value(convertedList); } else { diff --git a/aait/mobile/group-3/blog_app/lib/features/profile/data/repository/profile_repository_impl.dart b/aait/mobile/group-3/blog_app/lib/features/profile/data/repository/profile_repository_impl.dart index 3ed3f41c1..de4995fd7 100644 --- a/aait/mobile/group-3/blog_app/lib/features/profile/data/repository/profile_repository_impl.dart +++ b/aait/mobile/group-3/blog_app/lib/features/profile/data/repository/profile_repository_impl.dart @@ -21,7 +21,7 @@ class ProfileRepositoryImpl implements ProfileRepository { Future> getProfile() async { try { final remoteProfile = await remoteDataSource.getProfile(); - final bookmarks = await localDataSource.getBookmarkArticles(); + final bookmarks = await localDataSource.getBookmarkArticles(remoteProfile.id); return Right(remoteProfile.copyWith(bookmarks: bookmarks)); } on ServerException { return Left(ServerFailure( @@ -30,10 +30,10 @@ class ProfileRepositoryImpl implements ProfileRepository { } @override - Future> updateProfilePicture(XFile image) async { + Future> updateProfilePicture({ required image, required String userId}) async { try { final remoteProfile = await remoteDataSource.updateProfilePicture(image); - final bookmarks = await localDataSource.getBookmarkArticles(); + final bookmarks = await localDataSource.getBookmarkArticles(userId); return Right(remoteProfile.copyWith(bookmarks: bookmarks)); } on ServerException { return Left(ServerFailure( diff --git a/aait/mobile/group-3/blog_app/lib/features/profile/domain/repositories/profile_repository.dart b/aait/mobile/group-3/blog_app/lib/features/profile/domain/repositories/profile_repository.dart index f0a571437..f47baa153 100644 --- a/aait/mobile/group-3/blog_app/lib/features/profile/domain/repositories/profile_repository.dart +++ b/aait/mobile/group-3/blog_app/lib/features/profile/domain/repositories/profile_repository.dart @@ -5,5 +5,5 @@ import 'package:image_picker/image_picker.dart'; abstract class ProfileRepository { Future> getProfile(); - Future> updateProfilePicture(XFile image); + Future> updateProfilePicture({required XFile image, required String userId}); } diff --git a/aait/mobile/group-3/blog_app/lib/features/profile/domain/use_case/update_profile_picture.dart b/aait/mobile/group-3/blog_app/lib/features/profile/domain/use_case/update_profile_picture.dart index 44b1fc479..d4bff503c 100644 --- a/aait/mobile/group-3/blog_app/lib/features/profile/domain/use_case/update_profile_picture.dart +++ b/aait/mobile/group-3/blog_app/lib/features/profile/domain/use_case/update_profile_picture.dart @@ -3,13 +3,25 @@ import 'package:blog_app/core/use_case/usecase.dart'; import 'package:blog_app/features/profile/domain/entity/profile.dart'; import 'package:blog_app/features/profile/domain/repositories/profile_repository.dart'; import 'package:dartz/dartz.dart'; +import 'package:equatable/equatable.dart'; +import 'package:image_picker/image_picker.dart'; -class UpdateProfilePicture extends UseCase { +class UpdateProfilePicture extends UseCase { final ProfileRepository repository; UpdateProfilePicture({required this.repository}); @override - Future> call(Params params) async { - return await repository.updateProfilePicture(params.data); + Future> call(UpdateProfileParams params) async { + return await repository.updateProfilePicture(image: params.image, userId: params.userId); } } + +class UpdateProfileParams extends Equatable { + final String userId; + final XFile image; + + UpdateProfileParams({required this.image, required this.userId}); + + @override + List get props => [userId, image]; +} \ No newline at end of file diff --git a/aait/mobile/group-3/blog_app/lib/features/profile/presentation/bloc/profile_bloc.dart b/aait/mobile/group-3/blog_app/lib/features/profile/presentation/bloc/profile_bloc.dart index d1e068607..5ebe97d0e 100644 --- a/aait/mobile/group-3/blog_app/lib/features/profile/presentation/bloc/profile_bloc.dart +++ b/aait/mobile/group-3/blog_app/lib/features/profile/presentation/bloc/profile_bloc.dart @@ -52,7 +52,7 @@ class ProfileBloc extends Bloc { final _state = state as ProfileLoaded; if (event.imageFile != null) { emit(ProfileLoading(message: "Updating Profile Picture...")); - final result = await updateProfilePicture(Params(event.imageFile)); + final result = await updateProfilePicture(UpdateProfileParams(image: event.imageFile!, userId: _state.profile.id)); result.fold( (failure) => emit(ProfileError()), (profile) => emit(ProfileLoaded(