Write me a ai logo creator android app in : kotlin, jetpack compose,clean architecture and MVI. The app will use an api that actually will create and send the logo to my app, so the app actually will be a wrapper of the api.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Let's create an AI Logo Creator Android app using Kotlin, Jetpack Compose, Clean Architecture, and the Model-View-Intent (MVI) pattern. The app will interact with an API that generates logos and returns them to the app. We'll structure the app to be maintainable, testable, and scalable.
Here's what we'll cover:
Project Setup
Adding Dependencies
Setting up Clean Architecture Layers
Implementing the MVI Pattern
API Integration
Creating the UI with Jetpack Compose
Putting It All Together
Let's dive in!
1. Project Setup
Create a new Android project in Android Studio:
Language: Kotlin
Minimum SDK: Choose an appropriate API level (e.g., API 21)
Compose: Enable Jetpack Compose
2. Adding Dependencies
In your build.gradle (Module: app) file, add the necessary dependencies:
groovy
plugins {
id 'com.android.application'
id 'kotlin-android'
// Kapt for annotation processing if needed
id 'kotlin-kapt'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.ailogocreator"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.5.0'
}
}
dependencies {
// Jetpack Compose
implementation "androidx.compose.ui:ui:1.5.0"
implementation "androidx.compose.material:material:1.5.0"
implementation "androidx.compose.ui:ui-tooling-preview:1.5.0"
debugImplementation "androidx.compose.ui:ui-tooling:1.5.0"
// Lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
// Retrofit for API calls
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
// OkHttp for networking
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.10"
implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.10"
// Dependency Injection (Optional, e.g., Dagger/Hilt)
implementation "com.google.dagger:hilt-android:2.44"
kapt "com.google.dagger:hilt-android-compiler:2.44"
// Other dependencies as needed
}
Don't forget to sync the project after adding dependencies.
3. Setting up Clean Architecture Layers
Clean Architecture separates the code into layers:
Domain Layer: Contains business logic (Use Cases and Models)
Data Layer: Handles data sources (API, Database)
Presentation Layer: UI and ViewModels
3.1. Project Structure
Organize your project into packages:
code
- com.example.ailogocreator
- data
- api
- repository
- domain
- model
- repository
- usecase
- presentation
- ui
- viewmodel
- di (for Dependency Injection)
4. Implementing the MVI Pattern
In MVI, the key components are:
Intent: User actions
ViewState: UI state representation
Reducer: Applies changes to the state
Middleware: Handles side effects (e.g., API calls)
5. API Integration
Assuming the API endpoint is something like POST /generateLogo with a request body containing parameters for the logo creation.
// domain/model/LogoRequest.kt
package com.example.ailogocreator.domain.model
data class LogoRequest(
val companyName: String,
val slogan: String?,
val colors: List<String>?,
val style: String?
)
kotlin
// domain/model/LogoResponse.kt
package com.example.ailogocreator.domain.model
data class LogoResponse(
val logoUrl: String
)
// presentation/viewmodel/MainIntent.kt
package com.example.ailogocreator.presentation.viewmodel
sealed class MainIntent {
data class GenerateLogo(val companyName: String, val slogan: String?, val colors: List<String>?, val style: String?) : MainIntent()
}
ViewState
kotlin
// presentation/viewmodel/MainViewState.kt
package com.example.ailogocreator.presentation.viewmodel
import com.example.ailogocreator.domain.model.LogoResponse
sealed class MainViewState {
object Idle : MainViewState()
object Loading : MainViewState()
data class LogoGenerated(val logoResponse: LogoResponse) : MainViewState()
data class Error(val error: String) : MainViewState()
}
Gradle Sync: Sync your project to ensure all dependencies are loaded.
Notes
Replace the base URL (https://api.example.com/) with the actual API endpoint.
Handle API authentication if required (e.g., API keys, tokens).
Add error handling and input validation as needed.
Consider adding more features like allowing users to customize logos further.
Conclusion
You've now set up an Android app that uses Kotlin, Jetpack Compose, Clean Architecture, and MVI pattern to generate logos via an API. This structure ensures your app is maintainable and scalable for future enhancements.
Feel free to ask if you need further assistance or clarification on any part!
Dataset releases
Not yet included in a published release.
// presentation/ui/MainScreen.kt
package com.example.ailogocreator.presentation.ui
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberImagePainter
import coil.request.CachePolicy
import coil.request.ImageRequest
import com.example.ailogocreator.presentation.viewmodel.MainIntent
import com.example.ailogocreator.presentation.viewmodel.MainViewModel
import com.example.ailogocreator.presentation.viewmodel.MainViewState
@Composable
fun MainScreen(viewModel: MainViewModel = viewModel()) {
val state by viewModel.state.collectAsState()
var companyName by remember { mutableStateOf("") }
var slogan by remember { mutableStateOf("") }
var colors by remember { mutableStateOf("") }
var style by remember { mutableStateOf("") }
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center
) {
TextField(
value = companyName,
onValueChange = { companyName = it },
label = { Text("Company Name") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(8.dp))
TextField(
value = slogan,
onValueChange = { slogan = it },
label = { Text("Slogan (Optional)") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(8.dp))
TextField(
value = colors,
onValueChange = { colors = it },
label = { Text("Colors (comma-separated, Optional)") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(8.dp))
TextField(
value = style,
onValueChange = { style = it },
label = { Text("Style (Optional)") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = {
viewModel.intentChannel.offer(
MainIntent.GenerateLogo(
companyName = companyName,
slogan = if (slogan.isBlank()) null else slogan,
colors = if (colors.isBlank()) null else colors.split(",").map { it.trim() },
style = if (style.isBlank()) null else style
)
)
},
modifier = Modifier.align(Alignment.End)
) {
Text("Generate Logo")
}
Spacer(modifier = Modifier.height(16.dp))
when (state) {
is MainViewState.Idle -> {
// No action
}
is MainViewState.Loading -> {
CircularProgressIndicator(modifier = Modifier.align(Alignment.CenterHorizontally))
}
is MainViewState.LogoGenerated -> {
val logoUrl = (state as MainViewState.LogoGenerated).logoResponse.logoUrl
val context = LocalContext.current
val painter = rememberImagePainter(
ImageRequest.Builder(context)
.data(logoUrl)
.diskCachePolicy(CachePolicy.ENABLED)
.build()
)
Image(
painter = painter,
contentDescription = "Generated Logo",
modifier = Modifier
.fillMaxWidth()
.height(200.dp),
contentScale = ContentScale.Fit
)
}
is MainViewState.Error -> {
Text(
text = "Error: ${(state as MainViewState.Error).error}",
color = MaterialTheme.colors.error,
modifier = Modifier.align(Alignment.CenterHorizontally)
)
}
}
}
}