Skip to content

Commit 1df8b84

Browse files
All done
1 parent e0bbfe6 commit 1df8b84

File tree

8 files changed

+166
-51
lines changed

8 files changed

+166
-51
lines changed

app/build.gradle.kts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,6 @@ dependencies {
6969
debugImplementation(libs.androidx.ui.tooling)
7070
debugImplementation(libs.androidx.ui.test.manifest)
7171

72-
73-
// // Dagar Hilt
74-
// implementation("com.google.dagger:hilt-android:2.44")
75-
// kapt("com.google.dagger:hilt-android-compiler:2.44")
76-
//
77-
//
78-
// // Room Database
79-
// val room_version = "2.6.1"
80-
//
81-
// implementation("androidx.room:room-runtime:$room_version")
82-
// annotationProcessor("androidx.room:room-compiler:$room_version")
83-
// implementation("androidx.room:room-guava:$room_version")
84-
//
85-
// // To use Kotlin Symbol Processing (KAPT)
86-
// kapt("androidx.room:room-compiler:$room_version")
87-
//
88-
//
89-
// // Hilt ViewModel
90-
// implementation("androidx.hilt:hilt-compiler:1.2.0")
91-
// implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
92-
//
93-
//
94-
// // Navigation
95-
// val nav_version = "2.7.7"
96-
// implementation("androidx.navigation:navigation-compose:$nav_version")
97-
98-
99-
10072
val room_version = "2.6.1"
10173

10274
implementation("androidx.room:room-runtime:$room_version")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.contactbook.navigation
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.navigation.NavHostController
5+
import androidx.navigation.compose.NavHost
6+
import androidx.navigation.compose.composable
7+
import com.example.contactbook.ui_layer.ContactState
8+
import com.example.contactbook.ui_layer.ContactViewModel
9+
import com.example.contactbook.ui_layer.Home
10+
import com.example.contactbook.ui_layer.Splash
11+
12+
13+
@Composable
14+
fun NavGraph(
15+
navHostController: NavHostController,
16+
viewModel: ContactViewModel,
17+
state: ContactState,
18+
onEvent: () -> Unit
19+
) {
20+
21+
NavHost(navController = navHostController, startDestination = Routes.SPLASH.routes) {
22+
23+
composable (Routes.SPLASH.routes){
24+
Splash(navHostController = navHostController)
25+
}
26+
27+
composable(Routes.HOME.routes){
28+
Home(
29+
state = state,
30+
viewModel = viewModel
31+
) {
32+
onEvent()
33+
}
34+
}
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.contactbook.navigation
2+
3+
sealed class Routes (var routes: String){
4+
5+
object HOME : Routes("HOME")
6+
7+
object SPLASH : Routes("SPLASH")
8+
}

app/src/main/java/com/example/contactbook/ui_layer/Home.kt

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import android.content.Intent
44
import android.graphics.Bitmap
55
import android.graphics.BitmapFactory
66
import android.net.Uri
7-
import android.util.Log
87
import android.widget.Toast
8+
import androidx.activity.compose.ManagedActivityResultLauncher
99
import androidx.activity.compose.rememberLauncherForActivityResult
10-
import androidx.activity.result.contract.ActivityResultContract
1110
import androidx.activity.result.contract.ActivityResultContracts
1211
import androidx.compose.foundation.Image
1312
import androidx.compose.foundation.background
@@ -27,6 +26,7 @@ import androidx.compose.foundation.lazy.LazyColumn
2726
import androidx.compose.foundation.lazy.items
2827
import androidx.compose.foundation.shape.CircleShape
2928
import androidx.compose.foundation.shape.RoundedCornerShape
29+
import androidx.compose.foundation.text.KeyboardOptions
3030
import androidx.compose.material.icons.Icons
3131
import androidx.compose.material.icons.filled.Add
3232
import androidx.compose.material3.AlertDialog
@@ -62,15 +62,16 @@ import androidx.compose.ui.platform.LocalContext
6262
import androidx.compose.ui.res.painterResource
6363
import androidx.compose.ui.text.TextStyle
6464
import androidx.compose.ui.text.font.FontWeight
65+
import androidx.compose.ui.text.input.KeyboardType
6566
import androidx.compose.ui.text.style.TextAlign
66-
import androidx.compose.ui.tooling.preview.Preview
6767
import androidx.compose.ui.unit.dp
6868
import androidx.compose.ui.unit.sp
6969
import com.example.contactbook.R
70-
import com.example.contactbook.components.ProfileImage
70+
import com.example.contactbook.utils.ProfileImage
7171
import com.example.contactbook.data.database.Contact
7272
import com.example.contactbook.ui.theme.GrayColor
7373
import com.example.contactbook.ui.theme.PrimaryColor
74+
import com.example.contactbook.utils.compressImage
7475
import java.io.InputStream
7576

7677

@@ -93,6 +94,15 @@ fun Home(
9394

9495
if (byte != null) {
9596
state.image.value = byte
97+
98+
val compressedImage = compressImage(byte)
99+
if (compressedImage.size > 1024 * 1024) { // 1MB
100+
Toast.makeText(context, "Image size is too large. Please choose a smaller image.", Toast.LENGTH_SHORT).show()
101+
} else {
102+
state.image.value = compressedImage
103+
104+
Toast.makeText(context, "Image added", Toast.LENGTH_SHORT).show()
105+
}
96106
}
97107
}
98108
}
@@ -198,6 +208,7 @@ fun Home(
198208
state = state,
199209
data = it,
200210
viewModel = viewModel,
211+
launcher = launcher,
201212
onEvent = onEvent
202213
)
203214

@@ -241,17 +252,15 @@ fun Home(
241252
contentDescription = "Contact Image",
242253
modifier = Modifier
243254
.size(50.dp)
244-
.border(
245-
width = 2.dp,
246-
color = PrimaryColor,
247-
shape = CircleShape
248-
)
249255
.clip(
250256
shape = CircleShape
251-
),
257+
).clickable {
258+
launcher.launch("image/*")
259+
},
252260
contentScale = ContentScale.FillBounds
253261
)
254-
} else {
262+
}
263+
else {
255264

256265
Image(
257266
painter = painterResource(id = R.drawable.add_image_icon),
@@ -301,6 +310,7 @@ fun Home(
301310
onValueChange = {
302311
state.number.value = it
303312
},
313+
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
304314
leadingIcon = {
305315
Icon(
306316
painter = painterResource(id = R.drawable.phone_icon),
@@ -334,6 +344,7 @@ fun Home(
334344
contentDescription = " Gmail Icon"
335345
)
336346
},
347+
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
337348
placeholder = {
338349
Text(text = "Enter gmail")
339350
},
@@ -381,6 +392,7 @@ fun ShowContactCard(
381392
state: ContactState,
382393
data: Contact,
383394
viewModel: ContactViewModel,
395+
launcher : ManagedActivityResultLauncher<String,Uri?>,
384396
onEvent: () -> Unit
385397

386398
) {
@@ -513,6 +525,7 @@ fun ShowContactCard(
513525
state.name.value = data.name
514526
state.gmail.value = data.gmail
515527
state.number.value = data.number
528+
state.image.value = data.image!!
516529
dialogShow = true
517530
dropDownShow = false
518531

@@ -544,6 +557,7 @@ fun ShowContactCard(
544557
state.name.value = data.name
545558
state.gmail.value = data.gmail
546559
state.number.value = data.number
560+
state.image.value = data.image!!
547561
dropDownShow = false
548562
viewModel.deleteContact()
549563
Toast.makeText(
@@ -591,6 +605,11 @@ fun ShowContactCard(
591605
}
592606
}
593607

608+
var bitmapImage02: Bitmap? = null
609+
610+
if (state.image != null)
611+
bitmapImage02 = BitmapFactory.decodeByteArray(state.image.value, 0, state.image.value.size)
612+
594613

595614
if (dialogShow) {
596615

@@ -616,16 +635,27 @@ fun ShowContactCard(
616635
horizontalAlignment = Alignment.CenterHorizontally
617636
) {
618637

619-
Image(
620-
painter = painterResource(id = R.drawable.add_image_icon),
621-
contentDescription = " Add Image Icon",
622-
modifier = Modifier
623-
.size(50.dp)
624-
.clip(
625-
shape = CircleShape
626-
)
638+
if (bitmapImage02 != null) {
639+
Image(
640+
bitmap = bitmapImage02.asImageBitmap(),
641+
contentDescription = "Contact Image",
642+
modifier = Modifier
643+
.size(50.dp)
644+
.clip(
645+
shape = CircleShape
646+
).clickable {
647+
launcher.launch("image/*")
648+
},
649+
contentScale = ContentScale.FillBounds
650+
)
651+
}
652+
else {
627653

628-
)
654+
ProfileImage(data.name){
655+
launcher.launch("image/*")
656+
}
657+
658+
}
629659

630660
TextField(
631661
value = state.name.value,

app/src/main/java/com/example/contactbook/ui_layer/MainActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import androidx.compose.ui.Modifier
1616
import androidx.compose.ui.graphics.Color
1717
import androidx.compose.ui.tooling.preview.Preview
1818
import androidx.hilt.navigation.compose.hiltViewModel
19+
import androidx.navigation.compose.rememberNavController
20+
import com.example.contactbook.navigation.NavGraph
1921
import com.example.contactbook.ui.theme.ContactBookTheme
2022
import dagger.hilt.android.AndroidEntryPoint
2123
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -30,6 +32,7 @@ class MainActivity : ComponentActivity() {
3032

3133
val viewModel = hiltViewModel<ContactViewModel>()
3234
val state = viewModel.state.collectAsState().value
35+
val navHostController = rememberNavController()
3336

3437
ContactBookTheme {
3538
Scaffold(
@@ -39,7 +42,8 @@ class MainActivity : ComponentActivity() {
3942
.background(color = Color.White)
4043
) { innerPadding ->
4144
innerPadding
42-
Home(
45+
NavGraph (
46+
navHostController = navHostController,
4347
state = state,
4448
viewModel = viewModel
4549
) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.example.contactbook.ui_layer
2+
3+
import androidx.compose.foundation.Image
4+
import androidx.compose.foundation.background
5+
import androidx.compose.foundation.layout.Arrangement
6+
import androidx.compose.foundation.layout.Column
7+
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.layout.size
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.runtime.LaunchedEffect
11+
import androidx.compose.ui.Alignment
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.res.painterResource
14+
import androidx.compose.ui.unit.dp
15+
import androidx.navigation.NavHostController
16+
import com.example.contactbook.R
17+
import com.example.contactbook.navigation.Routes
18+
import com.example.contactbook.ui.theme.PrimaryColor
19+
import kotlinx.coroutines.Delay
20+
import kotlinx.coroutines.delay
21+
22+
23+
@Composable
24+
fun Splash(navHostController: NavHostController) {
25+
26+
27+
Column(
28+
modifier = Modifier
29+
.fillMaxSize()
30+
.background(color = PrimaryColor),
31+
horizontalAlignment = Alignment.CenterHorizontally,
32+
verticalArrangement = Arrangement.Center
33+
) {
34+
Image(
35+
painter = painterResource(R.drawable.logo),
36+
contentDescription = "Logo",
37+
modifier = Modifier.size(300.dp)
38+
)
39+
}
40+
41+
LaunchedEffect(key1 = true) {
42+
43+
delay(3000)
44+
45+
navHostController.navigate(Routes.HOME.routes)
46+
}
47+
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.contactbook.utils
2+
3+
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
5+
import java.io.ByteArrayOutputStream
6+
7+
fun compressImage( imageData: ByteArray ): ByteArray {
8+
9+
val bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.size)
10+
val outputStream = ByteArrayOutputStream()
11+
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream) // Adjust quality as needed
12+
return outputStream.toByteArray()
13+
}

app/src/main/java/com/example/contactbook/components/ProfileImage.kt renamed to app/src/main/java/com/example/contactbook/utils/ProfileImage.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package com.example.contactbook.components
1+
package com.example.contactbook.utils
22

33
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.clickable
45
import androidx.compose.foundation.layout.Box
56
import androidx.compose.foundation.layout.size
67
import androidx.compose.foundation.shape.CircleShape
@@ -17,7 +18,7 @@ import kotlin.random.Random
1718

1819

1920
@Composable
20-
fun ProfileImage(name: String) {
21+
fun ProfileImage(name: String, onClick: () -> Unit = {Unit}) {
2122

2223
val firstLetter = name.first().toString()
2324
val backgroundColor = Color(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
@@ -26,6 +27,9 @@ fun ProfileImage(name: String) {
2627
Box(
2728
contentAlignment = Alignment.Center,
2829
modifier = Modifier
30+
.clickable {
31+
onClick()
32+
}
2933
.size(50.dp)
3034
.background(color = backgroundColor, shape = CircleShape)
3135
) {

0 commit comments

Comments
 (0)