-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
148 lines (141 loc) · 5.08 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
* @lint-ignore-every XPLATJSCOPYRIGHT1
*/
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, List, AppState, AsyncStorage } from 'react-native';
import { createBottomTabNavigator, createAppContainer, createStackNavigator } from 'react-navigation';
import { Provider } from 'mobx-react';
import NewsAPI from 'newsapi';
import SearchPage from './SearchPage';
import CountryPage from './CountryPage';
import LanguagePage from './LanguagePage';
import { createStore, applyMiddleware } from 'redux';
// import { Provider } from 'react-redux';
import CountryPageModel, { reducer } from './ViewModels/CountryPageModel';
import SearchPageModel from './ViewModels/SearchPageModel';
import LanguagePageModel from './ViewModels/LanguagePageModel';
import SourcesPageModel from './ViewModels/SourcesPageModel';
import ArticlePage from './ArticlePage';
import SourcesPage from './SourcesPage';
/*
const tabs = createBottomTabNavigator({
Search: { screen: SearchPage },
Sources: { screen: SourcesPage }
});
*/
const Navigation = createStackNavigator(
{
Search: { screen: SearchPage },
Country: { screen: CountryPage },
Language: { screen: LanguagePage },
Article: { screen: ArticlePage },
Sources: { screen: SourcesPage }
},
{
initialRouteKey: 'Search'
}
);
const AppContainer = createAppContainer(Navigation);
// const store = createStore(reducer);
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props);
}
componentDidMount() {
AppState.addEventListener('change', this.handleAppStateExit);
this.loadPreferences().then(keys => {
console.log('keys: ' + keys);
if (keys.length === 0) {
console.log('App was started for the first time');
// preform set up
// create firstTimeStart viewModel triggering tooltips and
} else {
console.log('App was started and has started before');
}
});
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateExit);
}
handleAppStateExit = (nextAppState) => {
if (nextAppState === 'inactive') {
console.log('app is shutting down');
this.savePreferences();
}
}
// consider saving to azure instead - so that preferences is shared between devices with app installed
async savePreferences() {
const search = SearchPageModel.getInstance();
const setQuery = search.query ? search.query : '';
console.log('setQuery: ' + setQuery);
await AsyncStorage.setItem('query', setQuery);
const setCategory = search.selectedCategory ? search.selectedCategory : '';
await AsyncStorage.setItem('category', setCategory);
const countryPageModel = CountryPageModel.getInstance();
// only stringfy if properties contains value or error is thrown
const setCountry = countryPageModel.countries ? JSON.stringify(countryPageModel.countries) : '';
console.log('setCountry: ' + setCountry);
await AsyncStorage.setItem('countries', setCountry);
const languagePageModel = LanguagePageModel.getInstance();
const setLanguage = languagePageModel.languages ? JSON.stringify(languagePageModel.languages) : '';
console.log('setLanguage: ' + setLanguage);
await AsyncStorage.setItem('languages', setLanguage);
const sourcesPageModel = SourcesPageModel.getInstance();
const setSource = sourcesPageModel.previousSelectedSources() ?
JSON.stringify(sourcesPageModel.previousSelectedSources()) : '';
console.log('setSource: ' + setSource);
await AsyncStorage.setItem('sources', setSource);
/*
const keys = await AsyncStorage.getAllKeys(); // *** REMOVE WHEN BUILD IPA APK. made for simulating a first time start only
await AsyncStorage.multiRemove(keys);
*/
}
async loadPreferences() {
// console.log('loading prefrences');
const search = SearchPageModel.getInstance();
const query = await AsyncStorage.getItem('query');
if (query) {
search.query = query;
}
const category = await AsyncStorage.getItem('category');
if (category) {
search.selectedCategory = category;
}
const countryPageModel = CountryPageModel.getInstance();
const countries = await AsyncStorage.getItem('countries');
if (countries) {
countryPageModel.countries = JSON.parse(countries);
}
const languagePageModel = LanguagePageModel.getInstance();
const languages = await AsyncStorage.getItem('languages');
if (languages) {
languagePageModel.languages = JSON.parse(languages);
}
const sourcesPageModel = SourcesPageModel.getInstance();
const sources = await AsyncStorage.getItem('sources');
if (sources) {
sourcesPageModel.sources = JSON.parse(sources);
}
return await AsyncStorage.getAllKeys();
}
render() {
return (
<Provider
// ref={(ref) => { this.provider = ref; }}
countryPageModel={CountryPageModel.getInstance()}
searchPageModel={SearchPageModel.getInstance()}
languagePageModel={LanguagePageModel.getInstance()}
sourcesPageModel={SourcesPageModel.getInstance()}
>
<AppContainer />
</Provider>
);
}
}
// previous issue with mobx setting .babelrc