|
1 | 1 | <template>
|
2 | 2 | <Page>
|
3 |
| - <CollectionView for="item in listItemsObservable" |
4 |
| - separatorColor="transparent"> |
| 3 | + <CollectionView for="item in listItems" |
| 4 | + separatorColor="transparent"> |
| 5 | + |
| 6 | + |
| 7 | + <!-- A post with a single image --> |
| 8 | + <v-template if="item.type === 'post.image.single'"> |
| 9 | + <ListItem :item="item" @toggleLike="togglePostLike(item)"> |
| 10 | + <GridLayout rows="auto, auto"> |
| 11 | + <Label textWrap :text="item.body" class="content"/> |
| 12 | + |
| 13 | + <Label backgroundColor="red" |
| 14 | + height="200" |
| 15 | + stretch="aspectFill" |
| 16 | + row="1"/> |
| 17 | + </GridLayout> |
| 18 | + </ListItem> |
| 19 | + </v-template> |
| 20 | + |
| 21 | + <!-- A post with 2 images --> |
| 22 | + <v-template if="item.type === 'post.image.double'"> |
| 23 | + <ListItem :item="item" @toggleLike="togglePostLike(item)"> |
| 24 | + <GridLayout rows="auto, auto" columns="*, *"> |
| 25 | + <Label textWrap :text="item.body" class="content" colSpan="2"/> |
| 26 | + |
| 27 | + <Label backgroundColor="red" |
| 28 | + height="200" |
| 29 | + stretch="aspectFill" |
| 30 | + col="0" |
| 31 | + row="1"/> |
| 32 | + <Label backgroundColor="green" |
| 33 | + height="200" |
| 34 | + stretch="aspectFill" |
| 35 | + col="1" |
| 36 | + row="1"/> |
| 37 | + </GridLayout> |
| 38 | + </ListItem> |
| 39 | + </v-template> |
| 40 | + |
| 41 | + <!-- A post with more than 2 images --> |
| 42 | + <v-template if="item.type === 'post.image.multiple'"> |
| 43 | + <ListItem :item="item" @toggleLike="togglePostLike(item)"> |
| 44 | + <GridLayout rows="auto, auto, auto" columns="2*, *"> |
| 45 | + <Label textWrap :text="item.body" class="content" colSpan="2"/> |
| 46 | + |
| 47 | + <Label backgroundColor="red" |
| 48 | + height="200" |
| 49 | + stretch="aspectFill" |
| 50 | + col="0" |
| 51 | + row="1" |
| 52 | + rowSpan="2"/> |
| 53 | + <Label backgroundColor="green" |
| 54 | + height="100" |
| 55 | + stretch="aspectFill" |
| 56 | + col="1" |
| 57 | + row="1"/> |
| 58 | + <Label backgroundColor="blue" |
| 59 | + height="100" |
| 60 | + stretch="aspectFill" |
| 61 | + col="1" |
| 62 | + row="2"/> |
| 63 | + </GridLayout> |
| 64 | + </ListItem> |
| 65 | + </v-template> |
| 66 | + |
| 67 | + <!-- A default post if there are no events or images --> |
5 | 68 | <v-template if="item.type === 'post'">
|
6 |
| - <!--<Label :text="JSON.stringify(item, null, 2)"/>--> |
7 |
| - <GridLayout rows="50, *, 50" margin="8" background="white" ___height="800px"> |
8 |
| - <!-- Top Actions --> |
9 |
| - <Label backgroundColor="rgba(255, 0, 0, 0.2)"/> |
10 |
| - |
11 |
| - <!-- Content --> |
12 |
| - <Label :text="item.body" textWrap row="1"/> |
13 |
| - <!--<Label text="Static text to test" textWrap row="1"/>--> |
14 |
| - |
15 |
| - <!-- Bottom Actions --> |
16 |
| - <Label @tap="togglePostLike(item, $index)" |
17 |
| - :text="item.liked ? 'Unlike' : 'Like'" |
18 |
| - backgroundColor="rgba(0, 0, 255, 0.2)" |
19 |
| - row="2"/> |
20 |
| - </GridLayout> |
| 69 | + <ListItem :item="item" @toggleLike="togglePostLike(item)"/> |
21 | 70 | </v-template>
|
22 | 71 |
|
23 |
| - <!--<v-template if="item.type === 'indicator' && item.state === 'loading'">--> |
24 |
| - <!-- <GridLayout rows="auto" padding="24">--> |
25 |
| - <!-- <Label text="Loading..." row="1"/>--> |
26 |
| - <!-- </GridLayout>--> |
27 |
| - <!--</v-template>--> |
28 |
| - <!--<v-template if="item.type === 'indicator' && item.state === 'end'">--> |
29 |
| - <!-- <GridLayout rows="auto" padding="24">--> |
30 |
| - <!-- <Label text="Reached end." row="1"/>--> |
| 72 | + <v-template> |
| 73 | + <ListItem :item="item" @toggleLike="togglePostLike(item)"/> |
| 74 | + </v-template> |
| 75 | + |
| 76 | + <!--<v-template if="item.type === 'post'">--> |
| 77 | + <!-- <!–<Label :text="JSON.stringify(item, null, 2)"/>–>--> |
| 78 | + <!-- <GridLayout rows="50, *, 50" margin="8" background="white" ___height="800px">--> |
| 79 | + <!-- <!– Top Actions –>--> |
| 80 | + <!-- <Label backgroundColor="rgba(255, 0, 0, 0.2)"/>--> |
| 81 | + |
| 82 | + <!-- <!– Content –>--> |
| 83 | + <!-- <Label :text="item.body" textWrap row="1"/>--> |
| 84 | + <!-- <!–<Label text="Static text to test" textWrap row="1"/>–>--> |
| 85 | + |
| 86 | + <!-- <!– Bottom Actions –>--> |
| 87 | + <!-- <Label @tap="togglePostLike(item, $index)"--> |
| 88 | + <!-- :text="item.liked ? 'Unlike' : 'Like'"--> |
| 89 | + <!-- backgroundColor="rgba(0, 0, 255, 0.2)"--> |
| 90 | + <!-- row="2"/>--> |
31 | 91 | <!-- </GridLayout>-->
|
32 | 92 | <!--</v-template>-->
|
| 93 | + |
| 94 | + <v-template if="item.type === 'indicator' && item.state === 'loading'"> |
| 95 | + <GridLayout rows="auto" padding="24"> |
| 96 | + <Label text="Loading..." row="1"/> |
| 97 | + </GridLayout> |
| 98 | + </v-template> |
| 99 | + <v-template if="item.type === 'indicator' && item.state === 'end'"> |
| 100 | + <GridLayout rows="auto" padding="24"> |
| 101 | + <Label text="Reached end." row="1"/> |
| 102 | + </GridLayout> |
| 103 | + </v-template> |
33 | 104 | </CollectionView>
|
34 | 105 | </Page>
|
35 | 106 | </template>
|
36 | 107 |
|
37 | 108 | <script>
|
| 109 | + import ListItem from './ListItem' |
38 | 110 | import {ObservableArray} from '@nativescript/core'
|
39 |
| -
|
| 111 | + import {loremIpsum} from 'lorem-ipsum' |
| 112 | +
|
| 113 | + const getType = (images, event) => { |
| 114 | + let type = 'post' |
| 115 | +
|
| 116 | + if (event && images === 0) { |
| 117 | + type = 'post.event' |
| 118 | + } else if (images === 1) { |
| 119 | + type = 'post.image.single' |
| 120 | + } else if (images === 2) { |
| 121 | + type = 'post.image.double' |
| 122 | + } else if (images > 2) { |
| 123 | + type = 'post.image.multiple' |
| 124 | + } |
| 125 | + return type |
| 126 | + } |
| 127 | + const genPost = (sentences, images = 0, event = false) => ({ |
| 128 | + liked: false, |
| 129 | + type: getType(images, event), |
| 130 | + body: loremIpsum({ |
| 131 | + count: sentences, |
| 132 | + format: 'html' |
| 133 | + }) |
| 134 | + }) |
40 | 135 | const loadItems = () => new Promise(resolve => setTimeout(() => {
|
41 | 136 | resolve({
|
42 | 137 | items: [
|
43 |
| - { |
44 |
| - liked: false, |
45 |
| - body: 'Hello World '.repeat(200) |
46 |
| - }, |
47 |
| - { |
48 |
| - liked: true, |
49 |
| - body: 'Something else... '.repeat(20) |
50 |
| - }, |
51 |
| - { |
52 |
| - liked: false, |
53 |
| - body: 'Something even else. '.repeat(40) |
54 |
| - }, |
55 |
| - { |
56 |
| - liked: true, |
57 |
| - body: 'Something else 2. '.repeat(50) |
58 |
| - }, |
| 138 | + genPost(1, 0, true), |
| 139 | + genPost(3, 1, false), |
| 140 | + genPost(1, 2, false), |
| 141 | + genPost(10, 5, false), |
| 142 | + genPost(1, 0, false), |
| 143 | + genPost(1, 4, false), |
| 144 | + genPost(1, 1, true), |
| 145 | + genPost(1, 0, true), |
| 146 | + genPost(10, 3, false), |
| 147 | + genPost(1, 2, false), |
| 148 | + genPost(10, 1, false), |
| 149 | + genPost(1, 1, false), |
| 150 | + genPost(10, 0, false), |
59 | 151 | ]
|
60 | 152 | })
|
61 | 153 | }, 1000))
|
|
118 | 210 | return
|
119 | 211 | }
|
120 | 212 |
|
121 |
| - this.feedItems = res.items.map(item => { |
122 |
| - return { |
123 |
| - type: 'post', |
124 |
| - body: item.body, |
125 |
| - liked: !!item.liked |
126 |
| - } |
127 |
| - }) |
128 |
| -
|
| 213 | + this.feedItems = res.items |
129 | 214 | this.listState = ListStates.IDLE
|
130 | 215 |
|
131 | 216 | },
|
132 | 217 |
|
133 | 218 | async togglePostLike(item) {
|
134 | 219 | console.log('post like changed')
|
135 |
| -
|
136 | 220 | item.liked = !item.liked
|
137 |
| -
|
138 | 221 | // this.listItemsObservable.setItem(this.listItemsObservable.indexOf(item), {
|
139 | 222 | // ...item,
|
140 | 223 | // })
|
141 | 224 | }
|
| 225 | + }, |
| 226 | + components: { |
| 227 | + ListItem |
142 | 228 | }
|
143 | 229 | }
|
144 |
| -
|
145 |
| -
|
146 | 230 | </script>
|
0 commit comments