/**
 * 一简微博
 *
 * @author FantasyShao <fantasyshao@icloud.com>
 */

// Module dependencies
const React = require('react-native');
const url = require('url');

const config = require('./app/config');
const api = require('./app/api');
const common = require('./app/common');
const HomeView = require('./app/Home');

const {
  AppRegistry,
  View,
  WebView
} = React;

const OAUTH_URL = [
  'https://api.weibo.com/oauth2/authorize',
  '?client_id=' + config.app_key,
  '&response_type=code',
  '&redirect_uri=' + config.redirect_uri
].join('');

const Yijian = React.createClass({

  getInitialState () {
    return {
      login: false
    }
  },

  render () {
    if (this.state.login) {
      return (
        <HomeView />
      );
    }

    return (
      <View style={{flex: 1, marginTop: 20 }}>
        <WebView
          ref={'webview'}
          url={OAUTH_URL}
          automaticallyAdjustContentInsets={false}
          onNavigationStateChange={this.onNavigationStateChange}
          startInLoadingState={true}
          scalesPageToFit={true} />
      </View>);
  },

  onNavigationStateChange (navState) {
    var urlObj = url.parse(navState.url, true);

    if (urlObj.pathname === url.parse(config.redirect_uri).pathname && navState.loading) {
      // 获取code
      var code = urlObj.query.code;
      var auth_url = [config.auth_uri,
        '?client_id=' + config.app_key,
        '&client_secret=' + config.app_secret,
        '&grant_type=authorization_code',
        '&redirect_uri=' + config.redirect_uri,
        '&code=' + code
      ].join('');

      // 获取access_token
      fetch(auth_url, {
        method: 'post'
      }).then((response) => response.json())
        .then((responseData) => {
          this.setState({
            login: true
          });
          // 存储access_token
          common.storeItem(config.token_store_key, responseData.access_token);
          // 获取登录用户的uid
          fetch(api.account.getUid + '?access_token=' + responseData.access_token)
            .then((res) => res.json())
            .then((data) => {
              common.storeItem(config.uid_store_key, data.uid + '');
            })
            .done();
        })
        .catch((err) => console.log(err))
        .done();
    }
  }
});

AppRegistry.registerComponent('Yijian', () => Yijian);

@@ -347,16 +347,32 @@
  WebView
+,%0A  AsyncStorage
 %0A%7D = Rea
@@ -595,66 +595,405 @@
 %0A%0A  
-getInitialState () %7B%0A    return %7B%0A      login: false%0A    %7D
+async _loadInitialState () %7B%0A    try %7B%0A      let status = await AsyncStorage.getItem(config.login_statue_store_key);%0A%0A      if (status !== null) %7B%0A        this.setState(%7B%0A          login: status%0A        %7D);%0A      %7D%0A    %7D catch (e) %7B%0A      console.log(err);%0A    %7D%0A  %7D,%0A%0A  getInitialState () %7B%0A    return %7B%0A      login: false%0A    %7D%0A  %7D,%0A%0A  componentDidMount () %7B%0A    this._loadInitialState().done();
 %0A  %7D
@@ -2129,24 +2129,111 @@
         %7D);%0A
+          // %E5%AD%98%E5%82%A8%E7%99%BB%E5%BD%95%E7%8A%B6%E6%80%81%0A          common.storeItem(config.login_statue_store_key, 'true');%0A
           //

Store login status with AsyncStorage