package
com.example.newcanaryproject
import
android.app.Activity
import
android.content.Context
import
android.content.Intent
import
android.content.SharedPreferences
import
android.os.Bundle
import
android.util.Log
import
androidx.activity.ComponentActivity
import
androidx.activity.compose.setContent
import
androidx.compose.foundation.layout.*
import
androidx.compose.material.*
import
androidx.compose.material.icons.Icons
import
androidx.compose.material.icons.filled.Email
import
androidx.compose.material.icons.filled.Password
import
androidx.compose.runtime.*
import
androidx.compose.ui.Alignment
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.platform.LocalContext
import
androidx.compose.ui.text.TextStyle
import
androidx.compose.ui.text.font.FontFamily
import
androidx.compose.ui.text.font.FontWeight
import
androidx.compose.ui.text.input.PasswordVisualTransformation
import
androidx.compose.ui.text.input.TextFieldValue
import
androidx.compose.ui.text.style.TextAlign
import
androidx.compose.ui.unit.*
import
com.example.newcanaryproject.ui.theme.*
class
MainActivity : ComponentActivity() {
lateinit var sharedPreferences: SharedPreferences
var PREFS_KEY =
"prefs"
var EMAIL_KEY =
"email"
var PWD_KEY =
"pwd"
var e =
""
var p =
""
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContent {
sharedPreferences = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
val email = remember {
mutableStateOf(
""
)
}
val pwd = remember {
mutableStateOf(
""
)
}
email.value = sharedPreferences.getString(EMAIL_KEY,
""
).toString()
pwd.value = sharedPreferences.getString(PWD_KEY,
""
).toString()
e = email.value
p = pwd.value
NewCanaryProjectTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Scaffold(
topBar = {
TopAppBar(backgroundColor = greenColor,
title = {
Text(
text =
"Session Management in Android"
,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
color = Color.White
)
}
)
}
) {
sessionManagement(sharedPreferences)
}
}
}
}
}
override fun onStart() {
super
.onStart()
val activity = (
this
as? Activity)
sharedPreferences = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
val email = sharedPreferences.getString(EMAIL_KEY,
""
).toString()
val pwd = sharedPreferences.getString(PWD_KEY,
""
).toString()
if
(email !=
""
&& pwd !=
""
) {
val i = Intent(
this
, MainActivity2::
class
.java)
startActivity(i)
activity?.finish()
}
}
}
@Composable
fun sessionManagement(sharedPreferences: SharedPreferences) {
val ctx = LocalContext.current
val activity = (LocalContext.current as? Activity)
val emailValue = remember {
mutableStateOf(TextFieldValue())
}
val pwdValue = remember {
mutableStateOf(TextFieldValue())
}
Column(
modifier = Modifier
.fillMaxSize()
.fillMaxHeight()
.fillMaxWidth(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text =
"Session Management in Android"
,
color = greenColor,
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Bold, textAlign = TextAlign.Center
)
TextField(
value = emailValue.value,
onValueChange = { emailValue.value = it },
placeholder = { Text(text =
"Enter your email"
) },
modifier = Modifier
.padding(
16
.dp)
.fillMaxWidth(),
textStyle = TextStyle(color = Color.Black, fontSize =
15
.sp),
singleLine =
true
,
leadingIcon = {
Icon(imageVector = Icons.Default.Email, contentDescription =
"Email"
)
}
)
TextField(
value = pwdValue.value,
onValueChange = { pwdValue.value = it },
placeholder = { Text(text =
"Enter your password"
) },
modifier = Modifier
.padding(
16
.dp)
.fillMaxWidth(),
textStyle = TextStyle(color = Color.Black, fontSize =
15
.sp),
singleLine =
true
,
visualTransformation = PasswordVisualTransformation(),
leadingIcon = {
Icon(imageVector = Icons.Default.Password, contentDescription =
"Email"
)
}
)
Button(
modifier = Modifier
.fillMaxWidth()
.padding(
16
.dp),
onClick = {
saveData(emailValue.value.text, pwdValue.value.text, sharedPreferences, ctx)
activity?.finish()
},
colors = ButtonDefaults.buttonColors(
backgroundColor = greenColor,
)
)
{
Text(text =
"Login"
)
}
}
}
fun saveData(email: String, pwd: String, sharedPreferences: SharedPreferences, context: Context) {
val editor: SharedPreferences.Editor = sharedPreferences.edit()
editor.putString(
"email"
, email)
editor.putString(
"pwd"
, pwd)
editor.apply()
val i = Intent(context, MainActivity2::
class
.java)
context.startActivity(i)
}