Skip to content

Commit bfe5696

Browse files
committed
1. 重构福利,不得姐页面
2. 为configStore添加网络判断的方法 3. 解决webView页闪退的bug 4. 优化代码
1 parent cc06a76 commit bfe5696

File tree

11 files changed

+114
-110
lines changed

11 files changed

+114
-110
lines changed

app/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import React, { useEffect } from 'react';
77
import { View, ActivityIndicator, DeviceEventEmitter, BackHandler, ToastAndroid } from 'react-native';
88

99
import { AuthLoadingRouter } from './routers/AuthLoading';
10-
// import {SafeAreaView} from 'react-navigation';
11-
import { Toast } from './components';
12-
import { Provider } from 'mobx-react';
1310
import * as rootStore from './store/RootStore';
1411

1512
import SplashScreen from 'react-native-splash-screen';

app/mobx/News/BuDeJieMobx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class BuDeJieMobx extends ConfigStore {
183183
});
184184
}
185185

186-
// console.log('this.largeListData.slice', this.largeListData.slice());
186+
console.log('this.largeListData.slice', this.largeListData.slice());
187187
} catch (e) {
188188
this.showErrorView(e.message);
189189
console.log('e', e.message);

app/pages/News/BuDeJie/Components/BaseItem.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import { View, StyleSheet } from 'react-native';
88

99
import { RTBDJList, ToolBar, UserInfo, Joke, Picture } from '../../../../servers/News/interfaces';
1010
import { ContainerItem } from './Items/ContainerItem';
11-
import type { NavigationState } from 'react-navigation';
1211

1312
import { UserInfoView } from './Views/UserInfoView';
1413
import { ToolBarView } from './Views/ToolBarView';
1514

1615
type Props = {
1716
itemData: RTBDJList,
18-
navigate: NavigationState,
1917
itemPress: Function,
2018
picturePress: Function,
2119
videoPress: Function

app/pages/News/BuDeJie/index.js

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
* Created by Rabbit on 2019-03-05.
44
*/
55

6-
import React from 'react';
7-
import { StyleSheet, Text, View, Image, FlatList, Modal, ActivityIndicator } from 'react-native';
6+
import React, { useEffect, useRef } from 'react';
7+
import { StyleSheet } from 'react-native';
88

9-
import { TableList } from '../../../components';
109
import BaseContainer from '../../../components/BaseContainer';
1110

1211
import type { RTBDJList, RTWeal } from '../../../servers/News/interfaces';
1312
import { BuDeJieMobx } from '../../../mobx/News/BuDeJieMobx';
14-
import { inject, observer } from 'mobx-react';
13+
import { observer, useObservable } from 'mobx-react-lite';
1514
import { BaseItem } from './Components/BaseItem';
1615
import type { NavigationState } from 'react-navigation';
1716
import { ActionSheet, Overlay } from 'teaset';
@@ -21,59 +20,55 @@ import { Picture } from '../../../servers/News/interfaces';
2120
import PlaceholderView from './Components/Views/PlaceholderView';
2221
import type { RTBuDeJieType } from '../../../servers/News';
2322

24-
import { LargeList } from 'react-native-largelist-v3';
23+
import { LargeList, WaterfallList } from 'react-native-largelist-v3';
2524
import { System } from '../../../utils';
2625
import { PublicStore } from '../../../store/PublicStore';
26+
import { ConfigStore } from '../../../store/ConfigStore';
2727
// import { ChineseWithLastDateFooter } from 'react-native-spring-scrollview/Customize';
2828

2929
type Props = {
3030
type: RTBuDeJieType | string,
31-
navigate: NavigationState,
32-
publicStore: PublicStore
31+
navigation: NavigationState,
32+
publicStore: PublicStore,
33+
configStore: ConfigStore
3334
};
3435

35-
@inject('publicStore')
36-
@observer
37-
class BuDeJie extends React.Component<Props, any> {
38-
buDeJieMobx: BuDeJieMobx;
39-
customPopView: any;
40-
_list: LargeList;
41-
42-
constructor(props: Props) {
43-
super(props);
44-
this.buDeJieMobx = new BuDeJieMobx();
45-
}
36+
function actionSheetToSaveImage(url: string, props: Props) {
37+
const { saveImageWithIOS, saveImageWithAndroid } = props.publicStore;
38+
const items = [
39+
{
40+
title: '保存图片',
41+
onPress: () => (System.iOS ? saveImageWithIOS(url) : saveImageWithAndroid(url))
42+
}
43+
];
44+
const cancelItem = { title: '取消' };
45+
ActionSheet.show(items, cancelItem);
46+
}
47+
const BuDeJie = observer(function(props: Props) {
48+
const { navigation, type } = props;
49+
const buDeJieMobx = useObservable(new BuDeJieMobx());
50+
const { largeListData, maxtime = '', fetchBuDeJieData } = buDeJieMobx;
4651

47-
componentDidMount = async () => {
48-
const { maxtime } = this.buDeJieMobx;
49-
await this.buDeJieMobx.fetchBuDeJieData(this.props.type, maxtime);
50-
};
52+
const waterfallRef: WaterfallList = useRef();
53+
const customOverlayRef: Overlay = useRef();
5154

52-
actionSheetToSaveImage = (url: string) => {
53-
const { saveImageWithIOS, saveImageWithAndroid } = this.props.publicStore;
54-
const items = [
55-
{
56-
title: '保存图片',
57-
onPress: () => (System.iOS ? saveImageWithIOS(url) : saveImageWithAndroid(url))
58-
}
59-
];
60-
const cancelItem = { title: '取消' };
61-
ActionSheet.show(items, cancelItem);
62-
};
55+
useEffect(() => {
56+
fetchBuDeJieData(type, '');
57+
}, []);
6358

64-
picturePress = (item: Picture | any) => {
59+
function picturePress(item: Picture, props: Props) {
6560
if (item.isLongPicture || !item.is_gif) {
66-
this.props.navigate('WebView', { uri: item.weixin_url });
61+
navigation.navigate('WebView', { uri: item.weixin_url });
6762
} else {
6863
const overlayView = (
6964
<Overlay.PopView
7065
style={{ alignItems: 'center', justifyContent: 'center' }}
7166
overlayOpacity={1}
72-
ref={v => (this.customPopView = v)}
67+
ref={customOverlayRef}
7368
>
7469
<Button
75-
onPress={() => this.customPopView && this.customPopView.close()}
76-
onLongPress={() => this.actionSheetToSaveImage(item.cdn_img)}
70+
onPress={() => customOverlayRef && customOverlayRef.current.close()}
71+
onLongPress={() => actionSheetToSaveImage(item.cdn_img, props)}
7772
>
7873
<CustomImage
7974
source={{ uri: item.cdn_img }}
@@ -89,56 +84,50 @@ class BuDeJie extends React.Component<Props, any> {
8984
);
9085
Overlay.show(overlayView);
9186
}
92-
};
93-
94-
videoPress = (item: Picture) => {
95-
this.props.navigate('WebView', { uri: item.weixin_url });
96-
};
87+
}
9788

98-
renderItem = ({ section, row }: { section: number, row: number }) => {
99-
const { navigate } = this.props;
100-
const { largeListData } = this.buDeJieMobx;
89+
function videoPress(item: Picture) {
90+
navigation.navigate('WebView', { uri: item.weixin_url });
91+
}
10192

93+
function renderItem({ section, row }: { section: number, row: number }) {
10294
const item = largeListData[section].items[row];
103-
// console.log('item-----', item);
10495
return (
10596
<BaseItem
10697
itemData={item}
107-
navigate={navigate}
10898
itemPress={() => {
10999
alert(item.text);
110100
}}
111-
picturePress={() => this.picturePress(item)}
112-
videoPress={() => this.videoPress(item)}
101+
picturePress={() => picturePress(item, props)}
102+
videoPress={() => videoPress(item)}
113103
/>
114104
);
115-
};
105+
}
116106

117-
render() {
118-
const { largeListData, maxtime } = this.buDeJieMobx;
119-
return (
107+
return (
108+
<BaseContainer isHiddenNavBar={true} isTopNavigator={true} store={buDeJieMobx}>
120109
<LargeList
121110
style={styles.container}
122111
data={largeListData}
123-
ref={ref => (this._list = ref)}
112+
ref={waterfallRef}
124113
heightForIndexPath={({ section, row }: { section: number, row: number }) => {
125114
const item: RTBDJList = largeListData[section].items[row];
126115
return item.itemHeight;
127116
}}
128-
renderIndexPath={this.renderItem}
117+
renderIndexPath={renderItem}
129118
onRefresh={async () => {
130-
await this.buDeJieMobx.fetchBuDeJieData(this.props.type, '');
131-
this._list.endRefresh();
119+
await fetchBuDeJieData(type, '');
120+
waterfallRef.current.endRefresh();
132121
}}
133122
// loadingFooter={ChineseWithLastDateFooter}
134123
onLoading={async () => {
135-
await this.buDeJieMobx.fetchBuDeJieData(this.props.type, maxtime);
136-
this._list.endLoading();
124+
await fetchBuDeJieData(type, maxtime);
125+
waterfallRef.current.endLoading();
137126
}}
138127
/>
139-
);
140-
}
141-
}
128+
</BaseContainer>
129+
);
130+
});
142131

143132
const styles = StyleSheet.create({
144133
container: {

app/pages/News/News.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,35 @@ import { observer } from 'mobx-react-lite';
1313
import ScrollableTabView, { DefaultTabBar } from 'react-native-scrollable-tab-view';
1414

1515
import { Welfare } from './Welfare';
16-
// import { BuDeJie } from './BuDeJie';
1716
import { BuDeJie } from './BuDeJie';
18-
import { BuDeJieDetail } from './BuDeJieDetail';
17+
// import { BuDeJieDetail } from './BuDeJieDetail';
1918

2019
import type { NavigationState } from 'react-navigation';
2120
import type { RTBuDeJieType } from '../../servers/News';
2221
import { StoreContext } from '../../utils/Tool';
22+
import { BaseItem } from './BuDeJie/Components/BaseItem';
23+
import { BuDeJieMobx } from '../../mobx/News';
2324

2425
type typeItem = {
2526
title: string,
2627
type: RTBuDeJieType | string
2728
};
2829

2930
const typeArr: Array<typeItem> = [
30-
{ title: '福利', type: '福利' },
31-
{ title: '笑话', type: 29 },
32-
{ title: '图片', type: 10 },
3331
{ title: '全部', type: 1 },
34-
{ title: '视频', type: 41 }
32+
{ title: '视频', type: 41 },
33+
{ title: '图片', type: 10 },
34+
{ title: '笑话', type: 29 },
35+
{ title: '福利', type: '福利' }
3536
];
3637

3738
const News = observer(function(props) {
39+
3840
const store = useContext(StoreContext);
3941
const { configStore, publicStore, shiTuStore } = store;
40-
const { navigate } = props.navigation;
42+
const { navigation } = props;
4143

42-
console.log('store', store);
44+
// console.log('store', store);
4345

4446
return (
4547
<BaseContainer title={'百思不得姐'} isTopNavigator={true}>
@@ -53,12 +55,11 @@ const News = observer(function(props) {
5355
tabBarTextStyle={{ fontSize: 15 }}
5456
>
5557
{typeArr.map((item, i) => {
56-
if (i === 0) {
58+
if (i === 4) {
5759
return (
5860
<Welfare
5961
tabLabel={item.title}
6062
key={i}
61-
navigate={navigate}
6263
configStore={configStore}
6364
publicStore={publicStore}
6465
shiTuStore={shiTuStore}
@@ -70,7 +71,8 @@ const News = observer(function(props) {
7071
type={item.type}
7172
tabLabel={item.title}
7273
key={i}
73-
navigate={navigate}
74+
navigation={navigation}
75+
configStore={configStore}
7476
publicStore={publicStore}
7577
/>
7678
);
@@ -81,4 +83,4 @@ const News = observer(function(props) {
8183
);
8284
});
8385

84-
export { News, Welfare, BuDeJie, BuDeJieDetail };
86+
export { News, Welfare, BuDeJie };

app/pages/News/Welfare/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type Props = {
3131

3232
const Welfare = observer(function(props: Props) {
3333
const { dataSource, isRefreshing, loadWelfareData } = welfareMobx;
34-
const scrollViewRef: WaterfallList = useRef();
35-
const customPopView: Overlay = useRef();
34+
const waterfallRef: WaterfallList = useRef();
35+
const customOverlayRef: Overlay = useRef();
3636
const { publicStore, configStore, shiTuStore } = props;
3737

3838
useEffect(() => {
@@ -65,11 +65,11 @@ const Welfare = observer(function(props: Props) {
6565
style={{ alignItems: 'center', justifyContent: 'center' }}
6666
overlayOpacity={1}
6767
type="zoomOut"
68-
ref={customPopView}
68+
ref={customOverlayRef}
6969
>
7070
<Button
7171
onLongPress={() => actionSheetToSaveImage(item)}
72-
onPress={() => customPopView && customPopView.current.close()}
72+
onPress={() => customOverlayRef && customOverlayRef.current.close()}
7373
>
7474
<CustomImage
7575
source={{ uri: item.largeUrl }}
@@ -96,19 +96,19 @@ const Welfare = observer(function(props: Props) {
9696

9797
return (
9898
<WaterfallList
99-
ref={scrollViewRef}
99+
ref={waterfallRef}
100100
data={dataSource}
101101
style={styles.container}
102102
heightForItem={item => item.height + 10}
103103
preferColumnWidth={SCREEN_WIDTH / 2 - 10}
104104
renderItem={renderItem}
105105
onRefresh={() => {
106106
loadWelfareData('refreshing');
107-
!isRefreshing && scrollViewRef.current.endRefresh(); // 报错
107+
!isRefreshing && waterfallRef.current.endRefresh(); // 报错
108108
}}
109109
onLoading={async () => {
110110
loadWelfareData('load more');
111-
scrollViewRef && scrollViewRef.current.endLoading();
111+
waterfallRef && waterfallRef.current.endLoading();
112112
}}
113113
// loadingFooter={NormalFooter}
114114
/>

app/pages/WebView/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type State = {
2828
isForWard: boolean
2929
};
3030

31-
@inject('configStore')
31+
// @inject('configStore')
3232
export default class index extends React.Component<Props, State> {
3333
webView: WebView;
3434

@@ -49,7 +49,7 @@ export default class index extends React.Component<Props, State> {
4949
}
5050

5151
componentWillUnmount() {
52-
this.props.configStore.hideLoading();
52+
// this.props.configStore.hideLoading();
5353
StatusBar.setHidden(false);
5454
}
5555

@@ -110,14 +110,14 @@ export default class index extends React.Component<Props, State> {
110110
};
111111
onLoadEnd = () => {
112112
console.log('加载结束,成功或失败都会走到这里');
113-
this.props.configStore.hideLoading();
113+
// this.props.configStore.hideLoading();
114114
};
115115
onLoadStart = () => {
116116
console.log('开始加载');
117-
this.props.configStore.showLoading();
117+
// this.props.configStore.showLoading();
118118
};
119119
onError = () => {
120-
this.props.configStore.hideLoading();
120+
// this.props.configStore.hideLoading();
121121
Alert.alert(
122122
'加载失败',
123123
null,

app/routers/AppRouter.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { System } from '../utils/index';
1111
import { CustomIcon, Theme } from '../components/index';
1212

1313
import { ShiTu } from '../pages/ShiTu/index';
14-
import { News, Welfare, BuDeJie, BuDeJieDetail } from '../pages/News/News';
14+
import { News, Welfare, BuDeJie } from '../pages/News/News';
1515
import { Main } from '../pages/Main/index';
1616
import WebView from '../pages/WebView/index';
1717
import { Login } from '../pages/Login';
@@ -142,9 +142,6 @@ export const AppRouter = createStackNavigator(
142142
BuDeJie: {
143143
screen: BuDeJie
144144
},
145-
BuDeJieDetail: {
146-
screen: BuDeJieDetail
147-
},
148145
WebView: {
149146
screen: WebView
150147
},

0 commit comments

Comments
 (0)