Skip to content

Commit

Permalink
add german version
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Apr 2, 2024
1 parent 842673f commit 859c89c
Show file tree
Hide file tree
Showing 14 changed files with 548 additions and 14 deletions.
22 changes: 21 additions & 1 deletion composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Expand All @@ -19,7 +20,26 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="jan-tennert.github.io"
tools:ignore="AppLinkUrlError" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application>

</manifest>
14 changes: 14 additions & 0 deletions composeApp/src/androidMain/resources/xml/file_provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />

<cache-path name="apks" path ="/"/>
</paths>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class LoginScreenModel(
data object Loading : State
data class Error(val message: String) : State
data object SignUpSuccess : State
data class PasswordResetSuccess(val email: String) : State
}

fun login(email: String, password: String) {
Expand All @@ -41,6 +42,28 @@ class LoginScreenModel(
}
}

fun sendPasswordResetEmail(email: String) {
screenModelScope.launch {
mutableState.value = State.Loading
runCatching {
authenticationApiImpl.sendPasswordResetEmail(email)
}.onSuccess {
mutableState.value = State.PasswordResetSuccess(email)
}.onFailure(::handleError)
}
}

fun signInWithOtp(email: String, otp: String) {
screenModelScope.launch {
mutableState.value = State.Loading
runCatching {
authenticationApiImpl.signInWithOtp(email, otp)
}.onSuccess {
mutableState.value = State.Idle
}.onFailure(::handleError)
}
}

fun resetState() {
mutableState.value = State.Idle
}
Expand All @@ -49,6 +72,10 @@ class LoginScreenModel(
mutableState.value = State.Loading
}

fun setPassResetSuccess(email: String) {
mutableState.value = State.PasswordResetSuccess(email)
}

