colorgenerator as extension of Color

This commit is contained in:
brreynie 2023-05-10 11:59:28 +02:00
parent 79ed70bb17
commit 00751aaf51
2 changed files with 13 additions and 5 deletions

View file

@ -0,0 +1,10 @@
package be.ugent.sel.studeez.common.ext
import androidx.compose.ui.graphics.Color
import kotlin.random.Random
fun Color.Companion.generateRandomArgb(): Long {
val random = Random
val mask: Long = (0x000000FFL shl random.nextInt(0, 3)).inv()
return random.nextLong(0xFF000000L, 0xFFFFFFFFL) and mask
}

View file

@ -2,6 +2,8 @@ package be.ugent.sel.studeez.screens.subjects.form
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.Color
import be.ugent.sel.studeez.common.ext.generateRandomArgb
import be.ugent.sel.studeez.data.SelectedSubject
import be.ugent.sel.studeez.data.local.models.task.Subject
import be.ugent.sel.studeez.domain.LogService
@ -10,7 +12,6 @@ import be.ugent.sel.studeez.navigation.StudeezDestinations
import be.ugent.sel.studeez.screens.StudeezViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlin.random.Random
abstract class SubjectFormViewModel(
protected val subjectDAO: SubjectDAO,
@ -43,12 +44,9 @@ class SubjectCreateFormViewModel @Inject constructor(
override val uiState = mutableStateOf(SubjectFormUiState())
fun onCreate(openAndPopUp: (String, String) -> Unit) {
val random = Random
val mask: Long = (0x000000FFL shl random.nextInt(0, 3)).inv()
val randomColor = random.nextLong(0xFF000000L, 0xFFFFFFFFL) and mask
val newSubject = Subject(
name = name,
argb_color = randomColor,
argb_color = Color.generateRandomArgb(),
)
subjectDAO.saveSubject(
newSubject