Last active
October 28, 2025 11:08
-
-
Save Kyriakos-Georgiopoulos/14407b5c0950f85b6b2938dfc21e316f to your computer and use it in GitHub Desktop.
ThemeSwitch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright 2025 Kyriakos Georgiopoulos | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| import androidx.compose.animation.animateColorAsState | |
| import androidx.compose.animation.core.animateDpAsState | |
| import androidx.compose.animation.core.animateFloatAsState | |
| import androidx.compose.animation.core.tween | |
| import androidx.compose.foundation.Canvas | |
| import androidx.compose.foundation.Image | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.border | |
| import androidx.compose.foundation.clickable | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.offset | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.foundation.layout.size | |
| import androidx.compose.foundation.shape.CircleShape | |
| import androidx.compose.foundation.shape.RoundedCornerShape | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.getValue | |
| import androidx.compose.runtime.mutableStateOf | |
| import androidx.compose.runtime.remember | |
| import androidx.compose.runtime.setValue | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.draw.clip | |
| import androidx.compose.ui.draw.rotate | |
| import androidx.compose.ui.geometry.Offset | |
| import androidx.compose.ui.graphics.Brush | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.res.painterResource | |
| import androidx.compose.ui.tooling.preview.Preview | |
| import androidx.compose.ui.unit.dp | |
| import com.zengrip.R | |
| @Composable | |
| fun ThemeSwitch( | |
| isDarkTheme: Boolean, | |
| onCheckedChange: (Boolean) -> Unit, | |
| ) { | |
| // Animate circle’s movement smoothly | |
| val offset by animateDpAsState( | |
| targetValue = if (isDarkTheme) 96.dp else 4.dp, | |
| animationSpec = tween(durationMillis = 500) | |
| ) | |
| // Animate icon rotation (0 for day, 360 for night) | |
| val rotation by animateFloatAsState( | |
| targetValue = if (isDarkTheme) 360f else 0f, | |
| animationSpec = tween(durationMillis = 500) | |
| ) | |
| // Animate background color for sunset/sunrise | |
| val backgroundStart by animateColorAsState( | |
| targetValue = if (isDarkTheme) Color(0xFF000022) else Color(0xFFFFE082), | |
| animationSpec = tween(durationMillis = 1000) | |
| ) | |
| val backgroundEnd by animateColorAsState( | |
| targetValue = if (isDarkTheme) Color(0xFF000033) else Color(0xFFFFC107), | |
| animationSpec = tween(durationMillis = 1000) | |
| ) | |
| Box( | |
| modifier = Modifier | |
| .size(width = 160.dp, height = 70.dp) | |
| .clip(RoundedCornerShape(40.dp)) | |
| .border( | |
| width = 4.dp, | |
| color = if (isDarkTheme) { | |
| Color.White.copy(alpha = 0.5f) | |
| } else { | |
| Color(0xFFFFA500).copy(alpha = 0.4f) | |
| }, | |
| shape = RoundedCornerShape(40.dp) | |
| ) | |
| .clickable { onCheckedChange(!isDarkTheme) } | |
| .background( // Apply gradient directly | |
| Brush.linearGradient( | |
| colors = listOf(backgroundStart, backgroundEnd) | |
| ) | |
| ), | |
| contentAlignment = Alignment.CenterStart | |
| ) { | |
| // If it's dark, draw some "stars" | |
| if (isDarkTheme) { | |
| val numberOfStars = 30 | |
| val stars = remember { | |
| List(numberOfStars) { | |
| Star( | |
| x = (0..100).random().toFloat(), | |
| y = (0..100).random().toFloat(), | |
| radius = (1..3).random().toFloat() | |
| ) | |
| } | |
| } | |
| Canvas( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| .padding(10.dp) | |
| ) { | |
| val padding = 10f | |
| val width = size.width - padding * 2 | |
| val height = size.height - padding * 2 | |
| for (star in stars) { | |
| drawCircle( | |
| color = Color.White.copy(alpha = 0.8f), | |
| center = Offset( | |
| padding + star.x / 100 * width, | |
| padding + star.y / 100 * height | |
| ), | |
| radius = star.radius | |
| ) | |
| } | |
| } | |
| } | |
| Box( | |
| modifier = Modifier | |
| .offset(x = offset) | |
| .size(60.dp) | |
| .clip(CircleShape) | |
| .background(Color.White) | |
| .padding(6.dp), | |
| contentAlignment = Alignment.Center | |
| ) { | |
| Image( | |
| painter = painterResource(id = if (isDarkTheme) R.drawable.moon else R.drawable.sun), | |
| contentDescription = if (isDarkTheme) "Switch to Light" else "Switch to Dark", | |
| modifier = Modifier.size(40.dp) | |
| .rotate(rotation) | |
| ) | |
| } | |
| } | |
| } | |
| @Preview | |
| @Composable | |
| fun EnhancedThemeSwitchPreview() { | |
| var isDark by remember { mutableStateOf(false) } | |
| Box( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| .background(Color(0xFF303030)), | |
| contentAlignment = Alignment.Center | |
| ) { | |
| ThemeSwitch(isDark) { isDark = it } | |
| } | |
| } | |
| data class Star(val x: Float, val y: Float, val radius: Float) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment