Created
June 23, 2021 13:24
-
-
Save catalinghita8/0902fcfc6308e3202ac3cdb3f92290a6 to your computer and use it in GitHub Desktop.
This file contains 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
import android.os.Bundle | |
import androidx.activity.compose.setContent | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.shape.CircleShape | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.material.Card | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Surface | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.MutableState | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.res.painterResource | |
import androidx.compose.ui.text.font.FontWeight | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
import com.codingtroops.composesample.R | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val mockUserProfile = UserProfile.getMockProfileUser() | |
setContent { MainActivityComposable(mockUserProfile) } | |
} | |
} | |
@Composable | |
fun MainActivityComposable(userProfile: UserProfile) { | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = Helper.getGreenColor() | |
) | |
{ ProfileCardComposable(userProfile) } | |
} | |
@Composable | |
fun ProfileCardComposable(userProfile: UserProfile) { | |
val onlineStatus = remember { mutableStateOf(userProfile.isOnline) } | |
Card( | |
modifier = Modifier | |
.wrapContentSize() | |
.clickable(onClick = { onlineStatus.value = onlineStatus.value.not() }) | |
.clip(RoundedCornerShape(4.dp)) | |
.background(color = Helper.getWhiteColor()) | |
.padding(16.dp), | |
) { | |
Row(modifier = Modifier.height(intrinsicSize = IntrinsicSize.Max)) { | |
ProfilePictureComposable(onlineStatus, userProfile.profilePictureDrawableId) | |
ProfileContentComposable(onlineStatus, userProfile.name, userProfile.lastActivityMinutes) | |
} | |
} | |
} | |
@Composable | |
fun ProfilePictureComposable(onlineStatus: MutableState<Boolean>, pictureDrawableId: Int) { | |
Card( | |
shape = CircleShape, | |
border = BorderStroke(2.dp, color = if(onlineStatus.value) | |
Helper.getGreenColor() | |
else | |
Helper.getRedColor()), | |
modifier = Modifier | |
.size(48.dp), | |
elevation = 4.dp | |
) { | |
Image( | |
painter = painterResource(id = pictureDrawableId), | |
contentScale = ContentScale.Crop, | |
modifier = Modifier.size(48.dp), | |
contentDescription = "Profile picture holder" | |
) | |
} | |
} | |
@Composable | |
fun ProfileContentComposable(onlineStatus: MutableState<Boolean>, name: String, lastActivityMinutes: Int) { | |
Column( | |
modifier = Modifier | |
.fillMaxHeight() | |
.padding(start = 8.dp), | |
verticalArrangement = Arrangement.aligned(Alignment.CenterVertically) | |
) { | |
Text(name, fontWeight = FontWeight.Bold) | |
Text( | |
text = if (onlineStatus.value) | |
"Active now" | |
else | |
"$lastActivityMinutes mins ago", | |
style = MaterialTheme.typography.body2 | |
) | |
} | |
} | |
@Preview | |
@Composable | |
fun UserProfileCardPreview() { | |
val mockUserProfile = UserProfile.getMockProfileUser() | |
MainActivityComposable(mockUserProfile) | |
} | |
data class UserProfile( | |
val name: String, | |
val profilePictureDrawableId: Int, | |
val lastActivityMinutes: Int, | |
val isOnline: Boolean | |
) { | |
companion object { | |
fun getMockProfileUser() = UserProfile("Catalin Ghita", | |
R.drawable.profile_pic, | |
2, | |
true) | |
} | |
} | |
object Helper { | |
fun getColor(colorString: String) = | |
Color(android.graphics.Color.parseColor("#$colorString")) | |
fun getGreenColor() = getColor("9963890a") | |
fun getWhiteColor() = getColor("FFFFFFFF") | |
fun getRedColor() = getColor("99ac162c") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment