-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
MainUiImpl.kt
133 lines (124 loc) · 4.97 KB
/
MainUiImpl.kt
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
/*
* Copyright 2019 Louis Cognault Ayeva Derman. Use of this source code is governed by the Apache 2.0 license.
*/
package com.example.splitties.main
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import com.example.splitties.R
import splitties.dimensions.dip
import splitties.resources.dimenPxSize
import splitties.resources.styledColor
import splitties.views.coroutines.awaitOneClick
import splitties.views.coroutines.material.showAndAwaitOneClickThenHide
import splitties.views.dsl.appcompat.toolbar
import splitties.views.dsl.coordinatorlayout.anchorTo
import splitties.views.dsl.coordinatorlayout.appBarLParams
import splitties.views.dsl.coordinatorlayout.coordinatorLayout
import splitties.views.dsl.coordinatorlayout.defaultLParams
import splitties.views.dsl.core.add
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.margin
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.textView
import splitties.views.dsl.core.verticalLayout
import splitties.views.dsl.material.EXIT_UNTIL_COLLAPSED
import splitties.views.dsl.material.MaterialComponentsStyles
import splitties.views.dsl.material.PIN
import splitties.views.dsl.material.SCROLL
import splitties.views.dsl.material.actionBarLParams
import splitties.views.dsl.material.appBarLayout
import splitties.views.dsl.material.collapsingToolbarLayout
import splitties.views.dsl.material.contentScrollingWithAppBarLParams
import splitties.views.dsl.material.defaultLParams
import splitties.views.dsl.material.floatingActionButton
import splitties.views.dsl.recyclerview.wrapInRecyclerView
import splitties.views.gravityCenterHorizontal
import splitties.views.gravityEndBottom
import splitties.views.imageResource
import splitties.views.material.contentScrimColor
import splitties.views.textResource
class MainUiImpl(override val ctx: Context) : MainAndroidUi {
override suspend fun awaitFabClick() = fab.showAndAwaitOneClickThenHide()
override suspend fun awaitLaunchMaterialListDemoRequest() = launchDemoBtn.awaitOneClick()
override suspend fun awaitLaunchPermissionDemoRequest() {
bePoliteWithPermissionsBtn.awaitOneClick()
}
override suspend fun awaitLaunchSayHelloDemoRequest() {
sayHelloBtn.awaitOneClick()
}
override suspend fun awaitToggleNightModeRequest() {
toggleNightModeBtn.awaitOneClick()
}
override suspend fun awaitTrySoundRequest() {
trySoundBtn.awaitOneClick()
}
private val materialStyles = MaterialComponentsStyles(ctx)
private val materialButtons = materialStyles.button
private val launchDemoBtn = materialButtons.text {
textResource = R.string.go_to_the_demo
}
private val bePoliteWithPermissionsBtn = materialButtons.filled {
textResource = R.string.be_polite_with_permissions
}
private val sayHelloBtn = materialButtons.text {
textResource = R.string.say_hello
}
private val toggleNightModeBtn = materialButtons.filledWithIcon {
setIconResource(R.drawable.ic_invert_colors_white_24dp)
textResource = R.string.toggle_night_mode
}
private val trySoundBtn = materialButtons.filled {
text = "Play a sound"
}
private val fab = floatingActionButton {
imageResource = R.drawable.ic_favorite_white_24dp
}
private val content = verticalLayout {
add(launchDemoBtn, lParams {
gravity = gravityCenterHorizontal
topMargin = dip(8)
})
add(bePoliteWithPermissionsBtn, lParams {
gravity = gravityCenterHorizontal
topMargin = dip(8)
})
add(sayHelloBtn, lParams {
gravity = gravityCenterHorizontal
topMargin = dip(8)
})
add(toggleNightModeBtn, lParams {
gravity = gravityCenterHorizontal
bottomMargin = dip(8)
})
add(trySoundBtn, lParams {
gravity = gravityCenterHorizontal
bottomMargin = dip(8)
})
add(textView {
textResource = R.string.large_text
}, lParams {
margin = dimenPxSize(R.dimen.text_margin)
})
}.wrapInRecyclerView()
private val appBar = appBarLayout(theme = R.style.AppTheme_AppBarOverlay) {
add(collapsingToolbarLayout {
fitsSystemWindows = true
contentScrimColor = styledColor(R.attr.colorPrimary)
add(toolbar {
(ctx as? AppCompatActivity)?.setSupportActionBar(this)
popupTheme = R.style.AppTheme_PopupOverlay
}, actionBarLParams(collapseMode = PIN))
}, defaultLParams(height = matchParent) {
scrollFlags = SCROLL or EXIT_UNTIL_COLLAPSED
})
}
override val root = coordinatorLayout {
fitsSystemWindows = true
add(appBar, appBarLParams(dip(180)))
add(content, contentScrollingWithAppBarLParams())
add(fab, defaultLParams {
anchorTo(appBar, gravity = gravityEndBottom)
margin = dip(16)
})
}
}