/
ListExamplesShared.scala
225 lines (201 loc) · 6.15 KB
/
ListExamplesShared.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package sri.mobile.examples.uiexplorer.components
import sri.core.{ComponentP, CreateElement}
import scalajsplus.OptDefault
import sri.mobile.examples.uiexplorer.images._
import sri.platform.SriPlatform
import sri.universal.apis.StyleSheet
import sri.universal.components._
import sri.universal.styles.InlineStyleSheetUniversal
import scala.scalajs.js
import scala.scalajs.js.JSConverters.JSRichGenTraversableOnce
import scala.scalajs.js.annotation.JSName
object ListExamplesShared {
val LOREM_IPSUM =
s"""
|Lorem ipsum dolor sit amet, ius ad pertinax oportere accommodare, an vix
|civibus corrumpit referrentur. Te nam case ludus inciderint, te mea facilisi adipiscing. Sea id
|integre luptatum. In tota sale consequuntur nec. Erat ocurreret mei ei. Eu paulo sapientem
|vulputate est, vel an accusam intellegam interesset. Nam eu stet pericula reprimique, ea vim illud
|modus, putant invidunt reprehendunt ne qui.;
""".stripMargin
trait Item extends js.Object {
val title: String
val key: Int
val text: String
val pressed: js.UndefOr[Boolean] = js.undefined
val noImage: js.UndefOr[Boolean] = js.undefined
}
def genItemData(count: Int): js.Array[Item] = {
(0 to count)
.map(i => {
val itemHash = js.Math.abs(hashCode2(s"Item $i"))
new Item {
override val text: String =
LOREM_IPSUM.substring(0, itemHash % 301 + 20)
override val key: Int = i
override val pressed: js.UndefOr[Boolean] = false
override val title: String = s"Item $i"
}.asInstanceOf[Item]
})
.toJSArray
}
val SEPARATOR_HEIGHT = StyleSheet.hairlineWidth
val SeparatorComponent = View(style = styles.separator)(null)
val HeaderComponent = ViewC(
View(style = styles.headerFooter)(
TextC("LIST HEADER")
),
SeparatorComponent
)
val FooterComponent = ViewC(
SeparatorComponent,
View(style = styles.headerFooter)(TextC("LIST FOOTER")))
val THUMB_URLS = js.Array(LikeImage,
DislikeImage,
CallImage,
FistImage,
BandagedImage,
FlowersImage,
HeartImage,
LikingImage,
PartyImage,
PokeImage,
SuperLikeImage,
VictoryImage)
val HORIZ_WIDTH = 200
class ItemComponent extends ComponentP[ItemComponent.Props] {
def render() = {
val itemHash = js.Math.abs(hashCode2(props.item.title))
val imageSource: js.Any = THUMB_URLS(itemHash % THUMB_URLS.length)
TouchableHighlight(
onPress = () => props.onPress(props.item.key),
style = if (props.horizontal) styles.horizItem else styles.item)(
View(
style =
if (props.horizontal)
js.Array(styles.itemRow, styles.hWidth)
else styles.itemRow)(
if (props.item.noImage.isEmpty)
Image(style = styles.thumb, sourceDynamic = imageSource)
else null,
Text(style = styles.text,
numberOfLines =
if (props.horizontal || props.fixedHeight) 3 else OptDefault)(
s"${props.item.title} - ${props.item.text}"
)
)
)
}
}
object ItemComponent {
case class Props(fixedHeight: Boolean = false,
horizontal: Boolean = false,
item: Item,
onPress: Int => Unit)
def apply(props: Props) = CreateElement[ItemComponent](props)
}
def hashCode2(str: String): Int = {
var hash = 15
(str.length - 1 to (0, -1)).foreach(i => {
hash = (hash << 5 - hash) + str.charAt(i).toInt
})
hash
}
@inline
def PlainInput(placeholder: String,
value: js.UndefOr[String] = js.undefined,
onChange: String => _) = {
TextInput(
autoCapitalize = TextInputAutoCapitalize.NONE,
autoCorrect = false,
clearButtonMode = "always",
underlineColorAndroid = "transparent",
style = styles.searchTextInput,
placeholder = placeholder,
value = value,
onChangeText = onChange
)
}
@inline
def renderSmallSwitchOption(key: String,
value: Boolean,
onChange: Boolean => _) = {
View(style = styles.option)(
TextC(key),
Switch(style = styles.smallSwitch,
value = value,
onValueChange = onChange)
)
}
object styles extends InlineStyleSheetUniversal {
import dsl._
val headerFooter = style(
height := 30,
width := 100,
alignSelf := "center",
alignItems := "center",
justifyContent := "center"
)
val horizItem = style(
alignSelf := "flex-start" // Necessary for touch highlight
)
val item = style(
flex := 1
)
val option = style(
flexDirection := "row",
padding := 8,
paddingRight := 0
)
val itemRow = style(
flexDirection := "row",
padding := 10,
backgroundColor := "#F6F6F6"
)
val searchTextInput = style(
backgroundColor := "white",
borderColor := "#cccccc",
borderRadius := 3,
borderWidth := 1,
paddingLeft := 8,
paddingVertical := 0,
height := 26,
fontSize := 14,
flexGrow := 1
)
val separator = style(
height := SEPARATOR_HEIGHT,
backgroundColor := "gray"
)
val smallSwitch =
if (SriPlatform.isAndroid)
style(
top := 1,
margin := -6
// transform := [{scale := 0.7}]
)
else
style(
top := 4,
margin := -10
// transform := [{scale := 0.5}]
)
val stacked = style(
alignItems := "center",
backgroundColor := "#F6F6F6",
padding := 10
)
val thumb = style(
width := 64,
height := 64
)
val stackedText = style(
padding := 4,
fontSize := 18
)
val text = style(
flex := 1
)
val hWidth = style(width := HORIZ_WIDTH)
}
}