|
| 1 | +/*********************************************** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2016 珠峰课堂,Ramroll |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | + |
| 26 | +import React, {Component} from 'react' |
| 27 | + |
| 28 | +import { |
| 29 | + View, |
| 30 | + Text, |
| 31 | + Image, |
| 32 | + Dimensions, |
| 33 | + ScrollView, |
| 34 | + ActivityIndicator, |
| 35 | + StyleSheet, |
| 36 | + RefreshControl |
| 37 | +} from 'react-native' |
| 38 | + |
| 39 | +import {format_currency, flexCenter} from "basic" |
| 40 | + |
| 41 | + |
| 42 | +import {COLOR_TITLE, COLOR_TEXT_LIGHT, COLOR_PRICE} from "domain/def" |
| 43 | + |
| 44 | + |
| 45 | +import {ListView} from 'basic' |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +const course_gen = () => { |
| 50 | + return { |
| 51 | + image : "http://a.hiphotos.baidu.com/image/h%3D200/sign=af9259bf03082838770ddb148898a964/6159252dd42a2834bc76c4ab5fb5c9ea14cebfba.jpg", |
| 52 | + title : "顶级大神教你写node.js", |
| 53 | + author : "张仁阳", |
| 54 | + description : "国内顶尖大神教你写node.js.从零开始,循序渐进.......", |
| 55 | + price : Math.random() * 10000 + 5000 |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +export class Home extends Component { |
| 61 | + |
| 62 | + |
| 63 | + constructor(){ |
| 64 | + super() |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + const courses = [] |
| 69 | + |
| 70 | + for(let i = 0; i < 20; i++) { |
| 71 | + courses.push(course_gen()) |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + this.state = { |
| 76 | + courses : courses, |
| 77 | + loading : false |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + _renderItem(course, i) { |
| 82 | + return ( |
| 83 | + <CourseCard {...course} /> |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + _onScrollToBottom(y){ |
| 89 | + |
| 90 | + this.y = y |
| 91 | + console.log(y) |
| 92 | + } |
| 93 | + |
| 94 | + _renderBottomIndicator(){ |
| 95 | + |
| 96 | + const indicator = this.y < 100 ? |
| 97 | + <ScrollIndicator image={require("./arrow_down.png")}>下拉加载更多</ScrollIndicator> |
| 98 | + : <ScrollIndicator image={require("./arrow_up.png")}>释放加载更多</ScrollIndicator> |
| 99 | + |
| 100 | + return ( |
| 101 | + <View style={{height : 42, ...flexCenter}}> |
| 102 | + {this.state.loading ? |
| 103 | + <ActivityIndicator /> |
| 104 | + : |
| 105 | + indicator |
| 106 | + } |
| 107 | + </View> |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + _release(){ |
| 112 | + if(this.y > 100 && !this.state.loading) { |
| 113 | + this.setState({ loading : true }, (() => { |
| 114 | + }).bind(this)) |
| 115 | + |
| 116 | + setTimeout((() => { |
| 117 | + const courses = [] |
| 118 | + for(let i = 0; i < 20; i++) { |
| 119 | + courses.push(course_gen()) |
| 120 | + } |
| 121 | + this.refs.listView.append(courses) |
| 122 | + this.setState({ |
| 123 | + loading : false |
| 124 | + }) |
| 125 | + }).bind(this), 2000) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + _refresh(){ |
| 132 | + |
| 133 | + if(!this.state.loading) { |
| 134 | + |
| 135 | + this.setState({loading : true}, (() => { |
| 136 | + setTimeout((() => { |
| 137 | + const courses = [] |
| 138 | + for(let i = 0; i < 20; i++) { |
| 139 | + courses.push(course_gen()) |
| 140 | + } |
| 141 | + this.refs.listView.reset(courses) |
| 142 | + this.setState({ |
| 143 | + loading : false |
| 144 | + }) |
| 145 | + }).bind(this), 2000) |
| 146 | + }).bind(this)) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + render(){ |
| 151 | + |
| 152 | + const loading = this.state |
| 153 | + return ( |
| 154 | + <View> |
| 155 | + <ListView |
| 156 | + ref="listView" |
| 157 | + initialData={this.state.courses} |
| 158 | + renderItem={this._renderItem} |
| 159 | + onScrollToBottom={this._onScrollToBottom.bind(this)} |
| 160 | + renderBottomIndicator={this._renderBottomIndicator.bind(this)} |
| 161 | + onResponderRelease={this._release.bind(this)} |
| 162 | + refreshControl={ |
| 163 | + <RefreshControl refreshing={false} onRefresh={this._refresh.bind(this)} /> |
| 164 | + } |
| 165 | + /> |
| 166 | + </View> |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | +} |
| 171 | + |
| 172 | +class CourseCard extends Component{ |
| 173 | + |
| 174 | + render() { |
| 175 | + const W = Dimensions.get("window").width |
| 176 | + |
| 177 | + const {image, title, author, description, price} = this.props |
| 178 | + |
| 179 | + return <View style={courseStyle.cardContainer}> |
| 180 | + <Image |
| 181 | + source={{uri : image}} |
| 182 | + style={{width : W - 20, height : (W - 20) * 0.3}} |
| 183 | + /> |
| 184 | + <Title>{title}</Title> |
| 185 | + <Author label="讲师">{author}</Author> |
| 186 | + <Description>{description}</Description> |
| 187 | + <Price>{price}</Price> |
| 188 | + </View> |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | + |
| 193 | +const courseStyle = StyleSheet.create({ |
| 194 | + cardContainer: { |
| 195 | + marginBottom: 20, |
| 196 | + paddingBottom: 10, |
| 197 | + marginLeft: 10, |
| 198 | + marginRight: 10, borderRadius: 5, overflow: "hidden", borderWidth: 1, borderColor: "#c7c8c9", |
| 199 | + } |
| 200 | + } |
| 201 | +) |
| 202 | + |
| 203 | +const Paragraph = { |
| 204 | + paddingLeft : 20, |
| 205 | + paddingRight : 20, |
| 206 | + marginTop : 10 |
| 207 | + |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +const Title = ({children}) => { |
| 213 | + return <Text style={{...Paragraph, color : COLOR_TITLE, fontSize : 18, fontWeight : 'bold'}}>{children}</Text> |
| 214 | + |
| 215 | +} |
| 216 | +/** |
| 217 | + * 等同于 |
| 218 | + * class Title extends Component { |
| 219 | + * render() { |
| 220 | + * const {children} = this.props |
| 221 | + * .... |
| 222 | + * } |
| 223 | + * } |
| 224 | + * |
| 225 | + */ |
| 226 | + |
| 227 | +const ScrollIndicator = ({children, image}) => { |
| 228 | + return <View style={{flexDirection : 'row', ...flexCenter}}> |
| 229 | + <Text>{children}</Text> |
| 230 | + <Image source={image} style={{width : 18, height : 18}} /> |
| 231 | + </View> |
| 232 | + |
| 233 | + |
| 234 | +} |
| 235 | + |
| 236 | + |
| 237 | +const Author = ({label, children}) => { |
| 238 | + return <Text style={{...Paragraph, color : COLOR_TEXT_LIGHT}}>{label}:{children}</Text> |
| 239 | +} |
| 240 | + |
| 241 | + |
| 242 | +const Description = ({children}) => { |
| 243 | + return <Text style={{...Paragraph, color : COLOR_TEXT_LIGHT}}>{children}</Text> |
| 244 | +} |
| 245 | + |
| 246 | +// 1000 => 1,000.00 |
| 247 | +// 1000.000 => 1,000.00 |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | +const Price = ({children}) => { |
| 252 | + return <Text style={{...Paragraph, color : COLOR_PRICE,fontSize : 18, fontWeight : 'bold'}}>¥{format_currency(children)}</Text> |
| 253 | +} |
0 commit comments