Skip to content

Commit

Permalink
Create small and reusable components for twitter demos (#138)
Browse files Browse the repository at this point in the history
* fix: connectedCheck need more memory to run.

* refactor(twitter): Create small and reusable components
  • Loading branch information
GerardPaligot authored Nov 16, 2021
1 parent c8d0680 commit a768ba3
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ android {
composeOptions {
kotlinCompilerExtensionVersion = ProjectConfigs.kotlinCompilerExtensionVersion
}
dexOptions {
javaMaxHeapSize = "4G"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.content.ContextCompat
import com.guru.composecookbook.data.DemoDataProvider
import com.guru.composecookbook.theme.ComposeCookBookTheme
import com.guru.composecookbook.twitter.components.TwitterHome

class TwitterActivity : ComponentActivity() {
@ExperimentalAnimationApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)
setContent {
ComposeCookBookTheme {
TwitterHome()
val tweets = remember { DemoDataProvider.tweetList }
TwitterHome(
tweets = tweets,
onMessagesClick = { /*TODO*/ },
onRetweetClick = { /*TODO*/ },
onLikesClick = { /*TODO*/ },
onShareClick = { /*TODO*/ },
onNewTweetClicked = { /*TODO*/ }
)
}
}
}
Expand All @@ -34,6 +43,13 @@ class TwitterActivity : ComponentActivity() {
@Composable
fun DefaultPreview4() {
ComposeCookBookTheme {
TwitterHome()
TwitterHome(
tweets = DemoDataProvider.tweetList,
onMessagesClick = { /*TODO*/ },
onRetweetClick = { /*TODO*/ },
onLikesClick = { /*TODO*/ },
onShareClick = { /*TODO*/ },
onNewTweetClicked = { /*TODO*/ }
)
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
package com.guru.composecookbook.twitter.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.StarBorder
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.guru.composecookbook.data.DemoDataProvider
import com.guru.composecookbook.data.model.Tweet
import com.guru.composecookbook.theme.twitterColor
import com.guru.composecookbook.twitter.R
import com.guru.composecookbook.twitter.components.profiles.ProfilePicture
import com.guru.composecookbook.twitter.components.profiles.ProfilePictureSizes
import com.guru.composecookbook.twitter.components.tweets.TweetItem

@Composable
fun TwitterHome() {
fun TwitterHome(
tweets: List<Tweet>,
onMessagesClick: () -> Unit,
onRetweetClick: () -> Unit,
onLikesClick: () -> Unit,
onShareClick: () -> Unit,
onNewTweetClicked: () -> Unit,
modifier: Modifier = Modifier
) {
Scaffold(
modifier = modifier,
topBar = {
TopAppBar(
title = {
Expand All @@ -40,13 +46,10 @@ fun TwitterHome() {
contentColor = MaterialTheme.colors.onSurface,
elevation = 8.dp,
navigationIcon = {
Image(
painter = painterResource(id = R.drawable.p6),
contentDescription = null,
modifier = Modifier
.padding(vertical = 4.dp, horizontal = 8.dp)
.size(32.dp)
.clip(CircleShape)
ProfilePicture(
profileImageId = R.drawable.p6,
size = ProfilePictureSizes.small,
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
)
},
actions = {
Expand All @@ -59,47 +62,43 @@ fun TwitterHome() {
)
},
floatingActionButton = {
val rippleExplode = remember { mutableStateOf(false) }
FloatingActionButton(rippleExplode)
if (rippleExplode.value) {

}
ExtendedFloatingActionButton(
text = { Text(text = "Tweet") },
icon = {
Icon(
painter = painterResource(id = R.drawable.ic_twitter),
contentDescription = null
)
},
onClick = onNewTweetClicked,
backgroundColor = twitterColor
)
},
content = {
TwitterHomeContent()
LazyColumn {
items(tweets) {
TweetItem(
tweet = it,
onMessagesClick = onMessagesClick,
onRetweetClick = onRetweetClick,
onLikesClick = onLikesClick,
onShareClick = onShareClick,
)
}
}
}
)
}

@Composable
fun TwitterHomeContent() {
val tweets = remember { DemoDataProvider.tweetList }
LazyColumn {
items(
items = tweets,
itemContent = {
TwitterListItem(tweet = it)
})
}
}

@Composable
fun FloatingActionButton(rippleExplode: MutableState<Boolean>) {
ExtendedFloatingActionButton(
text = { Text(text = "Tweet") },
icon = {
Icon(
painter = painterResource(id = R.drawable.ic_twitter),
contentDescription = null
)
},
onClick = { rippleExplode.value = !rippleExplode.value },
backgroundColor = twitterColor
)
}

@Preview
@Composable
fun ShowTwitterScreen() {
TwitterHome()
TwitterHome(
tweets = DemoDataProvider.tweetList,
onMessagesClick = { /*TODO*/ },
onRetweetClick = { /*TODO*/ },
onLikesClick = { /*TODO*/ },
onShareClick = { /*TODO*/ },
onNewTweetClicked = { /*TODO*/ }
)
}

This file was deleted.

Loading

0 comments on commit a768ba3

Please sign in to comment.