private fun handleError(error: Throwable) {
when(error) {
is RestException -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jan.einkaufszettel.auth.data.remote

import io.github.jan.supabase.gotrue.Auth
import io.github.jan.supabase.gotrue.OtpType
import io.github.jan.supabase.gotrue.providers.builtin.Email
import io.github.jan.supabase.gotrue.user.UserInfo

Expand All @@ -18,6 +19,10 @@ interface AuthenticationApi {

suspend fun logout()

suspend fun sendPasswordResetEmail(email: String)

suspend fun signInWithOtp(email: String, otp: String)

}

internal class AuthenticationApiImpl(
Expand All @@ -42,4 +47,12 @@ internal class AuthenticationApiImpl(
auth.signOut()
}

override suspend fun sendPasswordResetEmail(email: String) {
auth.resetPasswordForEmail(email)
}

override suspend fun signInWithOtp(email: String, otp: String) {
auth.verifyEmailOtp(OtpType.Email.EMAIL, email, otp)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.jan.einkaufszettel.auth.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
Expand Down Expand Up @@ -59,6 +60,7 @@ object LoginScreen : Screen {
var email by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
var signUp by rememberSaveable { mutableStateOf(false) }
var showPasswordResetDialog by rememberSaveable { mutableStateOf(false) }

AuthForm {
val state = LocalAuthState.current
Expand All @@ -79,8 +81,25 @@ object LoginScreen : Screen {
onValueChange = { password = it },
label = { Text(Res.string.password) }
)
Row {
TextButton(
onClick = { showPasswordResetDialog = true }
) {
Text(Res.string.forgot_password)
}
TextButton(
onClick = { screenModel.setPassResetSuccess(email) }
) {
Text(Res.string.enter_code)
}
}
Button(
onClick = { if(signUp) screenModel.signUp(email, password) else screenModel.login(email, password) },
onClick = {
if (signUp) screenModel.signUp(
email,
password
) else screenModel.login(email, password)
},
enabled = state.validForm
) {
Text(if (signUp) Res.string.sign_up else Res.string.sign_in)
Expand Down Expand Up @@ -112,7 +131,7 @@ object LoginScreen : Screen {
}


when(screenState) {
when (screenState) {
is LoginScreenModel.State.Error -> {
ErrorDialog(
error = (screenState as LoginScreenModel.State.Error).message,
Expand All @@ -127,11 +146,30 @@ object LoginScreen : Screen {
screenModel.resetState()
}
}
is LoginScreenModel.State.PasswordResetSuccess -> {
val email = (screenState as LoginScreenModel.State.PasswordResetSuccess).email
var code by rememberSaveable { mutableStateOf("") }
PasswordResetSuccess(
code = code,
email = email,
onCodeChange = { code = it },
signIn = {
screenModel.signInWithOtp(email, code)
},
dismiss = {
screenModel.resetState()
}
)
}
else -> {}
}

if(nativeSignInResult != null && nativeSignInResult !in listOf(NativeSignInResult.Success, NativeSignInResult.ClosedByUser)) {
val message = when(nativeSignInResult) {
if (nativeSignInResult != null && nativeSignInResult !in listOf(
NativeSignInResult.Success,
NativeSignInResult.ClosedByUser
)
) {
val message = when (nativeSignInResult) {
is NativeSignInResult.Error -> Res.string.unknown_error.format((nativeSignInResult as NativeSignInResult.Error).message)
is NativeSignInResult.NetworkError -> Res.string.network_error
else -> ""
Expand All @@ -141,6 +179,97 @@ object LoginScreen : Screen {
onDismiss = { nativeSignInResult = null }
)
}
if (showPasswordResetDialog) {
PasswordResetDialog(
onDismiss = { showPasswordResetDialog = false },
onSend = {
screenModel.sendPasswordResetEmail(it)
showPasswordResetDialog = false
}
)
}
}

@OptIn(SupabaseExperimental::class, ExperimentalMaterial3Api::class)
@Composable
private fun PasswordResetSuccess(
code: String,
email: String,
onCodeChange: (String) -> Unit,
signIn: () -> Unit,
dismiss: () -> Unit
) {
AlertDialog(
onDismissRequest = { },
title = { Text(Res.string.forgot_password) },
text = {
Column {
Text(Res.string.password_reset_success.format(email))
Spacer(Modifier.height(8.dp))
OutlinedPasswordField(
value = code,
onValueChange = onCodeChange,
label = { Text(Res.string.code) }
)
}
},
confirmButton = {
TextButton(
onClick = signIn,
enabled = code.isNotBlank()
) {
Text("Ok")
}
},
dismissButton = {
TextButton(
onClick = dismiss
) {
Text(Res.string.cancel)
}
}
)
}

@OptIn(SupabaseExperimental::class, ExperimentalMaterial3Api::class)
@Composable
private fun PasswordResetDialog(
onDismiss: () -> Unit,
onSend: (email: String) -> Unit
) {
var email by rememberSaveable { mutableStateOf("") }
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(Res.string.forgot_password) },
text = {
Column {
Text(Res.string.forgot_password_message)
Spacer(Modifier.height(8.dp))
OutlinedEmailField(
value = email,
onValueChange = { email = it },
label = { Text(Res.string.email) }
)
}
},
confirmButton = {
TextButton(
onClick = {
onSend(email)
},
enabled = email.isNotBlank()
) {
Text(Res.string.send)
}
},
dismissButton = {
TextButton(
onClick = onDismiss
) {
Text(Res.string.cancel)
}
}
)
}

@Composable
Expand All @@ -160,7 +289,6 @@ object LoginScreen : Screen {
}



}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface ProfileApi {

suspend fun createProfileForCurrentUser(name: String): ProfileDto

suspend fun updateProfile(uid: String, name: String): ProfileDto

suspend fun updateOwnProfile(name: String): ProfileDto

}


Expand Down Expand Up @@ -61,4 +65,19 @@ internal class ProfileApiImpl(
}.decodeList()
}

override suspend fun updateOwnProfile(name: String): ProfileDto {
val uid = auth.currentUserOrNull()?.id ?: error("No user logged in")
return updateProfile(uid, name)
}

override suspend fun updateProfile(uid: String, name: String): ProfileDto {
val result = profileTable.update(ProfileDto(uid, name)) {
select()
filter {
ProfileDto::id eq uid
}
}
return result.decodeSingle()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ val supabaseModule = module {
defaultLogLevel = LogLevel.DEBUG
install(Auth) {
flowType = FlowType.PKCE
scheme = "https"
host = "jan-tennert.github.io/EinkaufszettelNext"
}
install(Storage)
install(Postgrest)
Expand Down
Loading

0 comments on commit 859c89c

Please sign in to comment.