Skip to content

Commit

Permalink
trend store
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Aug 8, 2018
1 parent 5b9fc26 commit b885e0e
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 60 deletions.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PODS:
- device_info (0.0.1):
- Flutter
- Flutter (1.0.0)
- flutter_statusbar (0.0.1):
- Flutter
- flutter_webview_plugin (0.0.1):
- Flutter
- fluttertoast (0.0.2):
Expand All @@ -31,6 +33,7 @@ DEPENDENCIES:
- connectivity (from `.symlinks/plugins/connectivity/ios`)
- device_info (from `.symlinks/plugins/device_info/ios`)
- Flutter (from `.symlinks/flutter/ios`)
- flutter_statusbar (from `.symlinks/plugins/flutter_statusbar/ios`)
- flutter_webview_plugin (from `.symlinks/plugins/flutter_webview_plugin/ios`)
- fluttertoast (from `.symlinks/plugins/fluttertoast/ios`)
- get_version (from `.symlinks/plugins/get_version/ios`)
Expand All @@ -52,6 +55,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/device_info/ios"
Flutter:
:path: ".symlinks/flutter/ios"
flutter_statusbar:
:path: ".symlinks/plugins/flutter_statusbar/ios"
flutter_webview_plugin:
:path: ".symlinks/plugins/flutter_webview_plugin/ios"
fluttertoast:
Expand All @@ -73,6 +78,7 @@ SPEC CHECKSUMS:
connectivity: d5992e3758d68b1c4b12d6e012fe0813f1aa1645
device_info: 76ce0b32e13034d1883be4a382433648f9dcee63
Flutter: 9d0fac939486c9aba2809b7982dfdbb47a7b0296
flutter_statusbar: a563eb9416256f17660646aceb2314b113127da9
flutter_webview_plugin: 116575b48572029304775b768e9f15ebfc316274
fluttertoast: ad0527b15297baf11349f3f581dae4d40f24f340
FMDB: 6198a90e7b6900cfc046e6bc0ef6ebb7be9236aa
Expand Down
2 changes: 2 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
"${BUILT_PRODUCTS_DIR}/Reachability/Reachability.framework",
"${BUILT_PRODUCTS_DIR}/connectivity/connectivity.framework",
"${BUILT_PRODUCTS_DIR}/device_info/device_info.framework",
"${BUILT_PRODUCTS_DIR}/flutter_statusbar/flutter_statusbar.framework",
"${BUILT_PRODUCTS_DIR}/flutter_webview_plugin/flutter_webview_plugin.framework",
"${BUILT_PRODUCTS_DIR}/fluttertoast/fluttertoast.framework",
"${BUILT_PRODUCTS_DIR}/get_version/get_version.framework",
Expand All @@ -247,6 +248,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Reachability.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/connectivity.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_statusbar.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_webview_plugin.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/fluttertoast.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/get_version.framework",
Expand Down
52 changes: 23 additions & 29 deletions lib/common/dao/ReposDao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import 'package:gsy_github_app_flutter/common/model/User.dart';
import 'package:gsy_github_app_flutter/common/net/Address.dart';
import 'package:gsy_github_app_flutter/common/net/Api.dart';
import 'package:gsy_github_app_flutter/common/net/trending/GithubTrending.dart';
import 'package:gsy_github_app_flutter/common/redux/TrendRedux.dart';
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:redux/redux.dart';

