Skip to content

Commit 51ac466

Browse files
authored
Merge pull request #2 from TheLukaszNs/master
Update develop with master
2 parents b713bec + d5ab69d commit 51ac466

File tree

8 files changed

+113
-16
lines changed

8 files changed

+113
-16
lines changed

android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ dependencies {
140140
compile "com.google.firebase:firebase-core:11.0.0"
141141
compile "com.google.firebase:firebase-analytics:11.0.0"
142142
compile "com.google.firebase:firebase-perf:11.0.0"
143+
compile "com.google.firebase:firebase-database:11.0.0"
143144

144145
// If you are receiving Google Play API availability issues, add the following dependency
145146
// compile "com.google.android.gms:play-services-base:11.0.0"

android/app/src/main/java/org/gdgsrilanka/codelanka/eznet/MainApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import io.invertase.firebase.analytics.RNFirebaseAnalyticsPackage;
1818
import io.invertase.firebase.crash.RNFirebaseCrashPackage;
1919
import io.invertase.firebase.perf.RNFirebasePerformancePackage;
20+
import io.invertase.firebase.database.RNFirebaseDatabasePackage;
21+
22+
import com.google.firebase.database.FirebaseDatabase;
2023

2124
public class MainApplication extends Application implements ReactApplication {
2225

@@ -34,6 +37,7 @@ protected List<ReactPackage> getPackages() {
3437
new RNFirebaseAnalyticsPackage(),
3538
new RNFirebaseCrashPackage(),
3639
new RNFirebasePerformancePackage(),
40+
new RNFirebaseDatabasePackage(),
3741
new VectorIconsPackage()
3842
);
3943
}
@@ -48,5 +52,6 @@ public ReactNativeHost getReactNativeHost() {
4852
public void onCreate() {
4953
super.onCreate();
5054
SoLoader.init(this, /* native exopackage */ false);
55+
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
5156
}
5257
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"react-native-responsive-image": "^2.1.0",
1919
"react-navigation": "^1.0.0-beta.11",
2020
"react-redux": "^5.0.5",
21-
"redux": "^3.7.1"
21+
"redux": "^3.7.1",
22+
"redux-thunk": "^2.2.0"
2223
},
2324
"devDependencies": {
2425
"babel-eslint": "^7.2.3",

src/actions/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { FETCH_DATA } from './types';
22

3-
import { categories as data } from './../api/dummy_api';
3+
import Api from '../api/Api';
44

5-
export const actionFetchData = () => ({
6-
type: FETCH_DATA,
7-
payload: data,
8-
});
5+
export const actionFetchData = () => (dispatch) => {
6+
Api.getCombinedData(Api.getCategories, Api.getSites)
7+
.then(data => dispatch({
8+
type: FETCH_DATA,
9+
payload: data,
10+
}));
11+
};

src/api/Api.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Firebase from './Firebase';
2+
3+
4+
class Api {
5+
/**
6+
* This method will return Promise containing something like this:
7+
{
8+
id: '1',
9+
title: 'පුවත් පත්',
10+
thumbnail: 'CATEGORY_NEWSPAPERS',
11+
items: [
12+
{
13+
id: '1_1',
14+
title: 'දිවයින',
15+
thumbnail: 'NEWSPAPERS_DIVAINA',
16+
url: 'http://www.divaina.com/',
17+
},
18+
]
19+
}
20+
* @param categories [reference to categories Promise]
21+
* @param sites [reference to sites Promise]
22+
* @returns {Promise}
23+
*/
24+
static getCombinedData = (categories, sites) => new Promise((resolve, reject) => {
25+
const finalArr = [];
26+
categories()
27+
.then((categoriesData) => {
28+
Object.keys(categoriesData).map((key, index) => {
29+
const title = key;
30+
const newTitle = title.charAt(0).toUpperCase() + title.slice(1);
31+
return finalArr.push({ id: index + 1, title: newTitle, thumbnail: undefined });
32+
// we have to add Thumbnail (both - to Firebase and a[[)
33+
});
34+
return finalArr;
35+
})
36+
.then((nextData) => {
37+
const newArray = [];
38+
nextData.map((key, index) => sites(key.title)
39+
.then((siteData) => {
40+
const itemsArray = []; // helper Array to store all formatted items
41+
Object.keys(siteData).map((key1, index1) => itemsArray.push({
42+
id: `${finalArr[index].id}_${index1}`,
43+
title: key1,
44+
url: `http://${siteData[key1].url}`, // for now it will produce bad URL for links starting with http:// or https:// [needs to be changed in database]
45+
}));
46+
newArray.push({ ...finalArr[index], items: itemsArray });
47+
if (newArray.length === finalArr.length) {
48+
resolve(newArray);
49+
}
50+
}));
51+
})
52+
.catch(err => reject(err));
53+
});
54+
55+
/**
56+
* Returns promise with all sites in given category
57+
* @param category
58+
* @returns {Promise}
59+
*/
60+
static getSites = category => Firebase.database().ref(`/Sites/${category}`).once('value')
61+
.then(snapshot => snapshot.val());
62+
63+
/**
64+
* Returns promise with all categories
65+
* @returns {Promise}
66+
*/
67+
static getCategories = () => Firebase.database().ref('/Categories').once('value')
68+
.then(snapshot => snapshot.val());
69+
}
70+
71+
export default Api;

src/api/Firebase.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import firebase from 'react-native-firebase';
2+
3+
const firebseInstance = firebase.initializeApp();
4+
5+
// It's exporting initialized Firebase Instance
6+
export default firebseInstance;

src/components/App.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@ class Categories extends Component {
3636
renderCategories() {
3737
const { navigate } = this.props.navigation;
3838

39-
const nodes = this.props.categoryData.map(category => (
40-
<BoxItem
41-
key={category.id}
42-
onPress={() => navigate('Detail', { title: category.title, items: category.items })}
43-
{...category}
44-
/>
45-
));
39+
let nodes = null;
40+
if (this.props.categoryData.length !== 0) {
41+
nodes = this.props.categoryData.map(category => (
42+
<BoxItem
43+
key={category.id}
44+
onPress={() => navigate('Detail', { title: category.title, items: category.items })}
45+
{...category}
46+
/>
47+
));
48+
}
4649

47-
return (
50+
return nodes === null ? null : (
4851
<TwoColumnView>
4952
{nodes}
5053
</TwoColumnView>

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import React from 'react';
22
import { Provider } from 'react-redux';
3-
import { createStore } from 'redux';
3+
import {
4+
createStore,
5+
applyMiddleware,
6+
compose,
7+
} from 'redux';
8+
import thunk from 'redux-thunk';
49
import reducers from './reducers';
510

611
import EZNetApp from './EZNetApp';
712

13+
const store = compose(applyMiddleware(thunk))(createStore)(reducers);
14+
815
export default () => (
9-
<Provider store={createStore(reducers)}>
16+
<Provider store={store}>
1017
<EZNetApp />
1118
</Provider>
1219
);

0 commit comments

Comments
 (0)