Skip to content

Commit 3cc999e

Browse files
add kdoc to RatingBar functions
1 parent 699b3eb commit 3cc999e

File tree

3 files changed

+207
-141
lines changed

3 files changed

+207
-141
lines changed

app/src/main/java/com/smarttoolfactory/composeratingbar/MainActivity.kt

Lines changed: 145 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import androidx.compose.material3.MaterialTheme
1818
import androidx.compose.material3.Slider
1919
import androidx.compose.material3.Surface
2020
import androidx.compose.material3.Text
21-
import androidx.compose.runtime.getValue
22-
import androidx.compose.runtime.mutableStateOf
23-
import androidx.compose.runtime.remember
24-
import androidx.compose.runtime.setValue
21+
import androidx.compose.runtime.*
2522
import androidx.compose.ui.Modifier
2623
import androidx.compose.ui.graphics.Color
2724
import androidx.compose.ui.graphics.ImageBitmap
@@ -45,146 +42,153 @@ class MainActivity : ComponentActivity() {
4542
modifier = Modifier.fillMaxSize(),
4643
color = MaterialTheme.colorScheme.background
4744
) {
45+
RatingbarDemo()
4846

49-
var rating by remember { mutableStateOf(3.7f) }
50-
var rating2 by remember { mutableStateOf(3.7f) }
51-
var rating3 by remember { mutableStateOf(2.3f) }
52-
var rating4 by remember { mutableStateOf(4.5f) }
53-
var rating5 by remember { mutableStateOf(1.7f) }
54-
var rating6 by remember { mutableStateOf(5f) }
55-
56-
val imageBackground = ImageBitmap.imageResource(id = R.drawable.star_background)
57-
val imageForeground = ImageBitmap.imageResource(id = R.drawable.star_foreground)
58-
59-
Column(modifier = Modifier.fillMaxSize()) {
60-
RatingBar(
61-
rating = rating,
62-
space = 2.dp,
63-
imageBackground = imageBackground,
64-
imageForeground = imageForeground,
65-
animationEnabled = false,
66-
gestureEnabled = true,
67-
itemSize = 60.dp
68-
) {
69-
rating = it
70-
}
71-
72-
Text(
73-
"Rating: $rating",
74-
fontSize = 16.sp,
75-
color = MaterialTheme.colorScheme.primary
76-
)
77-
78-
val purple500 = Color(0xff9C27B0)
79-
80-
RatingBar(
81-
rating = rating2,
82-
painterBackground = painterResource(id = R.drawable.star_background),
83-
painterForeground = painterResource(id = R.drawable.star_foreground),
84-
animationEnabled = false,
85-
gestureEnabled = false,
86-
tint = purple500,
87-
shimmer = Shimmer(
88-
colors = listOf(
89-
purple500.copy(.9f),
90-
purple500.copy(.3f),
91-
purple500.copy(.9f)
92-
)
93-
),
94-
itemSize = 60.dp
95-
) {
96-
rating2 = it
97-
}
98-
99-
Slider(
100-
value = rating2,
101-
onValueChange = { rating2 = it },
102-
valueRange = 0f..5f
103-
)
104-
105-
Text(
106-
"Rating: $rating2",
107-
fontSize = 16.sp,
108-
color = MaterialTheme.colorScheme.primary
109-
)
110-
111-
RatingBar(
112-
rating = rating3,
113-
painterBackground = painterResource(id = R.drawable.star_background),
114-
painterForeground = painterResource(id = R.drawable.star_foreground),
115-
tint = Color(0xff795548),
116-
animationEnabled = true,
117-
itemSize = 60.dp
118-
) {
119-
rating3 = it
120-
}
121-
122-
val pink500 = Color(0xffE91E63)
123-
RatingBar(
124-
rating = rating4,
125-
space = 2.dp,
126-
imageVectorBackground = Icons.Default.FavoriteBorder,
127-
imageVectorForeground = Icons.Default.Favorite,
128-
shimmer = Shimmer(
129-
color = pink500,
130-
animationSpec = infiniteRepeatable(
131-
animation = tween(durationMillis = 1000, easing = LinearEasing),
132-
repeatMode = RepeatMode.Reverse
133-
)
134-
),
135-
tint = pink500,
136-
itemSize = 40.dp
137-
) {
138-
rating4 = it
139-
}
140-
141-
RatingBar(
142-
rating = rating5,
143-
space = 2.dp,
144-
imageVectorBackground = ImageVector.vectorResource(id = R.drawable.outline_wb_cloudy_24),
145-
imageVectorForeground = ImageVector.vectorResource(id = R.drawable.baseline_wb_cloudy_24),
146-
tint = Color(0xff2196F3),
147-
itemSize = 60.dp
148-
) {
149-
rating5 = it
150-
}
151-
152-
RatingBar(
153-
rating = rating6,
154-
imageVectorBackground = ImageVector.vectorResource(id = R.drawable.twotone_person_24),
155-
imageVectorForeground = ImageVector.vectorResource(id = R.drawable.baseline_person_24),
156-
tint = Color(0xff795548),
157-
itemSize = 40.dp
158-
) {
159-
rating6 = it
160-
}
161-
162-
RatingBar(
163-
rating = 4.5f,
164-
space = 2.dp,
165-
itemCount = 10,
166-
imageBackground = imageBackground,
167-
imageForeground = imageForeground,
168-
shimmer = Shimmer()
169-
)
170-
Spacer(modifier=Modifier.height(10.dp))
171-
172-
RatingBar(
173-
rating = 8.3f,
174-
space = 4.dp,
175-
itemCount = 10,
176-
imageBackground = imageBackground,
177-
imageForeground = imageForeground,
178-
shimmer = Shimmer(
179-
animationSpec = infiniteRepeatable(
180-
animation = tween(durationMillis = 3000, easing = LinearEasing),
181-
repeatMode = RepeatMode.Restart
182-
)
183-
)
184-
)
185-
}
18647
}
18748
}
18849
}
18950
}
19051
}
52+
53+
@Composable
54+
private fun RatingbarDemo() {
55+
Column(modifier = Modifier.fillMaxSize()) {
56+
var rating by remember { mutableStateOf(3.7f) }
57+
var rating2 by remember { mutableStateOf(3.7f) }
58+
var rating3 by remember { mutableStateOf(2.3f) }
59+
var rating4 by remember { mutableStateOf(4.5f) }
60+
var rating5 by remember { mutableStateOf(1.7f) }
61+
var rating6 by remember { mutableStateOf(5f) }
62+
63+
val imageBackground = ImageBitmap.imageResource(id = R.drawable.star_background)
64+
val imageForeground = ImageBitmap.imageResource(id = R.drawable.star_foreground)
65+
66+
Column(modifier = Modifier.fillMaxSize()) {
67+
RatingBar(
68+
rating = rating,
69+
space = 2.dp,
70+
imageBackground = imageBackground,
71+
imageForeground = imageForeground,
72+
animationEnabled = false,
73+
gestureEnabled = true,
74+
itemSize = 60.dp
75+
) {
76+
rating = it
77+
}
78+
79+
Text(
80+
"Rating: $rating",
81+
fontSize = 16.sp,
82+
color = MaterialTheme.colorScheme.primary
83+
)
84+
85+
val purple500 = Color(0xff9C27B0)
86+
87+
RatingBar(
88+
rating = rating2,
89+
painterBackground = painterResource(id = R.drawable.star_background),
90+
painterForeground = painterResource(id = R.drawable.star_foreground),
91+
animationEnabled = false,
92+
gestureEnabled = false,
93+
tint = purple500,
94+
shimmer = Shimmer(
95+
colors = listOf(
96+
purple500.copy(.9f),
97+
purple500.copy(.3f),
98+
purple500.copy(.9f)
99+
)
100+
),
101+
itemSize = 60.dp
102+
) {
103+
rating2 = it
104+
}
105+
106+
Slider(
107+
value = rating2,
108+
onValueChange = { rating2 = it },
109+
valueRange = 0f..5f
110+
)
111+
112+
Text(
113+
"Rating: $rating2",
114+
fontSize = 16.sp,
115+
color = MaterialTheme.colorScheme.primary
116+
)
117+
118+
RatingBar(
119+
rating = rating3,
120+
painterBackground = painterResource(id = R.drawable.star_background),
121+
painterForeground = painterResource(id = R.drawable.star_foreground),
122+
tint = Color(0xff795548),
123+
animationEnabled = true,
124+
itemSize = 60.dp
125+
) {
126+
rating3 = it
127+
}
128+
129+
val pink500 = Color(0xffE91E63)
130+
RatingBar(
131+
rating = rating4,
132+
space = 2.dp,
133+
imageVectorBackground = Icons.Default.FavoriteBorder,
134+
imageVectorForeground = Icons.Default.Favorite,
135+
shimmer = Shimmer(
136+
color = pink500,
137+
animationSpec = infiniteRepeatable(
138+
animation = tween(durationMillis = 1000, easing = LinearEasing),
139+
repeatMode = RepeatMode.Reverse
140+
)
141+
),
142+
tint = pink500,
143+
itemSize = 40.dp
144+
) {
145+
rating4 = it
146+
}
147+
148+
RatingBar(
149+
rating = rating5,
150+
space = 2.dp,
151+
imageVectorBackground = ImageVector.vectorResource(id = R.drawable.outline_wb_cloudy_24),
152+
imageVectorForeground = ImageVector.vectorResource(id = R.drawable.baseline_wb_cloudy_24),
153+
tint = Color(0xff2196F3),
154+
itemSize = 60.dp
155+
) {
156+
rating5 = it
157+
}
158+
159+
RatingBar(
160+
rating = rating6,
161+
imageVectorBackground = ImageVector.vectorResource(id = R.drawable.twotone_person_24),
162+
imageVectorForeground = ImageVector.vectorResource(id = R.drawable.baseline_person_24),
163+
tint = Color(0xff795548),
164+
itemSize = 40.dp
165+
) {
166+
rating6 = it
167+
}
168+
169+
RatingBar(
170+
rating = 4.5f,
171+
space = 2.dp,
172+
itemCount = 10,
173+
imageBackground = imageBackground,
174+
imageForeground = imageForeground,
175+
shimmer = Shimmer()
176+
)
177+
Spacer(modifier=Modifier.height(10.dp))
178+
179+
RatingBar(
180+
rating = 8.3f,
181+
space = 4.dp,
182+
itemCount = 10,
183+
imageBackground = imageBackground,
184+
imageForeground = imageForeground,
185+
shimmer = Shimmer(
186+
animationSpec = infiniteRepeatable(
187+
animation = tween(durationMillis = 3000, easing = LinearEasing),
188+
repeatMode = RepeatMode.Restart
189+
)
190+
)
191+
)
192+
}
193+
}
194+
}

