forked from Sky24n/flutter_wanandroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
thl
committed
Jun 11, 2019
1 parent
777f6e7
commit 321bb14
Showing
30 changed files
with
867 additions
and
544 deletions.
There are no files selected for viewing
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,77 @@ | ||
import 'dart:collection'; | ||
|
||
import 'package:flutter_wanandroid/blocs/bloc_provider.dart'; | ||
import 'package:flutter_wanandroid/data/protocol/models.dart'; | ||
import 'package:flutter_wanandroid/data/repository/collect_repository.dart'; | ||
import 'package:flutter_wanandroid/event/event.dart'; | ||
import 'package:flutter_wanandroid/utils/util_index.dart'; | ||
import 'package:pull_to_refresh/pull_to_refresh.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
|
||
class CollectBloc implements BlocBase { | ||
BehaviorSubject<List<ReposModel>> _collectListBs = | ||
BehaviorSubject<List<ReposModel>>(); | ||
|
||
Sink<List<ReposModel>> get _collectListSink => _collectListBs.sink; | ||
|
||
Stream<List<ReposModel>> get collectListStream => | ||
_collectListBs.stream.asBroadcastStream(); | ||
|
||
List<ReposModel> _collectList; | ||
int _collectPage = 0; | ||
|
||
CollectRepository _collectRepository = new CollectRepository(); | ||
|
||
@override | ||
Future getData({String labelId, int page}) { | ||
return getCollectList(labelId, page); | ||
} | ||
|
||
@override | ||
Future onLoadMore({String labelId}) { | ||
int _page = ++_collectPage; | ||
return getData(labelId: labelId, page: _page); | ||
} | ||
|
||
@override | ||
Future onRefresh({String labelId, bool isReload}) { | ||
LogUtil.e("CollectBloc onRefresh...... $labelId"); | ||
_collectPage = 0; | ||
if (isReload == true) { | ||
_collectListSink.add(null); | ||
} | ||
return getData(labelId: labelId, page: _collectPage); | ||
} | ||
|
||
Future getCollectList(String labelId, int page) { | ||
return _collectRepository.getCollectList(page).then((list) { | ||
if (_collectList == null) { | ||
_collectList = new List(); | ||
} | ||
if (page == 0) { | ||
_collectList.clear(); | ||
} | ||
_collectList.addAll(list); | ||
_collectListSink.add(UnmodifiableListView<ReposModel>(_collectList)); | ||
_homeEventSink?.add(new StatusEvent( | ||
labelId, | ||
ObjectUtil.isEmpty(list) | ||
? RefreshStatus.noMore | ||
: RefreshStatus.idle)); | ||
}).catchError((_) { | ||
if (ObjectUtil.isEmpty(_collectList)) { | ||
_collectListBs.sink.addError("error"); | ||
} | ||
_collectPage--; | ||
_homeEventSink?.add(new StatusEvent(labelId, RefreshStatus.failed)); | ||
}); | ||
} | ||
|
||
Sink<StatusEvent> _homeEventSink; | ||
|
||
|
||
@override | ||
void dispose() { | ||
_collectListBs.close(); | ||
} | ||
} |
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
Oops, something went wrong.