/**
* Created by guoshuyu
Expand All @@ -45,41 +47,33 @@ class ReposDao {
* @param since 数据时长, 本日,本周,本月
* @param languageType 语言
*/
static getTrendDao({since = 'daily', languageType, page = 0, needDb = true}) async {
static getTrendDao(Store store, {since = 'daily', languageType, page = 0, needDb = true}) async {
TrendRepositoryDbProvider provider = new TrendRepositoryDbProvider();
String languageTypeDb = languageType ?? "*";
await provider.getData(languageTypeDb, since);
next() async {
String url = Address.trending(since, languageType);
var res = await new GitHubTrending().fetchTrending(url);
if (res != null && res.result && res.data.length > 0) {
List<TrendingRepoModel> list = new List();
var data = res.data;
if (data == null || data.length == 0) {
return new DataResult(null, false);
}
if (needDb) {
provider.insert(languageTypeDb, since, json.encode(data));
}
for (int i = 0; i < data.length; i++) {
TrendingRepoModel model = data[i];
list.add(model);
}
return new DataResult(list, true);
} else {
List<TrendingRepoModel> list = await provider.getData(languageTypeDb, since);
if (list != null && list.length > 0) {
store.dispatch(new RefreshTrendAction(list));
}
String url = Address.trending(since, languageType);
var res = await new GitHubTrending().fetchTrending(url);
if (res != null && res.result && res.data.length > 0) {
List<TrendingRepoModel> list = new List();
var data = res.data;
if (data == null || data.length == 0) {
return new DataResult(null, false);
}
}

if (needDb) {
List<TrendingRepoModel> list = await provider.getData(languageTypeDb, since);
if (list == null) {
return await next();
if (needDb) {
provider.insert(languageTypeDb, since, json.encode(data));
}
DataResult dataResult = new DataResult(list, true, next: next());
return dataResult;
for (int i = 0; i < data.length; i++) {
TrendingRepoModel model = data[i];
list.add(model);
}
store.dispatch(new RefreshTrendAction(list));
return new DataResult(list, true);
} else {
return new DataResult(null, false);
}
return await next();
}

/**
Expand Down
16 changes: 13 additions & 3 deletions lib/common/redux/GSYState.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import 'package:gsy_github_app_flutter/common/model/Event.dart';
import 'package:gsy_github_app_flutter/common/model/TrendingRepoModel.dart';
import 'package:gsy_github_app_flutter/common/model/User.dart';
import 'package:gsy_github_app_flutter/common/redux/UserRedux.dart';
import 'package:gsy_github_app_flutter/common/redux/EventRedux.dart';
import 'package:gsy_github_app_flutter/common/redux/TrendRedux.dart';

/**
* Redux全局State
* Created by guoshuyu
* Date: 2018-07-16
*/

///全局Redux store 的对象,保存State数据
class GSYState {
///用户信息
User userInfo;

///用户接受到的事件列表
List<Event> eventList = new List();
///构造方法
GSYState({this.userInfo, this.eventList});

///用户接受到的事件列表
List<TrendingRepoModel> trendList = new List();

///构造方法
GSYState({this.userInfo, this.eventList, this.trendList});
}

///通过 Reducer 创建 store 保存的 GSYState
GSYState appReducer(GSYState state, action) {
return GSYState(
///通过 UserReducer 将 GSYState 内的 userInfo 和 action 关联在一起
userInfo: UserReducer(state.userInfo, action),

///通过 EventReducer 将 GSYState 内的 eventList 和 action 关联在一起
eventList: EventReducer(state.eventList, action),

///通过 TrendReducer 将 GSYState 内的 trendList 和 action 关联在一起
trendList: TrendReducer(state.trendList, action),
);
}

28 changes: 28 additions & 0 deletions lib/common/redux/TrendRedux.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:gsy_github_app_flutter/common/model/TrendingRepoModel.dart';
import 'package:redux/redux.dart';

/**
* 事件Redux
* Created by guoshuyu
* Date: 2018-07-16
*/

final TrendReducer = combineReducers<List<TrendingRepoModel>>([
TypedReducer<List<TrendingRepoModel>, RefreshTrendAction>(_refresh),
]);

List<TrendingRepoModel> _refresh(List<TrendingRepoModel> list, action) {
list.clear();
if (action.list == null) {
return list;
} else {
list.addAll(action.list);
return list;
}
}

class RefreshTrendAction {
final List<TrendingRepoModel> list;

RefreshTrendAction(this.list);
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FlutterReduxApp extends StatelessWidget {

/// 创建Store,引用 GSYState 中的 appReducer 创建 Reducer
/// initialState 初始化 State
final store = new Store<GSYState>(appReducer, initialState: new GSYState(userInfo: User.empty(), eventList: new List()));
final store = new Store<GSYState>(appReducer, initialState: new GSYState(userInfo: User.empty(), eventList: new List(), trendList: new List()));

FlutterReduxApp({Key key}) : super(key: key);

Expand Down
84 changes: 58 additions & 26 deletions lib/page/TrendPage.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:gsy_github_app_flutter/common/dao/ReposDao.dart';
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
import 'package:gsy_github_app_flutter/common/utils/NavigatorUtils.dart';
import 'package:gsy_github_app_flutter/widget/GSYCardItem.dart';
import 'package:gsy_github_app_flutter/widget/GSYListState.dart';
import 'package:gsy_github_app_flutter/widget/GSYPullLoadWidget.dart';
import 'package:gsy_github_app_flutter/widget/ReposItem.dart';
import 'package:redux/redux.dart';

/**
* 主页趋势tab页
Expand All @@ -18,8 +23,8 @@ class TrendPage extends StatefulWidget {
}

class _TrendPageState extends GSYListState<TrendPage> {
TrendTypeModel selectTime = TrendTime[0];
TrendTypeModel selectType = TrendType[0];
TrendTypeModel selectTime = trendTime[0];
TrendTypeModel selectType = trendType[0];

_renderItem(e) {
ReposViewModel reposViewModel = ReposViewModel.fromTrendMap(e);
Expand All @@ -39,14 +44,14 @@ class _TrendPageState extends GSYListState<TrendPage> {
padding: new EdgeInsets.only(left: 0.0, top: 5.0, right: 0.0, bottom: 5.0),
child: new Row(
children: <Widget>[
_renderHeaderPopItem(selectTime.name, TrendTime, (TrendTypeModel result) {
_renderHeaderPopItem(selectTime.name, trendTime, (TrendTypeModel result) {
setState(() {
selectTime = result;
});
showRefreshLoading();
}),
new Container(height: 10.0, width: 0.5, color: Colors.white),
_renderHeaderPopItem(selectType.name, TrendType, (TrendTypeModel result) {
_renderHeaderPopItem(selectType.name, trendType, (TrendTypeModel result) {
setState(() {
selectType = result;
});
Expand Down Expand Up @@ -81,9 +86,24 @@ class _TrendPageState extends GSYListState<TrendPage> {
return list;
}

@override
Future<Null> handleRefresh() async {
if (isLoading) {
return null;
}
isLoading = true;
page = 1;
await ReposDao.getTrendDao(_getStore(), since: selectTime.value, languageType: selectType.value);
setState(() {
pullLoadWidgetControl.needLoadMore = false;
});
isLoading = false;
return null;
}

@override
requestRefresh() async {
return await ReposDao.getTrendDao(since: selectTime.value, languageType: selectType.value);
return null;
}

@override
Expand All @@ -92,11 +112,15 @@ class _TrendPageState extends GSYListState<TrendPage> {
}

@override
bool get isRefreshFirst => true;
bool get isRefreshFirst => false;

@override
void initState() {
super.initState();
void didChangeDependencies() {
pullLoadWidgetControl.dataList = _getStore().state.trendList;
if (pullLoadWidgetControl.dataList.length == 0) {
showRefreshLoading();
}
super.didChangeDependencies();
}

@override
Expand All @@ -105,25 +129,33 @@ class _TrendPageState extends GSYListState<TrendPage> {
clearData();
}

Store<GSYState> _getStore() {
return StoreProvider.of(context);
}

@override
Widget build(BuildContext context) {
super.build(context); // See AutomaticKeepAliveClientMixin.
return new Scaffold(
backgroundColor: Color(GSYColors.mainBackgroundColor),
appBar: new AppBar(
flexibleSpace: _renderHeader(),
backgroundColor: Color(GSYColors.mainBackgroundColor),
leading: new Container(),
elevation: 0.0,
),
body: GSYPullLoadWidget(
pullLoadWidgetControl,
(BuildContext context, int index) => _renderItem(pullLoadWidgetControl.dataList[index]),
handleRefresh,
onLoadMore,
refreshKey: refreshIndicatorKey,
),
);
return new StoreBuilder<GSYState>(
builder: (context, store) {
return new Scaffold(
backgroundColor: Color(GSYColors.mainBackgroundColor),
appBar: new AppBar(
flexibleSpace: _renderHeader(),
backgroundColor: Color(GSYColors.mainBackgroundColor),
leading: new Container(),
elevation: 0.0,
),
body: GSYPullLoadWidget(
pullLoadWidgetControl,
(BuildContext context, int index) => _renderItem(pullLoadWidgetControl.dataList[index]),
handleRefresh,
onLoadMore,
refreshKey: refreshIndicatorKey,
),
);
},
);;
}
}

Expand All @@ -134,13 +166,13 @@ class TrendTypeModel {
TrendTypeModel(this.name, this.value);
}

var TrendTime = [
var trendTime = [
TrendTypeModel(GSYStrings.trend_day, "daily"),
TrendTypeModel(GSYStrings.trend_week, "weekly"),
TrendTypeModel(GSYStrings.trend_month, "monthly"),
];

var TrendType = [
var trendType = [
TrendTypeModel(GSYStrings.trend_all, null),
TrendTypeModel("Java", "Java"),
TrendTypeModel("Kotlin", "Kotlin"),
Expand Down
3 changes: 2 additions & 1 deletion lib/widget/UserHeader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ class UserHeaderItem extends StatelessWidget {
padding: EdgeInsets.only(left: 10.0, right: 10.0),
width: width,
height: height,
///svg chart
child: new SvgPicture.network(
CommonUtils.getUserChartAddress(userInfo.login),
width: width,
height: height - 10,
allowDrawingOutsideViewBox: true,
placeholderBuilder: (BuildContext context) => new Container(
height: height / 2,
height: height,
width: width,
child: Center(
child: const SpinKitRipple (color: Color(GSYColors.primaryValue)),
Expand Down

0 comments on commit b885e0e

Please sign in to comment.