Skip to content

Commit

Permalink
implement search location
Browse files Browse the repository at this point in the history
  • Loading branch information
Kien Tran committed Oct 6, 2018
1 parent 8886e0e commit 3155271
Show file tree
Hide file tree
Showing 10 changed files with 108,537 additions and 103 deletions.
1 change: 1 addition & 0 deletions domain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Domain",
"main": "./dist/index.js",
"scripts": {
"build": "yarn install && tsc",
"test": "jest"
},
"keywords": [
Expand Down
104 changes: 69 additions & 35 deletions repositories/lib/location/LocationRepository.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,75 @@
import { Observable } from 'rxjs';
import { Location, LocationRepository } from 'weather-domain';
import {RxHR} from "@akanass/rx-http-request";
import {createClient} from 'react-native-google-maps-services';
import * as RxJs from 'rxjs';
import * as RxJsOperators from 'rxjs/operators'

import { Location, LocationRepository } from 'weather-domain';
import Axios, * as axios from "axios";
import { createClient } from 'react-native-google-maps-services';
// import { map, flatMap } from 'rxjs/operators';
// import {} from '@reactivex/rxjs'
// import 'rxjs/Rx'
export class DefaultLocationRepository implements LocationRepository {
searchCity(searchText: string): Observable<Location> {
return Observable.create(function (observer) {
searchCity(searchText: string): RxJs.Observable<Location> {
return RxJs.Observable.create(function (observer) {
const key = "AIzaSyBO35s72ZQbDBabY1y9JsW59SBsHKcIfC8"
const uri = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key="+ key +"&input=" + searchText + "&types=(cities)"

const client = createClient({
key: "AIzaSyDyp4K2oq2GDsHRIzy7Qpt87gGmlCRnvuI"

// axios.default.defaults.headers["referer"] = "http://kienhiepsi.com"
axios.default.get(uri)
.then( response => {
if (response.status == 200) {
var results: Array<Location> = []
console.log(response.data)
for (const prediction of response.data["predictions"]) {
var structured_formatting = prediction.structured_formatting
var mainText = structured_formatting.main_text
var secondaryText = structured_formatting.secondary_text

results.push(new Location(
mainText, secondaryText
))
}

observer.next(results)
}
// console.log(response); // Show the JSON response object.
})
let query = {
input: searchText
}

client.placesAutoComplete(query,
function (err, response) {
if (response) {

var results:Array<Location> = []
for (const prediction of response.json.predictions) {
var structured_formatting = prediction.structured_formatting
var mainText = structured_formatting.main_text
var secondaryText = structured_formatting.secondary_text

results.push(new Location(
mainText, secondaryText
))
}

observer.next(results)
} else {
console.log(err)

observer.error(err)
}
})
.catch( error => {
console.log(error); // Show the JSON response object.

observer.error(error)
})

// RxHR.get(uri, options).subscribe(
// data => {
// if (data.response.statusCode === 200) {
// var results: Array<Location> = []
// for (const prediction of data.body.predictions) {
// var structured_formatting = prediction.structured_formatting
// var mainText = structured_formatting.main_text
// var secondaryText = structured_formatting.secondary_text

// results.push(new Location(
// mainText, secondaryText
// ))
// }

// observer.next(results)
// }
// },
// error => {

// }
// )
// })
})
}
}

}


// var repository = new DefaultLocationRepository()
// repository.searchCity('ho').subscribe(
// locations => console.log(locations),
// error => console.log(error)
// )
7 changes: 4 additions & 3 deletions repositories/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "weather-repositories",
"version": "0.0.1",
"version": "0.0.2",
"description": "Repository",
"main": "./dist/index.js",
"scripts": {
"build": "yarn install && tsc",
"test": "jest",
"debug": "node --inspect-brk"
},
Expand All @@ -14,13 +15,13 @@
"author": "Kien Tran",
"license": "MIT",
"dependencies": {
"@akanass/rx-http-request": "^3.0.0",
"@google/maps": "^0.5.5",
"@reactivex/rxjs": "^6.3.3",
"@types/googlemaps": "^3.30.13",
"@types/jest": "^23.3.3",
"axios": "^0.18.0",
"google-maps": "^3.3.0",
"react-native-google-maps-services": "^0.5.1",
"rxjs": "^6.3.3",
"weather-domain": "file:../domain"
},
"devDependencies": {
Expand Down
111 changes: 47 additions & 64 deletions weather/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,55 @@

import React from 'react';
import { Component } from 'react'
import { Platform, StyleSheet, Text, View, Image, SafeAreaView, Alert } from 'react-native';
import { Card, ListItem, Button, Icon } from 'react-native-elements'
import { Platform, StyleSheet, Text, View, Image, SafeAreaView, Alert, FlatList } from 'react-native';
import { Card, ListItem, Button, Icon, SearchBar } from 'react-native-elements'
import { Location } from 'weather-domain'
import { DefaultLocationRepository } from 'weather-repositories'

const location = new Location("kien", "ho chi minh")

const users = [
{
name: 'brynn',
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/brynn/128.jpg'
}
]

const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});

import * as Rx from 'rxjs'
import { map, throttle, throttleTime } from 'rxjs/operators'
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props)
}

render() {
return (
<SafeAreaView style={{ flex: 1, backgroundColor: '#fff' }}>

<Card title="aaa" >
{
users.map((u, i) => {
return (
<View key={i}>
<Image
resizeMode="cover"
source={{ uri: u.avatar }}
/>
<Text>{u.name}</Text>
</View>
);
})
}
</Card>
</SafeAreaView>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
state = {
locations: [new Location("test", "test")]
}
onChangeText = (text) => {

var repository = new DefaultLocationRepository()
repository.searchCity(text)
.pipe(throttleTime(1000))
.subscribe(
results => {
console.log(results)
this.setState({
locations: results
})
}
)
}

_renderItem = ({ item }) => (

<Text
title={item.name}
>{item.name}</Text>
)

render() {
return (
<SafeAreaView>
<SearchBar
onChangeText={this.onChangeText}
placeholder='Enter location'
></SearchBar>
<FlatList
data={this.state.locations}
renderItem={this._renderItem}

>

</FlatList>
</SafeAreaView>
)
}
}
Loading

0 comments on commit 3155271

Please sign in to comment.