Skip to content

Commit fb294be

Browse files
committed
add loader for file view screen
1 parent d8ad75e commit fb294be

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { StyleSheet, View, ActivityIndicator, Text } from 'react-native';
3+
4+
import { colors } from '../../../config';
5+
6+
type Props = {
7+
animating: boolean,
8+
text: string,
9+
center: boolean,
10+
};
11+
12+
const styles = StyleSheet.create({
13+
loadingContainer: {
14+
backgroundColor: colors.white,
15+
flex: 1,
16+
alignItems: 'center',
17+
},
18+
center: {
19+
justifyContent: 'center',
20+
},
21+
text: {
22+
paddingTop: 20,
23+
},
24+
});
25+
26+
export default ({ animating, text, center }: Props) => (
27+
<View style={[styles.loadingContainer, center && styles.center]}>
28+
<ActivityIndicator animating={animating} size="large" />
29+
{text && <Text style={styles.text}>{text}</Text>}
30+
</View>
31+
);

src/gists/screens/components/Toolbar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Title = styled.Text`
2020
export default (props) => {
2121
let toolbarContent;
2222
if (typeof props.toolbarContent === 'string') {
23-
toolbarContent = <Title>{props.toolbarContent}</Title>
23+
toolbarContent = <Title numberOfLines={1}>{props.toolbarContent}</Title>
2424
} else if (typeof props.toolbarContent === 'function') {
2525
toolbarContent = props.toolbarContent();
2626
} else {

src/gists/screens/gistfilecontent.screen.js

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import React from 'react';
22
import styled from 'styled-components';
3-
import { Text, ScrollView } from 'react-native';
3+
import { Text, ScrollView, View } from 'react-native';
44
import axios from 'axios';
55
import { connect } from 'react-redux';
6+
import isEmpty from 'lodash/isEmpty';
67
import SyntaxHighlighter from 'react-native-syntax-highlighter';
78
import { github as GithubStyle } from 'react-syntax-highlighter/dist/styles/hljs';
89
import Toolbar from './components/Toolbar';
9-
import { normalizeFont } from '../../config';
10+
import { normalizeFont, colors } from '../../config';
1011
import { fetchFileContent } from './../../api';
12+
import LoadingView from './components/LoadingView';
1113

1214
const Container = styled.View`
15+
flex: 1;
16+
background: #FFFFFF;
1317
padding-top: 20px;
1418
`;
1519

@@ -20,6 +24,25 @@ const syntaxHighlighterStyle = {
2024
},
2125
};
2226

27+
const CodeContainer = styled.View`
28+
flex: 1;
29+
padding-vertical: 10;
30+
padding-horizontal: 10;
31+
margin-bottom: 10;
32+
`
33+
34+
const ErrorContainer = styled.View`
35+
flex: 1;
36+
align-items: center;
37+
justify-content: center;
38+
`;
39+
40+
const ErrorText = styled.Text`
41+
text-align: center;
42+
color: ${colors.greyDark};
43+
font-size: ${normalizeFont(14)};
44+
`
45+
2346
class GistFileScreen extends React.Component {
2447
constructor(props) {
2548
super(props);
@@ -33,6 +56,7 @@ class GistFileScreen extends React.Component {
3356
componentDidMount() {
3457
this.setState({
3558
isLoading: true,
59+
error: ''
3660
})
3761

3862
const { navigation, accessToken } = this.props;
@@ -42,7 +66,7 @@ class GistFileScreen extends React.Component {
4266
.then(response => {
4367
this.fileContent = response;
4468
this.setState({
45-
isLoading: true,
69+
isLoading: false,
4670
});
4771
})
4872
.catch(err => {
@@ -57,24 +81,35 @@ class GistFileScreen extends React.Component {
5781
const { navigation } = this.props;
5882
const fileName = navigation.state.params.fileData.filename;
5983
const fileType = fileName.split('.').pop();
84+
const { isLoading, error } = this.state;
6085

6186
return(
6287
<Container>
6388
<Toolbar
6489
toolbarContent={fileName}
6590
onBackPress={() => navigation.goBack()}
6691
/>
67-
<ScrollView>
68-
<SyntaxHighlighter
69-
language={fileType}
70-
CodeTag={Text}
71-
codeTagProps={{ style: {paddingRight: 15, paddingBottom: 0}}}
72-
style={syntaxHighlighterStyle}
73-
fontSize={normalizeFont(12)}
74-
>
75-
{this.fileContent}
76-
</SyntaxHighlighter>
77-
</ScrollView>
92+
{ isLoading && <LoadingView animating={true} center />}
93+
{ !isLoading && !isEmpty(error) &&
94+
<ErrorContainer>
95+
<ErrorText>{error}</ErrorText>
96+
</ErrorContainer>
97+
}
98+
{ !isLoading && !error &&
99+
<ScrollView>
100+
<CodeContainer>
101+
<SyntaxHighlighter
102+
language={fileType}
103+
CodeTag={Text}
104+
codeTagProps={{ style: {paddingRight: 15, paddingBottom: 0}}}
105+
style={syntaxHighlighterStyle}
106+
fontSize={normalizeFont(12)}
107+
>
108+
{this.fileContent}
109+
</SyntaxHighlighter>
110+
</CodeContainer>
111+
</ScrollView>
112+
}
78113
</Container>
79114
)
80115
}

0 commit comments

Comments
 (0)