Skip to content

Commit 5ff8088

Browse files
authored
Merge pull request #154 from xmartlabs/example-app
[Chore] Add activity indicator to the example app operations
2 parents 3897e9a + 91c4530 commit 5ff8088

File tree

4 files changed

+89
-37
lines changed

4 files changed

+89
-37
lines changed

example/app/(app)/home.tsx

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import Line, { AccessToken, UserProfile } from '@xmartlabs/react-native-line'
22
import { useRouter } from 'expo-router'
3-
import { useEffect, useState } from 'react'
4-
import {
5-
ActivityIndicator,
6-
Alert,
7-
Dimensions,
8-
Image,
9-
StyleSheet,
10-
} from 'react-native'
3+
import { Fragment, useEffect, useState } from 'react'
4+
import { Alert, Dimensions, Image, StyleSheet } from 'react-native'
115

126
import {
137
removeLocalStorageItem,
148
setLocalStorageItem,
159
} from '@/common/localStorage'
10+
import { ActivityBanner } from '@/components/ActivityBanner'
1611
import { Bullet } from '@/components/Bullet'
1712
import { Button } from '@/components/Button'
1813
import { ThemedView } from '@/components/ThemedView'
@@ -39,56 +34,60 @@ export default function () {
3934
}, [])
4035

4136
function logOut() {
42-
return Line.logout().then(() => {
43-
removeLocalStorageItem('accessToken')
44-
router.replace('/login')
45-
})
37+
setLoading(true)
38+
return Line.logout()
39+
.then(() => {
40+
removeLocalStorageItem('accessToken')
41+
router.replace('/login')
42+
})
43+
.finally(() => setLoading(false))
4644
}
4745

4846
function getFriendshipStatus() {
47+
setLoading(true)
4948
return Line.getFriendshipStatus()
5049
.then(result => Alert.alert(strings.isFriend, String(result.friendFlag)))
5150
.catch(handleError)
51+
.finally(() => setLoading(false))
5252
}
5353

5454
function refreshAccessToken() {
55+
setLoading(true)
5556
return Line.refreshAccessToken()
5657
.then(accessToken => {
5758
setLocalStorageItem('accessToken', accessToken.accessToken)
5859
setToken(accessToken)
5960
})
6061
.catch(handleError)
62+
.finally(() => setLoading(false))
6163
}
6264

6365
function verifyAccessToken() {
66+
setLoading(true)
6467
return Line.verifyAccessToken()
6568
.then(result => Alert.alert(result.clientId, result.expiresIn.toString()))
6669
.catch(handleError)
67-
}
68-
69-
if (loading) {
70-
return (
71-
<ThemedView style={styles.container}>
72-
<ActivityIndicator size="large" />
73-
</ThemedView>
74-
)
70+
.finally(() => setLoading(false))
7571
}
7672

7773
return (
78-
<ThemedView style={styles.container}>
79-
<ThemedView style={styles.contentContainer}>
80-
<Image source={{ uri: user?.pictureUrl }} style={styles.image} />
81-
<Bullet header={strings.name} text={user?.displayName} />
82-
<Bullet header={strings.userId} text={user?.userId} />
83-
<Bullet header={strings.accessToken} text={token?.accessToken} />
84-
</ThemedView>
85-
<Button onPress={getFriendshipStatus} text={strings.isFriend} />
86-
<ThemedView style={styles.row}>
87-
<Button onPress={verifyAccessToken} text={strings.verifyToken} />
88-
<Button onPress={refreshAccessToken} text={strings.refreshToken} />
74+
<Fragment>
75+
<ThemedView style={styles.container}>
76+
<ThemedView style={styles.contentContainer}>
77+
<Image source={{ uri: user?.pictureUrl }} style={styles.image} />
78+
<Bullet header={strings.name} text={user?.displayName} />
79+
<Bullet header={strings.userId} text={user?.userId} />
80+
<Bullet header={strings.accessToken} text={token?.accessToken} />
81+
</ThemedView>
82+
<Button onPress={getFriendshipStatus} text={strings.isFriend} />
83+
<ThemedView style={styles.row}>
84+
<Button onPress={verifyAccessToken} text={strings.verifyToken} />
85+
<Button onPress={refreshAccessToken} text={strings.refreshToken} />
86+
</ThemedView>
87+
<Button onPress={logOut} text={strings.logOut} />
8988
</ThemedView>
90-
<Button onPress={logOut} text={strings.logOut} />
91-
</ThemedView>
89+
{loading && <ActivityBanner />}
90+
</Fragment>
9291
)
9392
}
9493