ratingbar/src/main/java/com/smarttoolfactory/ratingbar/RatingBar.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ import kotlinx.coroutines.launch
3030
/**
3131
* Rating bar that can be used for setting rating by passing a fixed value or using gestures
3232
* to change current [rating]
33+
*
34+
* @param rating value to be set on this rating bar
35+
* @param imageBackground background for rating items. Item with borders to
36+
* show empty values
37+
* @param imageForeground foreground for rating items. Filled item to show percentage of rating
38+
* @param tint color for background and foreground items
39+
* @param itemSize size of the rating item to be displayed. This is intrinsic size of image
40+
* or vector file by default
41+
* @param animationEnabled when animation is enabled tap on any value is animated
42+
* @param gestureEnabled when gesture is not enabled value can only be changed by setting [rating]
43+
* @param shimmer shimmer effect for having a glow
44+
* @param itemCount maximum number of items
45+
* @param space space between rating items in dp
46+
* @param onRatingChange callback to notify user when rating has changed. This is helpful
47+
* for getting change after tap or drag gesture
48+
*
3349
*/
3450
@Composable
3551
fun RatingBar(
@@ -81,6 +97,26 @@ fun RatingBar(
8197
)
8298
}
8399

100+
/**
101+
* Rating bar that can be used for setting rating by passing a fixed value or using gestures
102+
* to change current [rating]
103+
*
104+
* @param rating value to be set on this rating bar
105+
* @param painterBackground background for rating items. Item with borders to
106+
* show empty values
107+
* @param painterForeground foreground for rating items. Filled item to show percentage of rating
108+
* @param tint color for background and foreground items
109+
* @param itemSize size of the rating item to be displayed. This is intrinsic size of image
110+
* or vector file by default
111+
* @param animationEnabled when animation is enabled tap on any value is animated
112+
* @param gestureEnabled when gesture is not enabled value can only be changed by setting [rating]
113+
* @param shimmer shimmer effect for having a glow
114+
* @param itemCount maximum number of items
115+
* @param space space between rating items in dp
116+
* @param onRatingChange callback to notify user when rating has changed. This is helpful
117+
* for getting change after tap or drag gesture
118+
*
119+
*/
84120
@Composable
85121
fun RatingBar(
86122
modifier: Modifier = Modifier,
@@ -132,6 +168,26 @@ fun RatingBar(
132168
)
133169
}
134170

