-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: add news page #28
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:rtu_mirea_app/common/errors/exceptions.dart'; | ||
import 'package:rtu_mirea_app/data/models/news_item_model.dart'; | ||
import 'dart:convert'; | ||
|
||
abstract class NewsRemoteData { | ||
Future<List<NewsItemModel>> getNews(int offset, int limit, [String? tag]); | ||
Future<List<String>> getTags(); | ||
} | ||
|
||
class NewsRemoteDataImpl extends NewsRemoteData { | ||
static const _API_BASE_URL = "http://schedule.mirea.ninja:5050"; | ||
|
||
final Dio httpClient; | ||
|
||
NewsRemoteDataImpl({required this.httpClient}); | ||
|
||
@override | ||
Future<List<NewsItemModel>> getNews(int offset, int limit, | ||
[String? tag]) async { | ||
try { | ||
final response = await httpClient.get( | ||
_API_BASE_URL + '/news' + '?tag=$tag&limit=$limit&offset=$offset'); | ||
|
||
if (response.statusCode == 200) { | ||
Map responseBody = response.data; | ||
return responseBody["news"] | ||
.map<NewsItemModel>((newsItem) => NewsItemModel.fromJson(newsItem)) | ||
.toList(); | ||
} else { | ||
throw ServerException('Response status code is $response.statusCode'); | ||
} | ||
} catch (e) { | ||
throw ServerException(e.toString()); | ||
} | ||
} | ||
|
||
@override | ||
Future<List<String>> getTags() async { | ||
try { | ||
final response = await httpClient.get(_API_BASE_URL + '/tags'); | ||
|
||
if (response.statusCode == 200) { | ||
Map responseBody = response.data; | ||
|
||
List<String> tags = []; | ||
tags = List<String>.from(responseBody["tags"].map((x) => x['name'])); | ||
return tags; | ||
} else { | ||
throw ServerException('Response status code is $response.statusCode'); | ||
} | ||
} catch (e) { | ||
throw ServerException(e.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:rtu_mirea_app/domain/entities/news_item.dart'; | ||
|
||
class NewsItemModel extends NewsItem { | ||
NewsItemModel({ | ||
required title, | ||
required text, | ||
required date, | ||
required images, | ||
required tags, | ||
}) : super( | ||
title: title, | ||
text: text, | ||
date: date, | ||
images: images, | ||
tags: tags, | ||
); | ||
|
||
factory NewsItemModel.fromJson(Map<String, dynamic> json) { | ||
return NewsItemModel( | ||
title: json['title'], | ||
text: json['text'], | ||
date: DateTime.parse(json['date']), | ||
images: List<String>.from(json["images"].map((x) => x['name'])), | ||
tags: List<String>.from(json["tags"].map((x) => x['name'])), | ||
); | ||
} | ||
|
||
@override | ||
List<Object?> get props => [title, text, date, images, tags]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:internet_connection_checker/internet_connection_checker.dart'; | ||
import 'package:rtu_mirea_app/common/errors/exceptions.dart'; | ||
import 'package:rtu_mirea_app/common/errors/failures.dart'; | ||
import 'package:rtu_mirea_app/data/datasources/news_remote.dart'; | ||
import 'package:rtu_mirea_app/domain/entities/news_item.dart'; | ||
import 'package:rtu_mirea_app/domain/repositories/news_repository.dart'; | ||
|
||
class NewsRepositoryImpl implements NewsRepository { | ||
final NewsRemoteData remoteDataSource; | ||
final InternetConnectionChecker connectionChecker; | ||
|
||
NewsRepositoryImpl({ | ||
required this.remoteDataSource, | ||
required this.connectionChecker, | ||
}); | ||
|
||
@override | ||
Future<Either<Failure, List<NewsItem>>> getNews(int offset, int limit, | ||
[String? tag]) async { | ||
if (await connectionChecker.hasConnection) { | ||
try { | ||
final newsList = await remoteDataSource.getNews(offset, limit, tag); | ||
return Right(newsList); | ||
} on ServerException { | ||
return Left(ServerFailure()); | ||
} | ||
} else { | ||
return Left(ServerFailure()); | ||
} | ||
} | ||
|
||
@override | ||
Future<Either<Failure, List<String>>> getTags() async { | ||
if (await connectionChecker.hasConnection) { | ||
try { | ||
final tagsList = await remoteDataSource.getTags(); | ||
return Right(tagsList); | ||
} on ServerException { | ||
return Left(ServerFailure()); | ||
} | ||
} else { | ||
return Left(ServerFailure()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
class NewsItem extends Equatable { | ||
final String title; | ||
final String text; | ||
final DateTime date; | ||
final List<String> images; | ||
final List<String> tags; | ||
|
||
NewsItem({ | ||
required this.title, | ||
required this.text, | ||
required this.date, | ||
required this.images, | ||
required this.tags, | ||
}); | ||
|
||
@override | ||
List<Object?> get props => [title, text, date, images, tags]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:rtu_mirea_app/common/errors/failures.dart'; | ||
import 'package:rtu_mirea_app/domain/entities/news_item.dart'; | ||
|
||
abstract class NewsRepository { | ||
Future<Either<Failure, List<NewsItem>>> getNews( | ||
int offset, int limit, String tag); | ||
Future<Either<Failure, List<String>>> getTags(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:rtu_mirea_app/common/errors/failures.dart'; | ||
import 'package:rtu_mirea_app/domain/entities/news_item.dart'; | ||
import 'package:rtu_mirea_app/domain/repositories/news_repository.dart'; | ||
import 'package:rtu_mirea_app/domain/usecases/usecase.dart'; | ||
|
||
class GetNews extends UseCase<List<NewsItem>, GetNewsParams> { | ||
final NewsRepository newsRepository; | ||
|
||
GetNews(this.newsRepository); | ||
|
||
@override | ||
Future<Either<Failure, List<NewsItem>>> call(GetNewsParams params) async { | ||
return await newsRepository.getNews( | ||
params.offset, params.limit, params.tag ?? 'все'); | ||
} | ||
} | ||
|
||
class GetNewsParams extends Equatable { | ||
final int offset; | ||
final int limit; | ||
final String? tag; | ||
|
||
GetNewsParams({required this.offset, required this.limit, this.tag}); | ||
|
||
@override | ||
List<Object?> get props => [offset, limit, tag]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:rtu_mirea_app/common/errors/failures.dart'; | ||
import 'package:rtu_mirea_app/domain/repositories/news_repository.dart'; | ||
import 'package:rtu_mirea_app/domain/usecases/usecase.dart'; | ||
|
||
class GetNewsTags extends UseCase<List<String>, void> { | ||
final NewsRepository newsRepository; | ||
|
||
GetNewsTags(this.newsRepository); | ||
|
||
@override | ||
Future<Either<Failure, List<String>>> call([_]) async { | ||
return await newsRepository.getTags(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:rtu_mirea_app/domain/entities/news_item.dart'; | ||
import 'package:rtu_mirea_app/domain/usecases/get_news.dart'; | ||
import 'package:rtu_mirea_app/domain/usecases/get_news_tags.dart'; | ||
|
||
part 'news_event.dart'; | ||
part 'news_state.dart'; | ||
|
||
class NewsBloc extends Bloc<NewsEvent, NewsState> { | ||
NewsBloc({ | ||
required this.getNews, | ||
required this.getNewsTags, | ||
}) : super(NewsInitial()); | ||
|
||
final GetNews getNews; | ||
final GetNewsTags getNewsTags; | ||
|
||
bool _isFirstFetch = true; | ||
int _offset = 0; | ||
|
||
@override | ||
Stream<NewsState> mapEventToState( | ||
NewsEvent event, | ||
) async* { | ||
if (event is NewsLoadEvent) { | ||
List<String> tagsList = []; | ||
List<NewsItem> oldNews = []; | ||
|
||
bool hasFetchError = false; | ||
|
||
if (_isFirstFetch) { | ||
yield NewsLoading(oldNews: oldNews, isFirstFetch: true); | ||
_isFirstFetch = false; | ||
final tags = await getNewsTags(); | ||
tags.fold((failure) { | ||
hasFetchError = true; | ||
}, (r) { | ||
tagsList = r; | ||
}); | ||
} else { | ||
if (state is NewsLoaded) { | ||
tagsList = (state as NewsLoaded).tags; | ||
oldNews = (state as NewsLoaded).news; | ||
} | ||
yield NewsLoading(oldNews: oldNews, isFirstFetch: false); | ||
} | ||
|
||
if (hasFetchError) { | ||
yield NewsLoadError(); | ||
return; | ||
} | ||
|
||
final news = await getNews(GetNewsParams(offset: _offset, limit: 10)); | ||
yield news.fold((failure) => NewsLoadError(), (r) { | ||
_offset += r.length - 1; | ||
List<NewsItem> newNews = List.from(oldNews)..addAll(r); | ||
return NewsLoaded(news: newNews, tags: tagsList); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
part of 'news_bloc.dart'; | ||
|
||
abstract class NewsEvent extends Equatable { | ||
const NewsEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class NewsLoadTagsEvent extends NewsEvent {} | ||
|
||
class NewsLoadEvent extends NewsEvent {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Необходимо добавить внедрение зависимостей в bloc.