example/app/(app)/login.tsx

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import Line from '@xmartlabs/react-native-line'
22
import * as Haptics from 'expo-haptics'
33
import { useRouter } from 'expo-router'
4+
import { Fragment, useState } from 'react'
45
import { Alert, Image, StyleSheet } from 'react-native'
56

67
import Logo from '@/assets/images/logo.png'
78
import { setLocalStorageItem } from '@/common/localStorage'
9+
import { ActivityBanner } from '@/components/ActivityBanner'
810
import { LineButton } from '@/components/LineButton'
911
import { ThemedView } from '@/components/ThemedView'
1012

1113
export default function () {
1214
const router = useRouter()
15+
const [loading, setLoading] = useState<boolean>(false)
1316

1417
function logIn() {
18+
setLoading(true)
1519
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
1620
return Line.login()
1721
.then(result => {
@@ -22,13 +26,19 @@ export default function () {
2226
.catch(() => {
2327
Alert.alert(strings.errorTitle, strings.errorMessage)
2428
})
29+
.finally(() => {
30+
setLoading(false)
31+
})
2532
}
2633

2734
return (
28-
<ThemedView style={styles.container}>
29-
<Image source={Logo} style={styles.logo} />
30-
<LineButton onPress={logIn} />
31-
</ThemedView>
35+
<Fragment>
36+
<ThemedView style={styles.container}>
37+
<Image source={Logo} style={styles.logo} />
38+
<LineButton onPress={logIn} />
39+
</ThemedView>
40+
{loading && <ActivityBanner />}
41+
</Fragment>
3242
)
3343
}
3444

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { FunctionComponent } from 'react'
2+
import { ActivityIndicator, StyleSheet } from 'react-native'
3+
import Animated, {
4+
FadeIn,
5+
FadeOut,
6+
ZoomIn,
7+
ZoomOut,
8+
} from 'react-native-reanimated'
9+
10+
import { Color } from '@/constants/Colors'
11+
12+
interface Props {
13+
backgroundColor?: Color
14+
}
15+
16+
export const ActivityBanner: FunctionComponent<Props> = ({
17+
backgroundColor = Color.Black50PCT,
18+
}) => (
19+
<Animated.View
20+
entering={FadeIn}
21+
exiting={FadeOut}
22+
style={[StyleSheet.absoluteFill, styles.container, { backgroundColor }]}>
23+
<Animated.View
24+
entering={ZoomIn}
25+
exiting={ZoomOut}
26+
style={styles.contentContainer}>
27+
<ActivityIndicator size="small" />
28+
</Animated.View>
29+
</Animated.View>
30+
)
31+
32+
const styles = StyleSheet.create({
33+
container: {
34+
alignItems: 'center',
35+
justifyContent: 'center',
36+
},
37+
contentContainer: {
38+
backgroundColor: Color.White,
39+
borderRadius: 12,
40+
padding: 12,
41+
},
42+
})

example/constants/Colors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export enum Color {
22
Black = '#000000',
33
Black8PCT = '#00000014',
4+
Black50PCT = '#00000080',
45
Gray = '#1E1E1E',
56
Gray20PCT = '#1E1E1E33',
67
Green = '#06C755',

0 commit comments

Comments
 (0)