1- import React from 'react' ;
1+ import React from 'react'
22import Game from "./app/containers/Game"
3- import { StackNavigator } from "react-navigation" ;
4- import { Text , View } from "react-native" ;
5- import _ from "lodash" ;
3+ import { StackNavigator } from "react-navigation"
4+ import { Text , View , PermissionsAndroid } from "react-native"
5+ import _ from "lodash"
66import { AdamConfig , defaultConfig , FrycekConfig , FrycekTestConfig , KacperConfig } from "./TEMP_CONFIGS"
7- import MainScreen from "./app/containers/MainScreen" ;
8- import { readActiveConfig , readConfigs } from "./app/services/db/configs" ;
9- import { ModeTypes } from "./app/services/db/format" ;
10- import ConfigProvider from "./app/containers/ConfigProvider" ;
11- import ConfigConsumer from "./app/containers/ConfigConsumer" ;
7+ import MainScreen from "./app/containers/MainScreen"
8+ import { readActiveConfig , readConfigs } from "./app/services/db/configs"
9+ import { ModeTypes } from "./app/services/db/format"
10+ import ConfigProvider from "./app/containers/ConfigProvider"
11+ import ConfigConsumer from "./app/containers/ConfigConsumer"
1212import Analytics from "appcenter-analytics"
13- import SplashScreen from "./app/containers/SplashScreen" ;
13+ import SplashScreen from "./app/containers/SplashScreen"
14+ import ErrorScreen from "./app/containers/ErrorScreen"
1415
1516const configFromDbToGameScreen = ( config ) => ( {
1617 ...config ,
@@ -21,7 +22,7 @@ const configFromDbToGameScreen = (config) => ({
2122 isInTestMode : material . isInTestMode ,
2223 isInLearningMode : material . isInLearningMode ,
2324 } ) ) ,
24- } ) ;
25+ } )
2526
2627function prepareLevels ( materials , repetitions , optionsNumber ) {
2728 let levels = _ . shuffle ( _ . flatMap ( materials , material =>
@@ -30,10 +31,10 @@ function prepareLevels(materials, repetitions, optionsNumber) {
3031 name : material . name ,
3132 images : material . images ,
3233 isCorrectAnswer : true
33- } ) ) ) ) ) ;
34+ } ) ) ) ) )
3435
35- levels = _ . map ( levels , level => _ . map ( level , material => ( { ...material , image : _ . sample ( material . images ) } ) ) ) ;
36- return _ ( levels ) ;
36+ levels = _ . map ( levels , level => _ . map ( level , material => ( { ...material , image : _ . sample ( material . images ) } ) ) )
37+ return _ ( levels )
3738}
3839
3940const GameScreen = ( { navigation} ) => {
@@ -43,7 +44,7 @@ const GameScreen = ({navigation}) => {
4344 const isTestMode = mode === ModeTypes . test
4445
4546 const materials = isTestMode ? _ . filter ( config . materials , 'isInTestMode' ) : config . materials ,
46- repetitions = isTestMode ? config . testConfig . numberOfRepetitions : config . numberOfRepetitions ;
47+ repetitions = isTestMode ? config . testConfig . numberOfRepetitions : config . numberOfRepetitions
4748
4849 return < Game
4950 levels = { prepareLevels ( materials , repetitions , config . picturesNumber ) }
@@ -61,7 +62,7 @@ const GameScreen = ({navigation}) => {
6162 }
6263 </ ConfigConsumer >
6364 )
64- } ;
65+ }
6566
6667
6768const AppNavigator = StackNavigator (
@@ -73,21 +74,21 @@ const AppNavigator = StackNavigator(
7374 {
7475 headerMode : "none" ,
7576 initialRouteName : "Splash"
76- } ) ;
77+ } )
7778
7879
7980export default class App extends React . Component {
8081 state = {
8182 fontLoaded : false ,
82- } ;
83+ }
8384
8485 async loadConfig ( ) {
8586 const { id, mode = ModeTypes . learning } = await readActiveConfig ( )
8687 console . log ( "Active config id: " , id , "in mode:" , mode === ModeTypes . learning ? "learning" : "test" )
8788
8889 const configs = await readConfigs ( )
8990
90- const activeConfig = configs . find ( config => config . id === id ) || defaultConfig ;
91+ const activeConfig = configs . find ( config => config . id === id ) || defaultConfig
9192
9293 console . log ( "Active config:" , activeConfig )
9394 return {
@@ -103,23 +104,54 @@ export default class App extends React.Component {
103104 console . log ( "Loaded all assets" )
104105 }
105106
107+ async requestExternalStoragePermission ( ) {
108+ try {
109+ const granted = await PermissionsAndroid . request (
110+ PermissionsAndroid . PERMISSIONS . WRITE_EXTERNAL_STORAGE ,
111+ {
112+ 'title' : 'Przyjazne Słowa - uprawnienia do zapisu' ,
113+ 'message' : "Aplikacja Przyjazne Słowa wymaga uprawnień do zapisu danych w pamięci urządzenia, aby móc komunikować się z aplikacją Przyjazne Słowa - Menadżer"
114+ }
115+ )
116+ if ( granted === PermissionsAndroid . RESULTS . GRANTED ) {
117+ console . log ( "You can use the camera" )
118+ return true ;
119+ } else {
120+ console . log ( "Camera permission denied" )
121+ return false ;
122+ }
123+ } catch ( err ) {
124+ console . warn ( err )
125+ return false ;
126+ }
127+ }
128+
106129 async componentDidMount ( ) {
107- Analytics . trackEvent ( "App loaded" ) ;
130+ Analytics . trackEvent ( "App loaded" )
131+ const hasPermissions = await this . requestExternalStoragePermission ( ) ;
132+
133+ if ( ! hasPermissions ) {
134+ this . setState ( { noPermissions : true } )
135+ return ;
136+ }
108137
109- await this . loadAssets ( ) ;
110- const { config, mode} = await this . loadConfig ( ) ;
138+ await this . loadAssets ( )
139+ const { config, mode} = await this . loadConfig ( )
111140 this . setState ( {
112141 assetsLoaded : true ,
113142 config : configFromDbToGameScreen ( config ) ,
114143 mode
115- } ) ;
144+ } )
116145 }
117146
118147 render ( ) {
148+ if ( this . state . noPermissions ) {
149+ return < ErrorScreen />
150+ }
119151 return ! this . state . assetsLoaded ? < Text > Loading</ Text > : (
120152 < ConfigProvider config = { this . state . config } mode = { this . state . mode } >
121153 < AppNavigator />
122154 </ ConfigProvider >
123- ) ;
155+ )
124156 }
125157}
0 commit comments