-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateCharacterActivity.kt
315 lines (287 loc) · 10.3 KB
/
CreateCharacterActivity.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.nimtome.app
import android.app.Activity
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ExtendedFloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Scaffold
import androidx.compose.material.ScaffoldState
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModelProvider
import com.nimtome.app.DndApplication.Companion.colorPalette
import com.nimtome.app.model.DndCharacter
import com.nimtome.app.model.DndClass
import com.nimtome.app.ui.components.ClassSelector
import com.nimtome.app.ui.components.DndTopBar
import com.nimtome.app.ui.components.LevelSelector
import com.nimtome.app.ui.logic.MAX_CHARACTER_LEVEL
import com.nimtome.app.ui.theme.DndSpellsTheme
import com.nimtome.app.viewmodel.CharacterViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
lateinit var characterViewModel: CharacterViewModel
fun addNewCharacter(character: DndCharacter) {
characterViewModel.insert(character)
}
class CreateCharacterActivity : ComponentActivity() {
companion object {
const val KEY_CHARACTER_NAME = "KEY_CHARACTER_NAME"
}
private var characterLiveData: LiveData<DndCharacter>? = null
@OptIn(ExperimentalMaterialApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
characterViewModel = ViewModelProvider(this)[CharacterViewModel::class.java]
val characterName = intent.getStringExtra(KEY_CHARACTER_NAME)
val isCreateMode = characterName.isNullOrBlank()
setContent {
MyApp {
var dndCharacter: DndCharacter? by remember { mutableStateOf(null) }
characterLiveData = characterName?.let { characterViewModel.get(it) }
val originalCharacter = characterLiveData?.observeAsState()
if (dndCharacter == null)
dndCharacter = if (!isCreateMode && originalCharacter != null) {
originalCharacter.value
} else {
DndCharacter()
}
if (dndCharacter == null)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxWidth()
) {
CircularProgressIndicator()
}
else
CreateCharacterScreenContent(
isCreateMode,
character = dndCharacter!!,
onChangeDndCharacter = { dndCharacter = it }
)
}
}
}
fun updateCharacter(character: DndCharacter) {
characterLiveData?.removeObservers(this)
characterViewModel.update(character)
}
fun deleteCharacter(character: DndCharacter) {
characterLiveData?.removeObservers(this)
characterViewModel.delete(character)
finish()
}
}
@ExperimentalMaterialApi
@Composable
fun CreateCharacterScreenContent(
isCreateMode: Boolean = true,
character: DndCharacter = DndCharacter(),
onChangeDndCharacter: (DndCharacter) -> Unit = {},
) {
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState()
val activity = LocalContext.current as? CreateCharacterActivity
val topText = when (isCreateMode) {
true -> "Create Your Character"
false -> "Modify Your Character"
}
Scaffold(
scaffoldState = scaffoldState,
modifier = Modifier.fillMaxSize(),
topBar = {
DndTopBar(
topText,
actions = {
if (!isCreateMode) {
IconButton(onClick = { activity?.deleteCharacter(character) }) {
Icon(Icons.Outlined.Delete, "delet character")
}
}
}
)
},
content = {
Column {
CharacterDetailList(
dndCharacter = character,
onChangeDndCharacter = {
onChangeDndCharacter(it.copy())
},
)
}
},
floatingActionButton = {
if (isCreateMode) {
CreateCharacterFloatingActionButton(
dndCharacter = character,
activity = activity,
scope = scope,
scaffoldState = scaffoldState
)
} else {
EditCharacterFloatingActionButton(
dndCharacter = character,
activity = activity,
scope = scope,
scaffoldState = scaffoldState
)
}
}
)
}
@Composable
fun EditCharacterFloatingActionButton(
dndCharacter: DndCharacter,
activity: CreateCharacterActivity?,
scope: CoroutineScope,
scaffoldState: ScaffoldState,
) {
val characterErrorText = stringResource(R.string.character_error)
ExtendedFloatingActionButton(
text = { Text(stringResource(R.string.modify_character)) },
onClick = {
val validation = validateCharacter(dndCharacter)
if (validation) {
activity?.updateCharacter(dndCharacter)
activity?.finish()
} else
scope.launch {
scaffoldState.snackbarHostState.showSnackbar(characterErrorText)
}
},
icon = { Icon(Icons.Outlined.Edit, contentDescription = null) },
)
}
@Composable
fun CreateCharacterFloatingActionButton(
dndCharacter: DndCharacter,
activity: Activity?,
scope: CoroutineScope,
scaffoldState: ScaffoldState,
) {
val characterErrorText = stringResource(R.string.character_error)
ExtendedFloatingActionButton(
text = { Text(stringResource(R.string.add_new_character)) },
onClick = {
val validation = validateCharacter(dndCharacter)
if (validation) {
addNewCharacter(dndCharacter)
activity?.finish()
} else
scope.launch {
scaffoldState.snackbarHostState.showSnackbar(characterErrorText)
}
},
icon = { Icon(Icons.Outlined.Add, contentDescription = null) }
)
}
fun validateCharacter(dndCharacter: DndCharacter): Boolean {
// General shit
var valid = true
if (dndCharacter.name.isEmpty())
valid = false
if (dndCharacter.dndClass == DndClass.None)
valid = false
if (dndCharacter.level > MAX_CHARACTER_LEVEL || dndCharacter.level < 0)
valid = false
return valid
}
@ExperimentalMaterialApi
@Composable
fun CharacterDetailList(
modifier: Modifier = Modifier,
dndCharacter: DndCharacter,
onChangeDndCharacter: (DndCharacter) -> Unit,
) {
val classList = DndClass.values().toList().subList(0, DndClass.values().size - 1)
Column {
OutlinedTextField(
value = dndCharacter.name,
onValueChange = {
onChangeDndCharacter(dndCharacter.copy(name = it))
},
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 25.dp, vertical = 10.dp),
label = { Text(text = stringResource(id = R.string.character_name)) }
)
ClassSelector(
modifier
.fillMaxWidth()
.padding(top = 10.dp, start = 25.dp, end = 25.dp, bottom = 15.dp),
allClasses = classList,
onClassChange = {
onChangeDndCharacter(dndCharacter.copy(dndClass = it))
},
starterClass = dndCharacter.dndClass
)
LevelSelector(
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 25.dp, vertical = 0.dp),
level = dndCharacter.level,
onLevelChange = {
onChangeDndCharacter(dndCharacter.copy(level = it))
},
)
}
}
@Composable
fun MyApp(component: @Composable () -> Unit) {
DndSpellsTheme(darkColors = colorPalette.darkColors, lightColors = colorPalette.lightColors) {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
component()
}
}
}
@ExperimentalMaterialApi
@Preview(showBackground = true)
@Composable
fun CreateCharacterPreview() {
DndSpellsTheme {
Surface {
CreateCharacterScreenContent()
}
}
}
@ExperimentalMaterialApi
@Preview(showBackground = true)
@Composable
fun CreateCharacterDarkPreview() {
DndSpellsTheme(darkTheme = true) {
Surface(color = MaterialTheme.colors.background) {
CreateCharacterScreenContent()
}
}
}