11package com.example.blockchainaccess
22
3+ import android.annotation.SuppressLint
34import android.os.Bundle
45import android.content.Intent
56import androidx.activity.ComponentActivity
@@ -32,9 +33,7 @@ import androidx.compose.ui.tooling.preview.Preview
3233import androidx.compose.ui.layout.ContentScale
3334import androidx.compose.ui.res.painterResource
3435import com.example.blockchainaccess.ui.theme.BlockchainAccessTheme
35- import com.example.blockchainaccess.ProfileService
36- import kotlinx.coroutines.CoroutineScope
37- import kotlinx.coroutines.launch
36+ import kotlinx.coroutines.*
3837import android.widget.Toast
3938
4039class CreateProfileActivity : ComponentActivity () {
@@ -54,6 +53,7 @@ class CreateProfileActivity : ComponentActivity() {
5453 }
5554}
5655
56+ @SuppressLint(" UnusedMaterial3ScaffoldPaddingParameter" )
5757@Composable
5858fun CreateProfileScreen (name : String , modifier : Modifier = Modifier ) {
5959 val context = LocalContext .current
@@ -111,8 +111,8 @@ fun CreateProfileScreen(name: String, modifier: Modifier = Modifier) {
111111 onValueChange = { username = it },
112112 label = { Text (" Username" ) },
113113 modifier = Modifier
114- .fillMaxWidth()
115- .padding(vertical = 8 .dp)
114+ .fillMaxWidth(0.88f )
115+ .padding(vertical = 6 .dp)
116116 )
117117
118118 TextField (
@@ -121,17 +121,17 @@ fun CreateProfileScreen(name: String, modifier: Modifier = Modifier) {
121121 label = { Text (" Password" ) },
122122 visualTransformation = PasswordVisualTransformation (),
123123 modifier = Modifier
124- .fillMaxWidth()
125- .padding(vertical = 8 .dp)
124+ .fillMaxWidth(0.88f )
125+ .padding(vertical = 6 .dp)
126126 )
127127 TextField (
128128 value = repeatPassword,
129129 onValueChange = { repeatPassword = it },
130130 label = { Text (" Repeat Password" ) },
131131 visualTransformation = PasswordVisualTransformation (),
132132 modifier = Modifier
133- .fillMaxWidth()
134- .padding(vertical = 8 .dp)
133+ .fillMaxWidth(0.88f )
134+ .padding(vertical = 6 .dp)
135135 )
136136 Spacer (modifier = Modifier .height(34 .dp))
137137 Button (
@@ -165,22 +165,42 @@ fun register(
165165 repeatPassword : String ,
166166 coroutineScope : CoroutineScope
167167) {
168- val result = ProfileService .createProfile(username, password, repeatPassword)
168+ if (username.isBlank() || password.isBlank() || repeatPassword.isBlank()) {
169+ Toast .makeText(context, " All fields must be filled." , Toast .LENGTH_SHORT ).show()
170+ return
171+ }
169172
170- result.fold(
171- onSuccess = { message ->
172- val id = message.substringAfter( " ID: " ).trim()
173- Toast .makeText(context, " Success: $message " , Toast . LENGTH_SHORT ).show()
173+ if (password != repeatPassword) {
174+ Toast .makeText(context, " Passwords do not match. " , Toast . LENGTH_SHORT ).show()
175+ return
176+ }
174177
175- coroutineScope.launch {
176- SessionManager .saveSession(context, id, username)
177- // Navigate to UserActivity after session is saved
178- val intent = Intent (context, UserActivity ::class .java)
179- context.startActivity(intent)
180- }
181- },
182- onFailure = { error ->
183- Toast .makeText(context, error.message ? : " Unknown error" , Toast .LENGTH_SHORT ).show()
178+ // Launch a coroutine to handle the asynchronous network request
179+ coroutineScope.launch(Dispatchers .IO ) {
180+ // The network request is made here, on a background thread.
181+ val result = NetworkClient .createAndRegisterProfile(username, password)
182+
183+ // Switch back to the main thread to update UI or navigate
184+ withContext(Dispatchers .Main ) {
185+ result.fold(
186+ onSuccess = { message ->
187+ Toast .makeText(context, message, Toast .LENGTH_SHORT ).show()
188+
189+ // Here's where the network call has already completed successfully.
190+ // Now, we can safely save the session and navigate.
191+ // Note: The message from the network client contains the ID.
192+ val id = message.substringAfter(" ID: " )
193+
194+ SessionManager .saveSession(context, id, username)
195+
196+ val intent = Intent (context, UserActivity ::class .java)
197+ context.startActivity(intent)
198+ },
199+ onFailure = { error ->
200+ // This block runs if the network call failed for any reason.
201+ Toast .makeText(context, error.message ? : " Unknown error" , Toast .LENGTH_SHORT ).show()
202+ }
203+ )
184204 }
185- )
205+ }
186206}
0 commit comments