Skip to content

Commit

Permalink
Refactor MacaoResult
Browse files Browse the repository at this point in the history
  • Loading branch information
pablichjenkov committed Jul 18, 2024
1 parent 827ae0f commit f6294e5
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package com.macaosoftware.plugin.account

import com.macaosoftware.plugin.MacaoPlugin
import com.macaosoftware.util.MacaoError
import com.macaosoftware.util.MacaoResult
import kotlin.native.ObjCName

@ObjCName(name = "AccountPlugin", exact = true)
interface AccountPlugin : MacaoPlugin {
suspend fun initialize(): Boolean
suspend fun createUserWithEmailAndPassword(signUpRequest: SignUpRequest): MacaoResult<MacaoUser>
suspend fun signInWithEmailAndPassword(signInRequest: SignInRequest): MacaoResult<MacaoUser>
suspend fun signInWithEmailLink(signInRequest: SignInRequestForEmailLink): MacaoResult<MacaoUser>
suspend fun sendSignInLinkToEmail(emailLinkData: EmailLinkData): MacaoResult<Unit>
suspend fun getCurrentUser(): MacaoResult<MacaoUser>
suspend fun createUserWithEmailAndPassword(signUpRequest: SignUpRequest): MacaoResult<MacaoUser, AccountPluginError>
suspend fun signInWithEmailAndPassword(signInRequest: SignInRequest): MacaoResult<MacaoUser, AccountPluginError>
suspend fun signInWithEmailLink(signInRequest: SignInRequestForEmailLink): MacaoResult<MacaoUser, AccountPluginError>
suspend fun sendSignInLinkToEmail(emailLinkData: EmailLinkData): MacaoResult<Unit, AccountPluginError>
suspend fun getCurrentUser(): MacaoResult<MacaoUser, AccountPluginError>

suspend fun getProviderData(): MacaoResult<ProviderData>
suspend fun updateProfile(displayName: String, photoUrl: String): MacaoResult<MacaoUser>
suspend fun updateFullProfile(displayName: String?, country: String?, photoUrl: String?, phoneNo: String?, facebookLink: String?, linkedIn: String?, github: String?): MacaoResult<UserData>
suspend fun updateEmail(newEmail: String): MacaoResult<MacaoUser>
suspend fun updatePassword(newPassword: String): MacaoResult<MacaoUser>
suspend fun sendEmailVerification(): MacaoResult<MacaoUser>
suspend fun sendPasswordReset(): MacaoResult<MacaoUser>
suspend fun deleteUser(): MacaoResult<Unit>
suspend fun fetchUserData(): MacaoResult<UserData>
suspend fun checkAndFetchUserData(): MacaoResult<UserData>
suspend fun signOut(): MacaoResult<Unit>
suspend fun getProviderData(): MacaoResult<ProviderData, AccountPluginError>
suspend fun updateProfile(displayName: String, photoUrl: String): MacaoResult<MacaoUser, AccountPluginError>
suspend fun updateFullProfile(
displayName: String?,
country: String?,
photoUrl: String?,
phoneNo: String?,
facebookLink: String?,
linkedIn: String?,
github: String?
): MacaoResult<UserData, AccountPluginError>
suspend fun updateEmail(newEmail: String): MacaoResult<MacaoUser, AccountPluginError>
suspend fun updatePassword(newPassword: String): MacaoResult<MacaoUser, AccountPluginError>
suspend fun sendEmailVerification(): MacaoResult<MacaoUser, AccountPluginError>
suspend fun sendPasswordReset(): MacaoResult<MacaoUser, AccountPluginError>
suspend fun deleteUser(): MacaoResult<Unit, AccountPluginError>
suspend fun fetchUserData(): MacaoResult<UserData, AccountPluginError>
suspend fun checkAndFetchUserData(): MacaoResult<UserData, AccountPluginError>
suspend fun signOut(): MacaoResult<Unit, AccountPluginError>
}

