initConfig

This module provides the initialization setup for the Smart1 SDK in Android applications.

Overview

The initConfig module contains two main components:

  1. Smart1SDK - Main entry point for SDK initialization

  2. InitSDKConfig - Configuration manager for user-specific SDK setup

Getting Started

Step 1: Initialize the SDK in Your Application Class

Before using any SDK features, you must initialize the SDK by calling Smart1SDK.initialize() in your Application class:

Basic Initialization (No Koin in your app):

import android.app.Application
import com.servinformacion.smart1sdk.android.init_config.Smart1SDK

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()

// Initialize Smart1 SDK
Smart1SDK.initialize(this)
}
}

Advanced Initialization (With your own Koin modules):

If you're already using Koin in your app, you can provide your own modules to the SDK:

import android.app.Application
import com.servinformacion.smart1sdk.android.init_config.Smart1SDK
import org.koin.core.logger.Level
import org.koin.dsl.module

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()

// Initialize SDK with your app modules
Smart1SDK.initialize(
context = this,
addKoinAndroidLogger = true,
koinAndroidLoggerLevel = Level.DEBUG,
extraKoinModules = listOf(
myAppModule,
viewModelModule
)
)
}
}

⚠️ Important: Do NOT call startKoin { } anywhere in your app. The SDK handles Koin initialization internally when you call Smart1SDK.initialize(). If you try to call startKoin() yourself, Koin will throw a KoinAppAlreadyStartedException error.

Step 2: Declare Your Application Class in AndroidManifest.xml

Make sure to register your Application class in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.yourapp">

<application
android:name=".MyApplication"
android:label="@string/app_name"
...>
<!-- Your activities and other components -->
</application>
</manifest>

Step 3: Configure SDK for a Specific User

After Koin initialization, configure the SDK for a specific user operator:

import com.servinformacion.smart1sdk.android.init_config.InitSDKConfig
import com.servinformacion.smart1sdk.android.core.ResultS1SDK

class MainActivity : AppCompatActivity() {

private val initSDKConfig = InitSDKConfig()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Configure SDK for a specific user
val result = initSDKConfig.init(
sdkApiKey = "your-sdk-api-key",
email = "user@example.com",
phoneNumber = null // Optional
)

when (result) {
is ResultS1SDK.Success -> {
// SDK is configured and ready to use
Log.d("SDK", "Configuration successful")
}
is ResultS1SDK.Error -> {
// Handle configuration error
Log.e("SDK", "Configuration failed: ${result.error}")
}
}
}
}

API Reference

Smart1SDK

Main entry point for SDK initialization.

Methods

  • initialize(context: Context, addKoinAndroidLogger: Boolean = false, koinAndroidLoggerLevel: Level = Level.INFO, extraKoinModules: List<Module> = emptyList())

    • Initializes the Smart1 SDK with Koin dependency injection

    • Parameters:

      • context: The Android application context (pass this from your Application class)

      • addKoinAndroidLogger: Whether to enable Koin's Android logger (default: false)

      • koinAndroidLoggerLevel: The logging level for Koin (default: Level.INFO)

      • extraKoinModules: Additional Koin modules from your app (default: empty list)

    • Example:

      Smart1SDK.initialize(
      context = this,
      addKoinAndroidLogger = true,
      koinAndroidLoggerLevel = Level.DEBUG,
      extraKoinModules = listOf(myAppModule)
      )

InitSDKConfig

Manages user-specific SDK configuration.

Methods

  • init(sdkApiKey: String, email: String?, phoneNumber: String?): ResultS1SDK<Boolean, ErrorS1SDK>

    • Configures the SDK for a specific user operator

    • Parameters:

      • sdkApiKey: Your SDK API key (required)

      • email: User's email address (optional, but either email or phoneNumber is required)

      • phoneNumber: User's phone number (optional, but either email or phoneNumber is required)

    • Returns: ResultS1SDK<Boolean, ErrorS1SDK>

      • Success: true if configuration was successful

      • Error: Contains error information if configuration failed

  • isInitialized(): Boolean

    • Checks if the SDK configuration has been initialized with the required data

    • Returns: true if the SDK has a valid API key and at least one identifier (email or phone number), false otherwise

    • Useful for checking if the SDK is ready to use before making API calls or starting SDK features

Important Notes

  1. Initialization Order

    • Always call Smart1SDK.initialize() in your Application class's onCreate() first

    • Then call InitSDKConfig.init() when you need to configure for a specific user

  2. User Switching

    • If you need to switch users, call InitSDKConfig.init() again with the new user's credentials

    • This will clear previous session data and configure for the new user

  3. Koin Integration

    • The SDK initializes Koin automatically when you call Smart1SDK.initialize()

    • DO NOT call startKoin { } anywhere in your app - this will cause a KoinAppAlreadyStartedException

    • If you need to add your own Koin modules, pass them via the extraKoinModules parameter

    • There can only be ONE Koin initialization per app, and the SDK handles it for you

  4. Error Handling

    • Always handle the ResultS1SDK return value from InitSDKConfig.init()

    • Common errors include:

      • Empty SDK API key

      • Missing both email and phone number

      • Invalid input data

Example: Complete Setup

// 1. Application class
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()

// Initialize SDK (this also initializes Koin)
Smart1SDK.initialize(
context = this,
addKoinAndroidLogger = true,
extraKoinModules = listOf(
// Your app's Koin modules here
viewModelModule,
repositoryModule
)
)
}
}

// 2. Activity or ViewModel
class LoginViewModel : ViewModel() {

private val initSDKConfig = InitSDKConfig()

fun configureSDK(apiKey: String, email: String) {
viewModelScope.launch {
val result = initSDKConfig.init(
sdkApiKey = apiKey,
email = email
)

when (result) {
is ResultS1SDK.Success -> {
// Proceed with SDK features
_sdkConfigured.value = true
}
is ResultS1SDK.Error -> {
// Show error to user
_error.value = result.error
}
}
}
}
}

Example: Checking Initialization Status

class MainViewModel : ViewModel() {

private val initSDKConfig = InitSDKConfig()

fun startSDKFeatures() {
// Check if SDK is already configured before using features
if (!initSDKConfig.isInitialized()) {
// SDK not configured, redirect to login/configuration screen
_navigationEvent.value = NavigateTo.Login
return
}

// SDK is ready, proceed with features
startLocationTracking()
loadOrders()
}

fun onAppStart() {
// Useful for determining initial navigation
if (initSDKConfig.isInitialized()) {
// User already configured, go to main screen
_navigationEvent.value = NavigateTo.Home
} else {
// Need configuration, go to login
_navigationEvent.value = NavigateTo.Login
}
}
}

Packages