-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApp.js
248 lines (220 loc) · 8.7 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import React from 'react'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { KeyboardAvoidingView, Appearance } from 'react-native'
import { NavigationContainer, DefaultTheme, DarkTheme } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'
import Toast from 'react-native-toast-message'
import * as Font from 'expo-font'
import * as SplashScreen from 'expo-splash-screen'
import Login from './views/Login'
import Home from './views/Home'
import AllBoards from './views/AllBoards'
import BoardDetails from './views/BoardDetails'
import CardDetails from './views/CardDetails'
import Settings from './views/Settings'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { setServer } from './store/serverSlice'
import { setTheme } from './store/themeSlice'
import { setToken } from './store/tokenSlice'
import { setColorScheme } from './store/colorSchemeSlice'
import * as Linking from 'expo-linking' // For creating an URL handler to retrieve the device token
import {encode as btoa} from 'base-64' // btoa isn't supported by android (and maybe also iOS)
import * as Device from 'expo-device'
import * as ScreenOrientation from 'expo-screen-orientation'
import { adapty } from 'react-native-adapty' // in-app purchases
import mobileAds from 'react-native-google-mobile-ads'
import { AdEventType, AdsConsent, AppOpenAd } from 'react-native-google-mobile-ads'
import { isUserSubscribed } from './utils'
// Creates Stack navigator
const Stack = createStackNavigator()
// Prevents native splash screen from autohiding before App component declaration
SplashScreen.preventAutoHideAsync().catch(console.warn)
// Application
class App extends React.Component {
async loadFonts() {
console.log('Loading fonts')
await Font.loadAsync({
deck: require('./assets/fonts/deck/deck.ttf'),
})
this.setState({ fontsLoaded: true })
}
constructor(props) {
console.log('initialising app')
super(props)
this.state = {
fontsLoaded: false,
colorScheme: 'light',
navigation: {},
}
// Force portrait mode on iPhones
if (Device.modelId && Device.modelId.startsWith('iPhone')) {
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP)
}
// Register handler to catch Nextcloud's redirect after successfull login
Linking.addEventListener('url', (url) => {this.handleRedirect(url)})
// Activates adapty and show ad if needed afterward
console.log('Activating adapty')
adapty.activate('public_live_dQQGIW4b.wZU2qtAbVtrojrx9ttUu').then( async () =>{
console.log('adapty activated, waiting 1 second before checking user\'subscribtion')
setTimeout(() => this.showAdIfNeeded(), 1000)
} )
}
// Try to show ad if user hasn't subscribed to a paying version of the app
// For the moment, we just let the user use the app freely if he doesn't give consent for ads
async showAdIfNeeded() {
console.log('Adapty activated, checking if user is subscribed to a paying version of the app')
if (! await isUserSubscribed()) {
// User hasn't subscribed to a paying version of the app, we'll try to show him/her ads.
// Checks if we need to re-ask consent (eg: due to conditions change at provider-side)
console.log('User has not subscribed to a paying version of the app, trying to display ads')
console.log('Checking if we need to ask user consent to display ads')
const consentInfo = await AdsConsent.requestInfoUpdate()
if (consentInfo.status !== 'OBTAINED') {
// Asks consent
console.log('Asking user consent')
await AdsConsent.loadAndShowConsentFormIfRequired();
} else {
console.log('Nope, we don\'t')
}
// Shows ad if user gaves enough consent
console.log('checking user consents')
const userChoice = await AdsConsent.getUserChoices();
if (userChoice.storeAndAccessInformationOnDevice) {
// Initializes ads
console.log('Ok we got user consent to display ads')
mobileAds().initialize().then(() => {
let requestOptions = { requestNonPersonalizedAdsOnly: true }
if (!userChoice.selectPersonalisedContent) {
console.log('Not for personalised ads though')
requestOptions = {}
}
const appOpenAd = AppOpenAd.createForAdRequest("ca-app-pub-8838289832709828/1694360664", requestOptions)
appOpenAd.addAdEventListener(AdEventType.LOADED, () => {
console.log('Showing ad')
appOpenAd.show()
});
console.log('Loading ad')
appOpenAd.load()
})
} else {
// For the moment, we just let the user use the app freely if he doesn't give consent for ads
console.log('User did not gave enough consent to use admob (missing "storeAndAccessInformationOnDevice" right)')
}
} else {
console.log('User is subscribed to a paying version of the app')
}
}
async componentDidMount() {
this.loadFonts()
// Sets theme
AsyncStorage.getItem('colorScheme').then(savedColorScheme => {
let colorScheme
if (savedColorScheme !== null && savedColorScheme !== 'os') {
colorScheme = savedColorScheme
console.log('colorScheme retrieved', colorScheme)
this.props.setColorScheme(colorScheme)
} else {
// Using os colorscheme
colorScheme = Appearance.getColorScheme()
console.log('using OS colorsSheme')
this.props.setColorScheme('os')
}
this.setState({ colorScheme: colorScheme})
this.props.setTheme(colorScheme)
})
// Registers theme change subscription
this._schemeSubscription = Appearance.addChangeListener(({ colorScheme }) => {
this.setState({ colorScheme })
this.props.setTheme(colorScheme)
this.props.setColorScheme(colorScheme)
});
// Retrieves last viewed board and stack if available
AsyncStorage.getItem('navigation').then(navigation => {
navigation = navigation != null ? JSON.parse(navigation) : { boardId: null, stackId: null }
console.log('Retrieved last navigated board+stack')
this.setState({navigation})
})
// Retrieve token from storage if available
AsyncStorage.getItem('NCtoken').then(token => {
if (token !== null) {
console.log('token retrieved from asyncStorage')
this.props.setToken('Basic ' + token)
AsyncStorage.getItem('NCserver').then(server => {
if (server !== null) {
console.log('server retrieved from asyncStorage')
this.props.setServer(server)
}
})
}
})
SplashScreen.hideAsync()
}
// Function to retrieve the device's token and save it after user logged in
async handleRedirect({url}) {
if (url.startsWith('nc://login/server')) {
let user = decodeURIComponent(url.substring(url.lastIndexOf('user:')+5, url.lastIndexOf('&'))).replace(/\+/g, ' ')
console.log('User is', user)
const pwd = url.substring(url.lastIndexOf(':')+1)
const token = btoa(user + ':' + pwd)
console.log('Persisting token in asyncStorage', token)
// TODO Use expo-secure-store to securely store the token
AsyncStorage.setItem('NCtoken', token);
console.log('Saving token in store')
this.props.setToken('Basic ' + token)
}
}
render() {
if(this.state.fontsLoaded && Object.keys(this.state.navigation).length !== 0) {
if (this.props.token.value === null || this.props.server.value === null) {
// No token is stored yet, we need to get one
return (
<NavigationContainer theme={this.state.colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
<Stack.Navigator screenOptions={({navigation}) => {return {detachPreviousScreen: !navigation.isFocused()}}}>
<Stack.Screen name="Home" component={Home} options={{ title: 'Login' }}/>
<Stack.Screen name="Login" component={Login}/>
</Stack.Navigator>
</NavigationContainer>
)
} else {
return (
<KeyboardAvoidingView style={{ flex: 1 }} behavior='padding'>
<NavigationContainer theme={this.state.colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
<Stack.Navigator
screenOptions={({navigation}) => {return {detachPreviousScreen: !navigation.isFocused()}}}
initialRouteName='AllBoards'
>
<Stack.Screen name="AllBoards" component={AllBoards} initialParams={{navigation: this.state.navigation}}/>
<Stack.Screen name="BoardDetails" component={BoardDetails} />
<Stack.Screen name="CardDetails" component={CardDetails} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
</NavigationContainer>
<Toast />
</KeyboardAvoidingView>
)
}
} else {
return null
}
}
}
// Connect to store
const mapStateToProps = state => ({
colorScheme: state.colorScheme,
server: state.server,
theme: state.theme,
token: state.token,
})
const mapDispatchToProps = dispatch => (
bindActionCreators( {
setColorScheme,
setServer,
setToken,
setTheme,
}, dispatch)
)
export default connect(
mapStateToProps,
mapDispatchToProps
)(App)