data class ProviderData(
Expand Down Expand Up @@ -82,10 +89,12 @@ data class SignupError(
val instanceId: Long = -1L,
val errorCode: Int = 1,
val errorDescription: String = "Signup Failed"
) : MacaoError
) : AccountPluginError

data class LoginError(
val instanceId: Long = -1L,
val errorCode: Int = 2,
val errorDescription: String = "Login Failed"
) : MacaoError
) : AccountPluginError

sealed interface AccountPluginError
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ class AccountPluginEmpty : AccountPlugin {
return true
}

override suspend fun createUserWithEmailAndPassword(signUpRequest: SignUpRequest): MacaoResult<MacaoUser> {
override suspend fun createUserWithEmailAndPassword(signUpRequest: SignUpRequest): MacaoResult<MacaoUser, AccountPluginError> {
println(" AuthPluginEmpty::createUserWithEmailAndPassword() has been called")
return MacaoResult.Success(
MacaoUser("test@gmail.com")
)
}

override suspend fun signInWithEmailAndPassword(signInRequest: SignInRequest): MacaoResult<MacaoUser> {
override suspend fun signInWithEmailAndPassword(signInRequest: SignInRequest): MacaoResult<MacaoUser, AccountPluginError> {
println(" AuthPluginEmpty::signInWithEmailAndPassword() has been called")
return MacaoResult.Success(
MacaoUser("test@gmail.com")
)
}

override suspend fun signInWithEmailLink(signInRequest: SignInRequestForEmailLink): MacaoResult<MacaoUser> {
override suspend fun signInWithEmailLink(signInRequest: SignInRequestForEmailLink): MacaoResult<MacaoUser, AccountPluginError> {
println(" AuthPluginEmpty::signInWithEmailLink() has been called")
return MacaoResult.Success(MacaoUser("empty@gmail.com"))
}

override suspend fun sendSignInLinkToEmail(emailLinkData: EmailLinkData): MacaoResult<Unit> {
override suspend fun sendSignInLinkToEmail(emailLinkData: EmailLinkData): MacaoResult<Unit, AccountPluginError> {
println(" AuthPluginEmpty::sendSignInLinkToEmail() has been called")
return MacaoResult.Success(Unit)
}

override suspend fun getCurrentUser(): MacaoResult<MacaoUser> {
override suspend fun getCurrentUser(): MacaoResult<MacaoUser, AccountPluginError> {
println(" AuthPluginEmpty::getCurrentUser() has been called")
return MacaoResult.Success(MacaoUser("empty@gmail.com"))
}

override suspend fun getProviderData(): MacaoResult<ProviderData> {
override suspend fun getProviderData(): MacaoResult<ProviderData, AccountPluginError> {
println(" AuthPluginEmpty::getProviderData() has been called")
return MacaoResult.Success(
ProviderData("", "", "", "")
Expand All @@ -52,7 +52,7 @@ class AccountPluginEmpty : AccountPlugin {
override suspend fun updateProfile(
displayName: String,
photoUrl: String
): MacaoResult<MacaoUser> {
): MacaoResult<MacaoUser, AccountPluginError> {
TODO("Not yet implemented")
}

Expand All @@ -64,43 +64,43 @@ class AccountPluginEmpty : AccountPlugin {
facebookLink: String?,
linkedIn: String?,
github: String?
): MacaoResult<UserData> {
): MacaoResult<UserData, AccountPluginError> {
TODO("Not yet implemented")
}


override suspend fun updateEmail(newEmail: String): MacaoResult<MacaoUser> {
override suspend fun updateEmail(newEmail: String): MacaoResult<MacaoUser, AccountPluginError> {
TODO("Not yet implemented")
}

override suspend fun updatePassword(newPassword: String): MacaoResult<MacaoUser> {
override suspend fun updatePassword(newPassword: String): MacaoResult<MacaoUser, AccountPluginError> {
TODO("Not yet implemented")
}

override suspend fun sendEmailVerification(): MacaoResult<MacaoUser> {
override suspend fun sendEmailVerification(): MacaoResult<MacaoUser, AccountPluginError> {
TODO("Not yet implemented")
}

override suspend fun sendPasswordReset(): MacaoResult<MacaoUser> {
override suspend fun sendPasswordReset(): MacaoResult<MacaoUser, AccountPluginError> {
TODO("Not yet implemented")
}

override suspend fun deleteUser(): MacaoResult<Unit> {
override suspend fun deleteUser(): MacaoResult<Unit, AccountPluginError> {
TODO("Not yet implemented")
}

override suspend fun fetchUserData(): MacaoResult<UserData> {
override suspend fun fetchUserData(): MacaoResult<UserData, AccountPluginError> {
TODO("Not yet implemented")
}

override suspend fun checkAndFetchUserData(): MacaoResult<UserData> {
override suspend fun checkAndFetchUserData(): MacaoResult<UserData, AccountPluginError> {
println(" AuthPluginEmpty::checkAndFetchUserData() has been called")
return MacaoResult.Success(
UserData()
)
}

override suspend fun signOut(): MacaoResult<Unit> {
override suspend fun signOut(): MacaoResult<Unit, AccountPluginError> {
println(" AuthPluginEmpty::logoutUser() has been called")
return MacaoResult.Success(Unit)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.macaosoftware.util

sealed class MacaoResult<out T> {
class Success<T>(val value: T) : MacaoResult<T>()
class Error(val error: MacaoError) : MacaoResult<Nothing>()
sealed class MacaoResult<out T, out E> {
class Success<T>(val value: T) : MacaoResult<T, Nothing>()
class Error<E>(val error: E) : MacaoResult<Nothing, E>()
}

interface MacaoError
2 changes: 1 addition & 1 deletion composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ kotlin {
implementation(compose.ui)
implementation(compose.foundation)
implementation(compose.material3)
implementation("org.jetbrains.compose.components:components-resources:1.6.1")
implementation("org.jetbrains.compose.components:components-resources:1.6.11")

// Coroutines core
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.macaosoftware.component.demo.startup

import com.macaosoftware.app.InitializationError
import com.macaosoftware.app.PluginManager
import com.macaosoftware.app.StartupTask
import com.macaosoftware.util.MacaoResult
Expand All @@ -14,7 +15,7 @@ class DatabaseMigrationStartupTask : StartupTask {
return true
}

override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit> {
override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit, InitializationError> {
// todo: Remove thios delay
delay(1000)
return MacaoResult.Success(Unit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.macaosoftware.component.demo.startup

import com.macaosoftware.app.InitializationError
import com.macaosoftware.plugin.MacaoApplicationCallback
import com.macaosoftware.component.core.Component
import com.macaosoftware.component.stack.StackComponent
Expand All @@ -15,7 +16,7 @@ class DemoRootComponentInitializer : RootComponentInitializer {
return true
}

override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Component> {
override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Component, InitializationError> {

// TODO: Remove this, only for simulating a network request
delay(1000)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.macaosoftware.component.demo.startup

import com.macaosoftware.app.InitializationError
import com.macaosoftware.app.PluginManager
import com.macaosoftware.app.StartupTask
import com.macaosoftware.util.MacaoResult
Expand All @@ -14,7 +15,7 @@ class LaunchDarklyStartupTask : StartupTask {
return true
}

override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit> {
override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit, InitializationError> {
// todo: Remove thios delay
delay(1000)
return MacaoResult.Success(Unit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.macaosoftware.component.demo.startup

import com.macaosoftware.app.InitializationError
import com.macaosoftware.app.PluginManager
import com.macaosoftware.app.StartupTask
import com.macaosoftware.util.MacaoResult
Expand All @@ -14,7 +15,7 @@ class SdkXyzStartupTask : StartupTask {
return true
}

override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit> {
override suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit, InitializationError> {
// todo: Remove thios delay
delay(1000)
return MacaoResult.Success(Unit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.macaosoftware.component.demo

import com.macaosoftware.app.InitializationError
import com.macaosoftware.plugin.MacaoApplicationCallback
import com.macaosoftware.app.PluginFactory
import com.macaosoftware.app.PluginManager
Expand All @@ -10,7 +11,7 @@ class JvmPluginManagerInitializer(
private val onExitJvmApplication: () -> Unit
) : PluginManagerInitializer {

override suspend fun initialize(): MacaoResult<PluginManager> {
override suspend fun initialize(): MacaoResult<PluginManager, InitializationError> {

val macaoAppCallbackPlugin = object : MacaoApplicationCallback {
override fun onExit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.macaosoftware.component.demo.viewmodel.topbar.Demo3PageTopBarViewMode
import com.macaosoftware.component.topbar.TopBarComponent
import com.macaosoftware.component.topbar.TopBarComponentDefaults
import com.macaosoftware.app.Created
import com.macaosoftware.app.InitializationError
import com.macaosoftware.app.InitializationErrorStage
import com.macaosoftware.app.InitializationSuccess
import com.macaosoftware.app.Initializing
import com.macaosoftware.app.MacaoApplication
Expand Down Expand Up @@ -57,7 +57,7 @@ class MainWindowComponent(
val applicationState = macaoKoinApplicationState.stage.value
) {
Created,
is InitializationError,
is InitializationErrorStage,
is Initializing -> {
// no-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fun MacaoApplication(
InitializationHandler(stage)
}

is InitializationError -> {
SplashScreen(stage.errorMsg)
is InitializationErrorStage -> {
SplashScreen(stage.error.errorMsg)
}

is InitializationSuccess -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class MacaoApplicationState(
}
when (result) {
is MacaoResult.Error -> {
stage.value = InitializationError(result.error.toString())
stage.value = InitializationErrorStage(result.error)
}

is MacaoResult.Success -> {
Expand All @@ -47,7 +47,7 @@ open class MacaoApplicationState(
}

is StartupTaskStatus.CompleteError -> {
stage.value = InitializationError(status.errorMsg)
stage.value = InitializationErrorStage(status.error)
}

is StartupTaskStatus.CompleteSuccess -> {
Expand All @@ -68,7 +68,7 @@ open class MacaoApplicationState(

when(result) {
is MacaoResult.Error -> {
stage.value = InitializationError(result.error.toString())
stage.value = InitializationErrorStage(result.error)
}
is MacaoResult.Success -> {
stage.value = InitializationSuccess(result.value)
Expand All @@ -86,5 +86,7 @@ sealed class Initializing : Stage() {
data object RootComponent : Initializing()
}

class InitializationError(val errorMsg: String) : Stage()
class InitializationErrorStage(val error: InitializationError) : Stage()
class InitializationSuccess(val rootComponent: Component) : Stage()

sealed class InitializationError(val errorMsg: String)
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface PluginManagerInitializer {
* The bridge will provide specific platfom actual implementations
* of plugin interfaces.
* */
suspend fun initialize(): MacaoResult<PluginManager>
suspend fun initialize(): MacaoResult<PluginManager, InitializationError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ interface RootComponentInitializer {
/**
* Initialize the Root Component of the Macao Application.
* */
suspend fun initialize(pluginManager: PluginManager): MacaoResult<Component>
suspend fun initialize(
pluginManager: PluginManager
): MacaoResult<Component, InitializationError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ interface StartupTask {
* Things like Database Migration and LaunchDarkly initialization
* can also take place here.
* */
suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit>
suspend fun initialize(pluginManager: PluginManager): MacaoResult<Unit, InitializationError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.macaosoftware.app

sealed class StartupTaskStatus {
class Running(val taskName: String) : StartupTaskStatus()
class CompleteError(val errorMsg: String) : StartupTaskStatus()
class CompleteError(val error: InitializationError) : StartupTaskStatus()
object CompleteSuccess : StartupTaskStatus()
}

0 comments on commit f6294e5

Please sign in to comment.