171+
/**
172+
* Rating bar that can be used for setting rating by passing a fixed value or using gestures
173+
* to change current [rating]
174+
*
175+
* @param rating value to be set on this rating bar
176+
* @param imageVectorBackground background for rating items. Item with borders to
177+
* show empty values
178+
* @param imageVectorForeground foreground for rating items. Filled item to show percentage of rating
179+
* @param tint color for background and foreground items
180+
* @param itemSize size of the rating item to be displayed. This is intrinsic size of image
181+
* or vector file by default
182+
* @param animationEnabled when animation is enabled tap on any value is animated
183+
* @param gestureEnabled when gesture is not enabled value can only be changed by setting [rating]
184+
* @param shimmer shimmer effect for having a glow
185+
* @param itemCount maximum number of items
186+
* @param space space between rating items in dp
187+
* @param onRatingChange callback to notify user when rating has changed. This is helpful
188+
* for getting change after tap or drag gesture
189+
*
190+
*/
135191
@Composable
136192
fun RatingBar(
137193
modifier: Modifier = Modifier,

ratingbar/src/main/java/com/smarttoolfactory/ratingbar/model/Shimmer.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ internal data class ShimmerData(
1111
val progress: Float,
1212
)
1313

14+
/**
15+
* Shimmer for rating items
16+
* @param colors colors that are displayed on brush
17+
* @param animationSpec [InfiniteRepeatableSpec] to set animation and repeat modes either repeat
18+
* or reverse.
19+
*/
1420
@Immutable
1521
data class Shimmer(
1622
val colors: List<Color> = listOf(

0 commit comments

Comments
 (0)