Initial commit
105
composeApp/build.gradle.kts
Normal file
@@ -0,0 +1,105 @@
|
||||
plugins {
|
||||
alias(libs.plugins.kotlinMultiplatform)
|
||||
alias(libs.plugins.androidApplication)
|
||||
alias(libs.plugins.composeMultiplatform)
|
||||
alias(libs.plugins.composeCompiler)
|
||||
alias(libs.plugins.kotlinSerialization)
|
||||
alias(libs.plugins.ksp)
|
||||
alias(libs.plugins.room)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
|
||||
androidTarget()
|
||||
|
||||
sourceSets {
|
||||
androidMain.dependencies {
|
||||
implementation(libs.compose.ui.tooling.preview)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.koin.android)
|
||||
implementation(libs.ktor.client.okhttp)
|
||||
}
|
||||
|
||||
commonMain.dependencies {
|
||||
implementation(libs.compose.runtime)
|
||||
implementation(libs.compose.foundation)
|
||||
implementation(compose.material3)
|
||||
implementation(compose.materialIconsExtended)
|
||||
implementation(compose.components.resources)
|
||||
implementation(libs.compose.ui)
|
||||
implementation(libs.compose.ui.tooling.preview)
|
||||
|
||||
api(libs.koin.core)
|
||||
implementation(libs.koin.compose)
|
||||
implementation(libs.koin.compose.viewmodel)
|
||||
implementation(libs.androidx.lifecycle.runtime.compose)
|
||||
implementation(libs.androidx.lifecycle.viewmodel.compose)
|
||||
implementation(libs.androidx.navigation.compose)
|
||||
|
||||
implementation(libs.room.runtime)
|
||||
implementation(libs.sqlite.bundled)
|
||||
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
|
||||
implementation(libs.coil.compose)
|
||||
implementation(libs.coil.network.ktor3)
|
||||
}
|
||||
|
||||
commonTest.dependencies {
|
||||
implementation(libs.kotlin.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "io.assessment.contacts"
|
||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||
|
||||
buildFeatures {
|
||||
buildConfig = true
|
||||
}
|
||||
|
||||
lint {
|
||||
abortOnError = false
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "io.assessment.contacts"
|
||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||
targetSdk = libs.versions.android.targetSdk.get().toInt()
|
||||
versionCode = 1
|
||||
versionName = "1.0.0"
|
||||
}
|
||||
|
||||
packaging {
|
||||
resources {
|
||||
excludes += "/META-INF/{AL2.0,LGPL2.1}"
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
getByName("debug") {
|
||||
isDebuggable = true
|
||||
}
|
||||
getByName("release") {
|
||||
isMinifyEnabled = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
add("kspAndroid", libs.room.compiler)
|
||||
debugImplementation(libs.compose.ui.tooling)
|
||||
}
|
||||
|
||||
room {
|
||||
schemaDirectory("$projectDir/schemas")
|
||||
}
|
||||
|
||||
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
|
||||
compilerOptions {
|
||||
freeCompilerArgs.add("-Xexpect-actual-classes")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
{
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 1,
|
||||
"identityHash": "c812bdba76dc08f1c83d24558a84ae29",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "contacts",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `name` TEXT NOT NULL, `npub` TEXT NOT NULL, `avatarUrl` TEXT, `status` TEXT NOT NULL, `groupId` TEXT, PRIMARY KEY(`id`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "npub",
|
||||
"columnName": "npub",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "avatarUrl",
|
||||
"columnName": "avatarUrl",
|
||||
"affinity": "TEXT"
|
||||
},
|
||||
{
|
||||
"fieldPath": "status",
|
||||
"columnName": "status",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "groupId",
|
||||
"columnName": "groupId",
|
||||
"affinity": "TEXT"
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"tableName": "groups",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `name` TEXT NOT NULL, `memberCount` INTEGER NOT NULL, PRIMARY KEY(`id`))",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
"columnName": "id",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "name",
|
||||
"columnName": "name",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "memberCount",
|
||||
"columnName": "memberCount",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
"autoGenerate": false,
|
||||
"columnNames": [
|
||||
"id"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c812bdba76dc08f1c83d24558a84ae29')"
|
||||
]
|
||||
}
|
||||
}
|
||||
25
composeApp/src/androidMain/AndroidManifest.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:name=".ContactsApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@android:style/Theme.Material.Light.NoActionBar">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:theme="@android:style/Theme.Material.Light.NoActionBar">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.assessment.contacts
|
||||
|
||||
import android.app.Application
|
||||
import io.assessment.contacts.core.data.database.DatabaseSeeder
|
||||
import io.assessment.contacts.di.databaseModule
|
||||
import io.assessment.contacts.di.sharedAppModule
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.android.ext.android.inject
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.android.ext.koin.androidLogger
|
||||
import org.koin.core.context.startKoin
|
||||
|
||||
class ContactsApplication : Application() {
|
||||
|
||||
private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val databaseSeeder: DatabaseSeeder by inject()
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
startKoin {
|
||||
androidContext(this@ContactsApplication)
|
||||
androidLogger()
|
||||
modules(databaseModule, sharedAppModule)
|
||||
}
|
||||
|
||||
applicationScope.launch {
|
||||
databaseSeeder.seedIfEmpty()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.assessment.contacts
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
setContent {
|
||||
App()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package io.assessment.contacts.contacts.presentation
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
import io.assessment.contacts.designsystem.ContactsTheme
|
||||
|
||||
private val sampleContacts = listOf(
|
||||
ContactVO(
|
||||
id = "1",
|
||||
name = "Alice Johnson",
|
||||
npub = "npub1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0",
|
||||
avatarUrl = null,
|
||||
status = ContactStatus.ACTIVE,
|
||||
),
|
||||
ContactVO(
|
||||
id = "2",
|
||||
name = "Bob Smith",
|
||||
npub = "npub1z9y8x7w6v5u4t3s2r1q0p9o8n7m6l5k4j3i2h",
|
||||
avatarUrl = null,
|
||||
status = ContactStatus.PENDING,
|
||||
),
|
||||
ContactVO(
|
||||
id = "3",
|
||||
name = "Charlie Brown",
|
||||
npub = "npub1m2n3o4p5q6r7s8t9u0v1w2x3y4z5a6b7c8d9e",
|
||||
avatarUrl = null,
|
||||
status = ContactStatus.INACTIVE,
|
||||
),
|
||||
)
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactsScreenContentPreview() {
|
||||
ContactsTheme {
|
||||
ContactsScreenContent(
|
||||
state = ContactsState(
|
||||
contacts = sampleContacts,
|
||||
isLoading = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactsScreenLoadingPreview() {
|
||||
ContactsTheme {
|
||||
ContactsScreenContent(
|
||||
state = ContactsState(isLoading = true),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactsScreenEmptyPreview() {
|
||||
ContactsTheme {
|
||||
ContactsScreenContent(
|
||||
state = ContactsState(
|
||||
contacts = emptyList(),
|
||||
isLoading = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactListItemActivePreview() {
|
||||
ContactsTheme {
|
||||
ContactListItem(
|
||||
contact = ContactVO(
|
||||
id = "1",
|
||||
name = "Alice Johnson",
|
||||
npub = "npub1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0",
|
||||
avatarUrl = null,
|
||||
status = ContactStatus.ACTIVE,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactListItemPendingPreview() {
|
||||
ContactsTheme {
|
||||
ContactListItem(
|
||||
contact = ContactVO(
|
||||
id = "2",
|
||||
name = "Bob Smith",
|
||||
npub = "npub1z9y8x7w6v5u4t3s2r1q0p9o8n7m6l5k4j3i2h",
|
||||
avatarUrl = null,
|
||||
status = ContactStatus.PENDING,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactListItemInactivePreview() {
|
||||
ContactsTheme {
|
||||
ContactListItem(
|
||||
contact = ContactVO(
|
||||
id = "3",
|
||||
name = "Charlie Brown",
|
||||
npub = "npub1m2n3o4p5q6r7s8t9u0v1w2x3y4z5a6b7c8d9e",
|
||||
avatarUrl = null,
|
||||
status = ContactStatus.INACTIVE,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun ContactAvatarWithoutImagePreview() {
|
||||
ContactsTheme {
|
||||
ContactAvatar(
|
||||
avatarUrl = null,
|
||||
name = "Alice Johnson",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun StatusBadgeActivePreview() {
|
||||
ContactsTheme {
|
||||
StatusBadge(status = ContactStatus.ACTIVE)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun StatusBadgePendingPreview() {
|
||||
ContactsTheme {
|
||||
StatusBadge(status = ContactStatus.PENDING)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun StatusBadgeInactivePreview() {
|
||||
ContactsTheme {
|
||||
StatusBadge(status = ContactStatus.INACTIVE)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
class DatabaseFactory(
|
||||
private val context: Context,
|
||||
) {
|
||||
fun create(): RoomDatabase.Builder<ContactsDatabase> {
|
||||
val appContext = context.applicationContext
|
||||
val dbFile = appContext.getDatabasePath("contacts.db")
|
||||
return Room.databaseBuilder(
|
||||
context = appContext,
|
||||
klass = ContactsDatabase::class.java,
|
||||
name = dbFile.absolutePath,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.assessment.contacts.di
|
||||
|
||||
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
|
||||
import io.assessment.contacts.core.data.database.ContactsDatabase
|
||||
import io.assessment.contacts.core.data.database.DatabaseFactory
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.dsl.module
|
||||
|
||||
val databaseModule = module {
|
||||
single {
|
||||
DatabaseFactory(androidContext())
|
||||
.create()
|
||||
.setDriver(BundledSQLiteDriver())
|
||||
.setQueryCoroutineContext(Dispatchers.IO)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package io.assessment.contacts.groups.presentation
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import io.assessment.contacts.designsystem.ContactsTheme
|
||||
|
||||
private val sampleGroups = listOf(
|
||||
GroupVO(
|
||||
id = "1",
|
||||
name = "Engineering Team",
|
||||
memberCount = 12,
|
||||
),
|
||||
GroupVO(
|
||||
id = "2",
|
||||
name = "Design Team",
|
||||
memberCount = 5,
|
||||
),
|
||||
GroupVO(
|
||||
id = "3",
|
||||
name = "Marketing",
|
||||
memberCount = 8,
|
||||
),
|
||||
)
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun GroupsScreenContentPreview() {
|
||||
ContactsTheme {
|
||||
GroupsScreenContent(
|
||||
state = GroupsState(
|
||||
groups = sampleGroups,
|
||||
isLoading = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun GroupsScreenLoadingPreview() {
|
||||
ContactsTheme {
|
||||
GroupsScreenContent(
|
||||
state = GroupsState(isLoading = true),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun GroupsScreenEmptyPreview() {
|
||||
ContactsTheme {
|
||||
GroupsScreenContent(
|
||||
state = GroupsState(
|
||||
groups = emptyList(),
|
||||
isLoading = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun GroupListItemPreview() {
|
||||
ContactsTheme {
|
||||
GroupListItem(
|
||||
group = GroupVO(
|
||||
id = "1",
|
||||
name = "Engineering Team",
|
||||
memberCount = 12,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
BIN
composeApp/src/androidMain/res/mipmap-hdpi/ic_launcher.webp
Normal file
|
After Width: | Height: | Size: 968 B |
|
After Width: | Height: | Size: 2.4 KiB |
BIN
composeApp/src/androidMain/res/mipmap-mdpi/ic_launcher.webp
Normal file
|
After Width: | Height: | Size: 748 B |
|
After Width: | Height: | Size: 1.5 KiB |
BIN
composeApp/src/androidMain/res/mipmap-xhdpi/ic_launcher.webp
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 3.4 KiB |
BIN
composeApp/src/androidMain/res/mipmap-xxhdpi/ic_launcher.webp
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 5.4 KiB |
BIN
composeApp/src/androidMain/res/mipmap-xxxhdpi/ic_launcher.webp
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 7.6 KiB |
4
composeApp/src/androidMain/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Contacts Assessment</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Contacts Assessment</string>
|
||||
|
||||
<!-- Navigation -->
|
||||
<string name="nav_groups">Groups</string>
|
||||
<string name="nav_contacts">Contacts</string>
|
||||
|
||||
<!-- Contacts Screen -->
|
||||
<string name="contacts_title">Contacts</string>
|
||||
<string name="contacts_avatar_description">Avatar of %1$s</string>
|
||||
|
||||
<!-- Groups Screen -->
|
||||
<string name="groups_title">Groups</string>
|
||||
<string name="groups_member_count">%1$d members</string>
|
||||
|
||||
<!-- Status Labels -->
|
||||
<string name="status_active">Active</string>
|
||||
<string name="status_pending">Pending</string>
|
||||
<string name="status_inactive">Inactive</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.assessment.contacts
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import io.assessment.contacts.designsystem.ContactsTheme
|
||||
import io.assessment.contacts.navigation.NavigationRoot
|
||||
|
||||
@Composable
|
||||
fun App() {
|
||||
ContactsTheme {
|
||||
NavigationRoot()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.assessment.contacts
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class MainViewModel : ViewModel()
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.assessment.contacts.contacts.data
|
||||
|
||||
import io.assessment.contacts.core.data.database.ContactEntity
|
||||
import io.assessment.contacts.core.domain.contact.Contact
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
|
||||
fun ContactEntity.toDomain(): Contact {
|
||||
return Contact(
|
||||
id = id,
|
||||
name = name,
|
||||
npub = npub,
|
||||
avatarUrl = avatarUrl,
|
||||
status = ContactStatus.INACTIVE,
|
||||
groupId = groupId,
|
||||
)
|
||||
}
|
||||
|
||||
fun Contact.toEntity(): ContactEntity {
|
||||
return ContactEntity(
|
||||
id = id,
|
||||
name = name,
|
||||
npub = npub,
|
||||
avatarUrl = avatarUrl,
|
||||
status = status,
|
||||
groupId = groupId,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.assessment.contacts.contacts.data
|
||||
|
||||
import io.assessment.contacts.contacts.domain.ObserveContactsUseCase
|
||||
import io.assessment.contacts.core.domain.contact.Contact
|
||||
import io.assessment.contacts.core.domain.contact.ContactError
|
||||
import io.assessment.contacts.core.domain.contact.ContactRepository
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class ObserveContactsUseCaseImpl(
|
||||
private val contactRepository: ContactRepository,
|
||||
) : ObserveContactsUseCase {
|
||||
|
||||
override fun invoke(): Flow<Result<List<Contact>, ContactError>> {
|
||||
return contactRepository.observeContacts()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.assessment.contacts.contacts.di
|
||||
|
||||
import io.assessment.contacts.contacts.domain.ObserveContactsUseCase
|
||||
import io.assessment.contacts.contacts.data.ObserveContactsUseCaseImpl
|
||||
import io.assessment.contacts.contacts.presentation.ContactsViewModel
|
||||
import org.koin.core.module.dsl.factoryOf
|
||||
import org.koin.core.module.dsl.viewModelOf
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
|
||||
val contactsModule = module {
|
||||
factoryOf(::ObserveContactsUseCaseImpl) bind ObserveContactsUseCase::class
|
||||
viewModelOf(::ContactsViewModel)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.assessment.contacts.contacts.domain
|
||||
|
||||
import io.assessment.contacts.core.domain.contact.Contact
|
||||
import io.assessment.contacts.core.domain.contact.ContactError
|
||||
import io.assessment.contacts.core.domain.usecase.NoParamsFlowUseCase
|
||||
|
||||
interface ObserveContactsUseCase : NoParamsFlowUseCase<List<Contact>, ContactError>
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.assessment.contacts.contacts.presentation
|
||||
|
||||
import io.assessment.contacts.core.domain.contact.Contact
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
|
||||
data class ContactVO(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val npub: String,
|
||||
val avatarUrl: String?,
|
||||
val status: ContactStatus,
|
||||
)
|
||||
|
||||
fun Contact.toVO(): ContactVO {
|
||||
return ContactVO(
|
||||
id = id,
|
||||
name = name,
|
||||
npub = npub,
|
||||
avatarUrl = avatarUrl,
|
||||
status = status,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package io.assessment.contacts.contacts.presentation
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
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.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Person
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import coil3.compose.AsyncImage
|
||||
import contactsassessment.composeapp.generated.resources.Res
|
||||
import contactsassessment.composeapp.generated.resources.contacts_avatar_description
|
||||
import contactsassessment.composeapp.generated.resources.contacts_title
|
||||
import contactsassessment.composeapp.generated.resources.status_active
|
||||
import contactsassessment.composeapp.generated.resources.status_inactive
|
||||
import contactsassessment.composeapp.generated.resources.status_pending
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
import io.assessment.contacts.designsystem.extendedColors
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
import org.koin.compose.viewmodel.koinViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ContactsScreen(
|
||||
viewModel: ContactsViewModel = koinViewModel(),
|
||||
) {
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
|
||||
ContactsScreenContent(state = state)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
internal fun ContactsScreenContent(
|
||||
state: ContactsState,
|
||||
) {
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(Res.string.contacts_title)) },
|
||||
)
|
||||
},
|
||||
) { paddingValues ->
|
||||
if (state.isLoading) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
items(
|
||||
items = state.contacts,
|
||||
key = { it.id },
|
||||
) { contact ->
|
||||
ContactListItem(contact = contact)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun ContactListItem(
|
||||
contact: ContactVO,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
ContactAvatar(
|
||||
avatarUrl = contact.avatarUrl,
|
||||
name = contact.name,
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
|
||||
Column(
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
Text(
|
||||
text = contact.name,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
Text(
|
||||
text = contact.npub,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
StatusBadge(status = contact.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun ContactAvatar(
|
||||
avatarUrl: String?,
|
||||
name: String,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
Box(
|
||||
modifier = modifier
|
||||
.size(48.dp)
|
||||
.clip(CircleShape)
|
||||
.background(MaterialTheme.colorScheme.primaryContainer),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
if (avatarUrl != null) {
|
||||
AsyncImage(
|
||||
model = avatarUrl,
|
||||
contentDescription = stringResource(Res.string.contacts_avatar_description, name),
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentScale = ContentScale.Crop,
|
||||
)
|
||||
} else {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Person,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun StatusBadge(
|
||||
status: ContactStatus,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val (backgroundColor, text) = when (status) {
|
||||
ContactStatus.ACTIVE -> extendedColors.statusActive to stringResource(Res.string.status_active)
|
||||
ContactStatus.PENDING -> extendedColors.statusPending to stringResource(Res.string.status_pending)
|
||||
ContactStatus.INACTIVE -> extendedColors.statusInactive to stringResource(Res.string.status_inactive)
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = modifier
|
||||
.clip(RoundedCornerShape(16.dp))
|
||||
.background(backgroundColor.copy(alpha = 0.2f))
|
||||
.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
) {
|
||||
Text(
|
||||
text = text,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = backgroundColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package io.assessment.contacts.contacts.presentation
|
||||
|
||||
data class ContactsState(
|
||||
val contacts: List<ContactVO> = emptyList(),
|
||||
val isLoading: Boolean = true,
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.assessment.contacts.contacts.presentation
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import io.assessment.contacts.contacts.domain.ObserveContactsUseCase
|
||||
import io.assessment.contacts.core.domain.result.filterSuccess
|
||||
import io.assessment.contacts.core.presentation.stateInWhileSubscribed
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class ContactsViewModel(
|
||||
observeContactsUseCase: ObserveContactsUseCase,
|
||||
) : ViewModel() {
|
||||
|
||||
val state = observeContactsUseCase()
|
||||
.filterSuccess()
|
||||
.map { contacts ->
|
||||
ContactsState(
|
||||
contacts = contacts.take(1).map { it.toVO() },
|
||||
isLoading = false,
|
||||
)
|
||||
}
|
||||
.stateInWhileSubscribed(
|
||||
scope = viewModelScope,
|
||||
initialValue = ContactsState(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.assessment.contacts.contacts.presentation.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
object ContactsNavDestinations {
|
||||
@Serializable
|
||||
object ContactsList
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface ContactDao {
|
||||
|
||||
@Query("SELECT * FROM contacts ORDER BY name ASC")
|
||||
fun observeAll(): Flow<List<ContactEntity>>
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE name LIKE '%' || :query || '%' ORDER BY name ASC")
|
||||
fun observeByNameQuery(query: String): Flow<List<ContactEntity>>
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE status = :status ORDER BY name ASC")
|
||||
fun observeByStatus(status: ContactStatus): Flow<List<ContactEntity>>
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE groupId = :groupId ORDER BY name ASC")
|
||||
fun observeByGroupId(groupId: String): Flow<List<ContactEntity>>
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE id = :id")
|
||||
suspend fun getById(id: String): ContactEntity?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertAll(contacts: List<ContactEntity>)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insert(contact: ContactEntity)
|
||||
|
||||
@Query("DELETE FROM contacts")
|
||||
suspend fun deleteAll()
|
||||
|
||||
@Query("DELETE FROM contacts WHERE id = :id")
|
||||
suspend fun deleteById(id: String)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
|
||||
@Entity(tableName = "contacts")
|
||||
data class ContactEntity(
|
||||
@PrimaryKey
|
||||
val id: String,
|
||||
val name: String,
|
||||
val npub: String,
|
||||
val avatarUrl: String?,
|
||||
val status: ContactStatus,
|
||||
val groupId: String?,
|
||||
)
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
ContactEntity::class,
|
||||
GroupEntity::class,
|
||||
],
|
||||
version = 1,
|
||||
exportSchema = true,
|
||||
)
|
||||
abstract class ContactsDatabase : RoomDatabase() {
|
||||
abstract val contactDao: ContactDao
|
||||
abstract val groupDao: GroupDao
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import io.assessment.contacts.core.domain.contact.ContactStatus
|
||||
|
||||
class DatabaseSeeder(
|
||||
private val contactDao: ContactDao,
|
||||
private val groupDao: GroupDao,
|
||||
) {
|
||||
suspend fun seedIfEmpty() {
|
||||
if (contactDao.getById("c1") == null) {
|
||||
seedContacts()
|
||||
}
|
||||
if (groupDao.getById("g1") == null) {
|
||||
seedGroups()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun seedContacts() {
|
||||
val contacts = listOf(
|
||||
ContactEntity(
|
||||
id = "c1",
|
||||
name = "Alice Johnson",
|
||||
npub = "npub1alice...7x8q",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=alice",
|
||||
status = ContactStatus.ACTIVE,
|
||||
groupId = "g1",
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c2",
|
||||
name = "Bob Smith",
|
||||
npub = "npub1bob...3k9m",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=bob",
|
||||
status = ContactStatus.PENDING,
|
||||
groupId = "g1",
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c3",
|
||||
name = "Charlie Brown",
|
||||
npub = "npub1charlie...5n2p",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=charlie",
|
||||
status = ContactStatus.ACTIVE,
|
||||
groupId = "g1",
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c4",
|
||||
name = "Diana Ross",
|
||||
npub = "npub1diana...8w4r",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=diana",
|
||||
status = ContactStatus.INACTIVE,
|
||||
groupId = "g2",
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c5",
|
||||
name = "Edward Norton",
|
||||
npub = "npub1edward...2j6t",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=edward",
|
||||
status = ContactStatus.PENDING,
|
||||
groupId = "g2",
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c6",
|
||||
name = "Fiona Apple",
|
||||
npub = "npub1fiona...9v3s",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=fiona",
|
||||
status = ContactStatus.ACTIVE,
|
||||
groupId = "g3",
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c7",
|
||||
name = "George Clooney",
|
||||
npub = "npub1george...4h8y",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=george",
|
||||
status = ContactStatus.ACTIVE,
|
||||
groupId = null,
|
||||
),
|
||||
ContactEntity(
|
||||
id = "c8",
|
||||
name = "Helen Mirren",
|
||||
npub = "npub1helen...6m1z",
|
||||
avatarUrl = "https://i.pravatar.cc/150?u=helen",
|
||||
status = ContactStatus.PENDING,
|
||||
groupId = null,
|
||||
),
|
||||
)
|
||||
contactDao.insertAll(contacts)
|
||||
}
|
||||
|
||||
private suspend fun seedGroups() {
|
||||
val groups = listOf(
|
||||
GroupEntity(
|
||||
id = "g1",
|
||||
name = "Alice's group",
|
||||
memberCount = 3,
|
||||
),
|
||||
GroupEntity(
|
||||
id = "g2",
|
||||
name = "Close friends",
|
||||
memberCount = 2,
|
||||
),
|
||||
GroupEntity(
|
||||
id = "g3",
|
||||
name = "Colleagues",
|
||||
memberCount = 1,
|
||||
),
|
||||
)
|
||||
groupDao.insertAll(groups)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface GroupDao {
|
||||
|
||||
@Query("SELECT * FROM groups ORDER BY name ASC")
|
||||
fun observeAll(): Flow<List<GroupEntity>>
|
||||
|
||||
@Query("SELECT * FROM groups WHERE id = :id")
|
||||
suspend fun getById(id: String): GroupEntity?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertAll(groups: List<GroupEntity>)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insert(group: GroupEntity)
|
||||
|
||||
@Query("DELETE FROM groups")
|
||||
suspend fun deleteAll()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.assessment.contacts.core.data.database
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "groups")
|
||||
data class GroupEntity(
|
||||
@PrimaryKey
|
||||
val id: String,
|
||||
val name: String,
|
||||
val memberCount: Int,
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.assessment.contacts.core.data.datasource
|
||||
|
||||
import io.assessment.contacts.core.data.database.ContactDao
|
||||
import io.assessment.contacts.core.data.database.ContactEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface ContactDataSource {
|
||||
fun observeContacts(): Flow<List<ContactEntity>>
|
||||
fun observeContactsByName(query: String): Flow<List<ContactEntity>>
|
||||
}
|
||||
|
||||
class RoomContactDataSource(
|
||||
private val contactDao: ContactDao,
|
||||
) : ContactDataSource {
|
||||
|
||||
override fun observeContacts(): Flow<List<ContactEntity>> {
|
||||
return contactDao.observeAll()
|
||||
}
|
||||
|
||||
override fun observeContactsByName(query: String): Flow<List<ContactEntity>> {
|
||||
return contactDao.observeByNameQuery(query)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.assessment.contacts.core.data.datasource
|
||||
|
||||
import io.assessment.contacts.core.data.database.GroupDao
|
||||
import io.assessment.contacts.core.data.database.GroupEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface GroupDataSource {
|
||||
fun observeGroups(): Flow<List<GroupEntity>>
|
||||
}
|
||||
|
||||
class RoomGroupDataSource(
|
||||
private val groupDao: GroupDao,
|
||||
) : GroupDataSource {
|
||||
|
||||
override fun observeGroups(): Flow<List<GroupEntity>> {
|
||||
return groupDao.observeAll()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.assessment.contacts.core.data.repository
|
||||
|
||||
import io.assessment.contacts.contacts.data.toDomain
|
||||
import io.assessment.contacts.core.data.datasource.ContactDataSource
|
||||
import io.assessment.contacts.core.domain.contact.Contact
|
||||
import io.assessment.contacts.core.domain.contact.ContactError
|
||||
import io.assessment.contacts.core.domain.contact.ContactRepository
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class ContactRepositoryImpl(
|
||||
private val contactDataSource: ContactDataSource,
|
||||
) : ContactRepository {
|
||||
|
||||
override fun observeContacts(): Flow<Result<List<Contact>, ContactError>> {
|
||||
return contactDataSource.observeContacts()
|
||||
.map { entities ->
|
||||
Result.Success(entities.map { it.toDomain() })
|
||||
}
|
||||
}
|
||||
|
||||
override fun observeContactsByName(query: String): Flow<Result<List<Contact>, ContactError>> {
|
||||
return contactDataSource.observeContactsByName(query)
|
||||
.map { entities ->
|
||||
Result.Success(entities.map { it.toDomain() })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package io.assessment.contacts.core.data.repository
|
||||
|
||||
import io.assessment.contacts.core.data.datasource.GroupDataSource
|
||||
import io.assessment.contacts.core.domain.group.Group
|
||||
import io.assessment.contacts.core.domain.group.GroupError
|
||||
import io.assessment.contacts.core.domain.group.GroupRepository
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import io.assessment.contacts.groups.data.toDomain
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class GroupRepositoryImpl(
|
||||
private val groupDataSource: GroupDataSource,
|
||||
) : GroupRepository {
|
||||
|
||||
override fun observeGroups(): Flow<Result<List<Group>, GroupError>> {
|
||||
return groupDataSource.observeGroups()
|
||||
.map { entities ->
|
||||
Result.Success(entities.map { it.toDomain() })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.assessment.contacts.core.domain.contact
|
||||
|
||||
data class Contact(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val npub: String,
|
||||
val avatarUrl: String?,
|
||||
val status: ContactStatus,
|
||||
val groupId: String?,
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.assessment.contacts.core.domain.contact
|
||||
|
||||
import io.assessment.contacts.core.domain.result.Error
|
||||
|
||||
sealed interface ContactError : Error {
|
||||
data object NotFound : ContactError
|
||||
data object NetworkError : ContactError
|
||||
data class Unknown(val message: String) : ContactError
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.assessment.contacts.core.domain.contact
|
||||
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface ContactRepository {
|
||||
fun observeContacts(): Flow<Result<List<Contact>, ContactError>>
|
||||
fun observeContactsByName(query: String): Flow<Result<List<Contact>, ContactError>>
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.assessment.contacts.core.domain.contact
|
||||
|
||||
enum class ContactStatus {
|
||||
ACTIVE,
|
||||
PENDING,
|
||||
INACTIVE,
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.assessment.contacts.core.domain.group
|
||||
|
||||
data class Group(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val memberCount: Int,
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.assessment.contacts.core.domain.group
|
||||
|
||||
import io.assessment.contacts.core.domain.result.Error
|
||||
|
||||
sealed interface GroupError : Error {
|
||||
data object NotFound : GroupError
|
||||
data object NetworkError : GroupError
|
||||
data class Unknown(val message: String) : GroupError
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.assessment.contacts.core.domain.group
|
||||
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface GroupRepository {
|
||||
fun observeGroups(): Flow<Result<List<Group>, GroupError>>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package io.assessment.contacts.core.domain.result
|
||||
|
||||
interface Error
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.assessment.contacts.core.domain.result
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.transform
|
||||
|
||||
fun <T, E : Error, R> Flow<Result<T, E>>.mapSuccess(transform: suspend (T) -> R): Flow<Result<R, E>> =
|
||||
transform { result ->
|
||||
when (result) {
|
||||
is Result.Success -> emit(Result.Success(transform(result.data)))
|
||||
is Result.Error -> emit(Result.Error(result.error))
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, E1 : Error, E2 : Error> Flow<Result<T, E1>>.mapError(transform: (E1) -> E2): Flow<Result<T, E2>> =
|
||||
map { result -> result.mapError(transform) }
|
||||
|
||||
fun <T, E : Error, R> Flow<Result<T, E>>.flatMapSuccess(transform: suspend (T) -> Result<R, E>): Flow<Result<R, E>> =
|
||||
transform { result ->
|
||||
when (result) {
|
||||
is Result.Success -> emit(transform(result.data))
|
||||
is Result.Error -> emit(Result.Error(result.error))
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, E : Error> Flow<Result<T, E>>.filterSuccess(): Flow<T> =
|
||||
transform { result ->
|
||||
if (result is Result.Success) {
|
||||
emit(result.data)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, E : Error> Flow<Result<T, E>>.filterError(): Flow<E> =
|
||||
transform { result ->
|
||||
if (result is Result.Error) {
|
||||
emit(result.error)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, E1 : Error, R, E2 : Error> Flow<Result<T, E1>>.biMap(
|
||||
onSuccess: suspend (T) -> Result<R, E2>,
|
||||
onError: (E1) -> E2,
|
||||
): Flow<Result<R, E2>> =
|
||||
transform { result ->
|
||||
when (result) {
|
||||
is Result.Success -> emit(onSuccess(result.data))
|
||||
is Result.Error -> emit(Result.Error(onError(result.error)))
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, E1 : Error, R, E2 : Error> Flow<Result<T, E1>>.transformResult(
|
||||
onSuccess: suspend (T) -> R,
|
||||
onError: (E1) -> E2,
|
||||
): Flow<Result<R, E2>> =
|
||||
transform { result ->
|
||||
when (result) {
|
||||
is Result.Success -> emit(Result.Success(onSuccess(result.data)))
|
||||
is Result.Error -> emit(Result.Error(onError(result.error)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package io.assessment.contacts.core.domain.result
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
|
||||
sealed interface Result<out D, out E : Error> {
|
||||
data class Success<out D>(
|
||||
val data: D,
|
||||
) : Result<D, Nothing>
|
||||
|
||||
data class Error<out E : io.assessment.contacts.core.domain.result.Error>(
|
||||
val error: E,
|
||||
) : Result<Nothing, E>
|
||||
}
|
||||
|
||||
inline fun <T, E : Error, R> Result<T, E>.map(map: (T) -> R): Result<R, E> =
|
||||
when (this) {
|
||||
is Result.Error -> Result.Error(error)
|
||||
is Result.Success -> Result.Success(map(data))
|
||||
}
|
||||
|
||||
fun <T, E : Error> Result<T, E>.asEmptyDataResult(): EmptyResult<E> = map { }
|
||||
|
||||
inline fun <T, E : Error> Result<T, E>.onSuccess(action: (T) -> Unit): Result<T, E> =
|
||||
when (this) {
|
||||
is Result.Error -> this
|
||||
is Result.Success -> {
|
||||
action(data)
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, E : Error> Result<T, E>.onError(action: (E) -> Unit): Result<T, E> =
|
||||
when (this) {
|
||||
is Result.Error -> {
|
||||
action(error)
|
||||
this
|
||||
}
|
||||
is Result.Success -> this
|
||||
}
|
||||
|
||||
fun <T, E : Error> Result<T, E>.successValue(): T? =
|
||||
when (this) {
|
||||
is Result.Success -> data
|
||||
is Result.Error -> null
|
||||
}
|
||||
|
||||
inline fun <T, E : Error, R> Result<T, E>.mapSuccess(transform: (T) -> R): Result<R, E> =
|
||||
when (this) {
|
||||
is Result.Success -> Result.Success(transform(data))
|
||||
is Result.Error -> Result.Error(error)
|
||||
}
|
||||
|
||||
inline fun <T, E1 : Error, E2 : Error> Result<T, E1>.mapError(transform: (E1) -> E2): Result<T, E2> =
|
||||
when (this) {
|
||||
is Result.Success -> Result.Success(data)
|
||||
is Result.Error -> Result.Error(transform(error))
|
||||
}
|
||||
|
||||
inline fun <T, E : Error, R> Result<T, E>.flatMapSuccess(transform: (T) -> Result<R, E>): Result<R, E> =
|
||||
when (this) {
|
||||
is Result.Success -> transform(data)
|
||||
is Result.Error -> Result.Error(error)
|
||||
}
|
||||
|
||||
inline fun <T, E : Error, R> Result<T, E>.fold(
|
||||
onSuccess: (T) -> R,
|
||||
onError: (E) -> R,
|
||||
): R =
|
||||
when (this) {
|
||||
is Result.Success -> onSuccess(data)
|
||||
is Result.Error -> onError(error)
|
||||
}
|
||||
|
||||
typealias EmptyResult<E> = Result<Unit, E>
|
||||
|
||||
fun <T, E : Error> Result<T, E>.asFlow(): Flow<Result<T, E>> = flowOf(this)
|
||||
@@ -0,0 +1,24 @@
|
||||
package io.assessment.contacts.core.domain.usecase
|
||||
|
||||
import io.assessment.contacts.core.domain.result.Error
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface UseCase<in P, out R, out E : Error> {
|
||||
suspend operator fun invoke(params: P): Result<R, E>
|
||||
}
|
||||
|
||||
interface NoParamsUseCase<out R, out E : Error> {
|
||||
suspend operator fun invoke(): Result<R, E>
|
||||
}
|
||||
|
||||
interface FlowUseCase<in P, out R, out E : Error> {
|
||||
operator fun invoke(params: P): Flow<Result<R, E>>
|
||||
}
|
||||
|
||||
interface NoParamsFlowUseCase<out R, out E : Error> {
|
||||
operator fun invoke(): Flow<Result<R, E>>
|
||||
}
|
||||
|
||||
typealias ActionUseCase<P, E> = UseCase<P, Unit, E>
|
||||
typealias NoParamsActionUseCase = NoParamsUseCase<Unit, Error>
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.assessment.contacts.core.presentation
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun <T> ObserveAsEvents(
|
||||
flow: Flow<T>,
|
||||
key1: Any? = null,
|
||||
key2: Any? = null,
|
||||
onEvent: (T) -> Unit,
|
||||
) {
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
LaunchedEffect(flow, lifecycleOwner.lifecycle, key1, key2) {
|
||||
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
withContext(Dispatchers.Main.immediate) {
|
||||
flow.collect(onEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.assessment.contacts.core.presentation
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
|
||||
fun <T> Flow<T>.stateInWhileSubscribed(
|
||||
scope: CoroutineScope,
|
||||
initialValue: T,
|
||||
): StateFlow<T> = this.stateIn(scope, SharingStarted.WhileSubscribed(5000), initialValue)
|
||||
@@ -0,0 +1,63 @@
|
||||
package io.assessment.contacts.designsystem
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
// Common Red Palette Colors
|
||||
val RedDarkest = Color(0xFF410001)
|
||||
val RedSecondaryDarkest = Color(0xFF410004)
|
||||
val RedVeryDark = Color(0xFF690003)
|
||||
val RedDark = Color(0xFF8B0006)
|
||||
val RedMediumDark = Color(0xFF930007)
|
||||
val RedMedium = Color(0xFFB50E11)
|
||||
val RedBright = Color(0xFFBA1A1A)
|
||||
val RedSecondaryDark = Color(0xFF68000C)
|
||||
val RedSecondaryMedium = Color(0xFFB51824)
|
||||
val RedSecondaryBright = Color(0xFFD93539)
|
||||
val RedTertiaryDark = Color(0xFF5A0009)
|
||||
val RedTertiaryMedium = Color(0xFF930015)
|
||||
val RedAccent = Color(0xFFBC1515)
|
||||
val RedLight = Color(0xFFFF5353)
|
||||
val RedVeryLight = Color(0xFFFFB3AE)
|
||||
val RedPastel = Color(0xFFFFB4AA)
|
||||
val RedSoft = Color(0xFFFFB4AB)
|
||||
val RedPale = Color(0xFFFFC3BC)
|
||||
val RedMuted = Color(0xFFE5BDB8)
|
||||
val RedVeryPale = Color(0xFFFFDAD5)
|
||||
val RedSecondaryPale = Color(0xFFFFDAD7)
|
||||
val RedAlmostWhite = Color(0xFFFFDAD6)
|
||||
val RedUltraPale = Color(0xFFFFEDEB)
|
||||
|
||||
// Surface Colors
|
||||
val SurfaceDarkest = Color(0xFF0F0E0E)
|
||||
val SurfaceVeryDark = Color(0xFF141313)
|
||||
val SurfaceDark = Color(0xFF1D1B1B)
|
||||
val SurfaceMediumDark = Color(0xFF211F1F)
|
||||
val SurfaceMedium = Color(0xFF2B2A29)
|
||||
val SurfaceGray = Color(0xFF323030)
|
||||
val SurfaceLightGray = Color(0xFF363434)
|
||||
val SurfaceBright = Color(0xFF3B3938)
|
||||
val SurfaceOutline = Color(0xFF444748)
|
||||
val SurfaceVariant = Color(0xFF747878)
|
||||
val SurfaceOutlineLight = Color(0xFF8E9192)
|
||||
val SurfaceLight = Color(0xFFC4C7C7)
|
||||
val SurfacePale = Color(0xFFDED9D8)
|
||||
val SurfaceVeryPale = Color(0xFFE6E1E1)
|
||||
val SurfaceLightest = Color(0xFFECE7E6)
|
||||
val SurfaceNearWhite = Color(0xFFF2EDEC)
|
||||
val SurfaceAlmostWhite = Color(0xFFF5F0EF)
|
||||
val SurfaceOffWhite = Color(0xFFF8F2F2)
|
||||
val SurfaceWhiteTinted = Color(0xFFFDF8F7)
|
||||
val SurfaceAlmostPure = Color(0xFFFFFBFF)
|
||||
|
||||
// Pure Colors
|
||||
val PureBlack = Color(0xFF000000)
|
||||
val PureWhite = Color(0xFFFFFFFF)
|
||||
|
||||
// Accent Colors
|
||||
val AccentGreen = Color(0xFF34C759)
|
||||
val AccentOrange = Color(0xFFFF9500)
|
||||
|
||||
// Status Colors
|
||||
val StatusGreen = Color(0xFF4CAF50)
|
||||
val StatusOrange = Color(0xFFFFA726)
|
||||
val StatusGray = Color(0xFF9E9E9E)
|
||||
@@ -0,0 +1,137 @@
|
||||
package io.assessment.contacts.designsystem
|
||||
|
||||
import androidx.compose.material3.ColorScheme
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.Immutable
|
||||
import androidx.compose.runtime.ReadOnlyComposable
|
||||
import androidx.compose.runtime.staticCompositionLocalOf
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val LocalExtendedColors = staticCompositionLocalOf { extendedColors }
|
||||
|
||||
val ColorScheme.extended: ExtendedColors
|
||||
@ReadOnlyComposable
|
||||
@Composable
|
||||
get() = LocalExtendedColors.current
|
||||
|
||||
@Immutable
|
||||
data class ExtendedColors(
|
||||
val primaryFixed: Color,
|
||||
val primaryFixedDim: Color,
|
||||
val onPrimaryFixed: Color,
|
||||
val onPrimaryFixedVariant: Color,
|
||||
val secondaryFixed: Color,
|
||||
val secondaryFixedDim: Color,
|
||||
val onSecondaryFixed: Color,
|
||||
val onSecondaryFixedVariant: Color,
|
||||
val tertiaryFixed: Color,
|
||||
val tertiaryFixedDim: Color,
|
||||
val onTertiaryFixed: Color,
|
||||
val onTertiaryFixedVariant: Color,
|
||||
val shadow: Color,
|
||||
val accentGreen: Color,
|
||||
val accentOrange: Color,
|
||||
val disabledContainer: Color,
|
||||
val statusActive: Color,
|
||||
val statusPending: Color,
|
||||
val statusInactive: Color,
|
||||
)
|
||||
|
||||
val LightColorScheme =
|
||||
lightColorScheme(
|
||||
primary = RedDark,
|
||||
onPrimary = PureWhite,
|
||||
primaryContainer = RedMedium,
|
||||
onPrimaryContainer = RedPale,
|
||||
secondary = RedSecondaryMedium,
|
||||
onSecondary = PureWhite,
|
||||
secondaryContainer = RedSecondaryBright,
|
||||
onSecondaryContainer = SurfaceAlmostPure,
|
||||
tertiary = RedDark,
|
||||
onTertiary = PureWhite,
|
||||
tertiaryContainer = RedMedium,
|
||||
onTertiaryContainer = RedPale,
|
||||
error = RedBright,
|
||||
onError = PureWhite,
|
||||
errorContainer = RedAlmostWhite,
|
||||
onErrorContainer = RedMediumDark,
|
||||
surfaceDim = SurfacePale,
|
||||
surface = SurfaceWhiteTinted,
|
||||
surfaceBright = SurfaceDark,
|
||||
surfaceTint = RedUltraPale,
|
||||
surfaceContainerLowest = PureWhite,
|
||||
surfaceContainerLow = SurfaceOffWhite,
|
||||
surfaceContainer = SurfaceNearWhite,
|
||||
surfaceContainerHigh = SurfaceLightest,
|
||||
surfaceContainerHighest = SurfaceVeryPale,
|
||||
onSurface = SurfaceDark,
|
||||
onSurfaceVariant = SurfaceOutline,
|
||||
outline = SurfaceVariant,
|
||||
outlineVariant = RedMuted,
|
||||
inverseSurface = SurfaceGray,
|
||||
inverseOnSurface = SurfaceAlmostWhite,
|
||||
inversePrimary = RedPastel,
|
||||
scrim = PureBlack,
|
||||
)
|
||||
|
||||
val DarkColorScheme =
|
||||
darkColorScheme(
|
||||
primary = RedPastel,
|
||||
onPrimary = RedVeryDark,
|
||||
primaryContainer = RedMedium,
|
||||
onPrimaryContainer = RedPale,
|
||||
secondary = RedVeryLight,
|
||||
onSecondary = RedSecondaryDark,
|
||||
secondaryContainer = RedLight,
|
||||
onSecondaryContainer = RedTertiaryDark,
|
||||
tertiary = RedPastel,
|
||||
onTertiary = RedVeryDark,
|
||||
tertiaryContainer = RedMedium,
|
||||
onTertiaryContainer = RedPale,
|
||||
error = RedSoft,
|
||||
onError = RedVeryDark,
|
||||
errorContainer = RedMediumDark,
|
||||
onErrorContainer = RedAlmostWhite,
|
||||
surfaceDim = SurfaceVeryDark,
|
||||
surface = SurfaceVeryDark,
|
||||
surfaceBright = SurfaceBright,
|
||||
surfaceTint = RedMediumDark,
|
||||
surfaceContainerLowest = SurfaceDarkest,
|
||||
surfaceContainerLow = SurfaceDark,
|
||||
surfaceContainer = SurfaceMediumDark,
|
||||
surfaceContainerHigh = SurfaceMedium,
|
||||
surfaceContainerHighest = RedMediumDark,
|
||||
onSurface = SurfaceVeryPale,
|
||||
onSurfaceVariant = SurfaceLight,
|
||||
outline = SurfaceOutlineLight,
|
||||
outlineVariant = SurfaceOutline,
|
||||
inverseSurface = SurfaceVeryPale,
|
||||
inverseOnSurface = SurfaceGray,
|
||||
inversePrimary = RedAccent,
|
||||
scrim = PureBlack,
|
||||
)
|
||||
|
||||
val extendedColors =
|
||||
ExtendedColors(
|
||||
primaryFixed = RedVeryPale,
|
||||
primaryFixedDim = RedPastel,
|
||||
onPrimaryFixed = RedDarkest,
|
||||
onPrimaryFixedVariant = RedMediumDark,
|
||||
secondaryFixed = RedSecondaryPale,
|
||||
secondaryFixedDim = RedVeryLight,
|
||||
onSecondaryFixed = RedSecondaryDarkest,
|
||||
onSecondaryFixedVariant = RedTertiaryMedium,
|
||||
tertiaryFixed = RedVeryPale,
|
||||
tertiaryFixedDim = RedPastel,
|
||||
onTertiaryFixed = RedDarkest,
|
||||
onTertiaryFixedVariant = RedMediumDark,
|
||||
shadow = PureBlack,
|
||||
accentGreen = AccentGreen,
|
||||
accentOrange = AccentOrange,
|
||||
disabledContainer = SurfaceVeryPale,
|
||||
statusActive = StatusGreen,
|
||||
statusPending = StatusOrange,
|
||||
statusInactive = StatusGray,
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.assessment.contacts.designsystem
|
||||
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalInspectionMode
|
||||
|
||||
@Composable
|
||||
fun ContactsTheme(
|
||||
darkTheme: Boolean = if (LocalInspectionMode.current) isSystemInDarkTheme() else false,
|
||||
content: @Composable () -> Unit,
|
||||
) {
|
||||
val colorScheme = if (darkTheme) DarkColorScheme else LightColorScheme
|
||||
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = ContactsTypography,
|
||||
content = content,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package io.assessment.contacts.designsystem
|
||||
|
||||
import androidx.compose.material3.Typography
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
val ContactsTypography = Typography(
|
||||
displayLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 57.sp,
|
||||
lineHeight = 64.sp,
|
||||
letterSpacing = (-0.25).sp,
|
||||
),
|
||||
displayMedium = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 45.sp,
|
||||
lineHeight = 52.sp,
|
||||
letterSpacing = 0.sp,
|
||||
),
|
||||
displaySmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 36.sp,
|
||||
lineHeight = 44.sp,
|
||||
letterSpacing = 0.sp,
|
||||
),
|
||||
headlineLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 32.sp,
|
||||
lineHeight = 40.sp,
|
||||
letterSpacing = 0.sp,
|
||||
),
|
||||
headlineMedium = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 28.sp,
|
||||
lineHeight = 36.sp,
|
||||
letterSpacing = 0.sp,
|
||||
),
|
||||
headlineSmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 24.sp,
|
||||
lineHeight = 32.sp,
|
||||
letterSpacing = 0.sp,
|
||||
),
|
||||
titleLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 22.sp,
|
||||
lineHeight = 28.sp,
|
||||
letterSpacing = 0.sp,
|
||||
),
|
||||
titleMedium = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 24.sp,
|
||||
letterSpacing = 0.15.sp,
|
||||
),
|
||||
titleSmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 14.sp,
|
||||
lineHeight = 20.sp,
|
||||
letterSpacing = 0.1.sp,
|
||||
),
|
||||
bodyLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 24.sp,
|
||||
letterSpacing = 0.5.sp,
|
||||
),
|
||||
bodyMedium = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 14.sp,
|
||||
lineHeight = 20.sp,
|
||||
letterSpacing = 0.25.sp,
|
||||
),
|
||||
bodySmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 12.sp,
|
||||
lineHeight = 16.sp,
|
||||
letterSpacing = 0.4.sp,
|
||||
),
|
||||
labelLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 14.sp,
|
||||
lineHeight = 20.sp,
|
||||
letterSpacing = 0.1.sp,
|
||||
),
|
||||
labelMedium = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 12.sp,
|
||||
lineHeight = 16.sp,
|
||||
letterSpacing = 0.5.sp,
|
||||
),
|
||||
labelSmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 11.sp,
|
||||
lineHeight = 16.sp,
|
||||
letterSpacing = 0.5.sp,
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.assessment.contacts.di
|
||||
|
||||
import io.assessment.contacts.core.data.database.ContactsDatabase
|
||||
import io.assessment.contacts.core.data.database.DatabaseSeeder
|
||||
import io.assessment.contacts.core.data.datasource.ContactDataSource
|
||||
import io.assessment.contacts.core.data.datasource.GroupDataSource
|
||||
import io.assessment.contacts.core.data.datasource.RoomContactDataSource
|
||||
import io.assessment.contacts.core.data.datasource.RoomGroupDataSource
|
||||
import io.assessment.contacts.core.data.repository.ContactRepositoryImpl
|
||||
import io.assessment.contacts.core.data.repository.GroupRepositoryImpl
|
||||
import io.assessment.contacts.core.domain.contact.ContactRepository
|
||||
import io.assessment.contacts.core.domain.group.GroupRepository
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
|
||||
val coreDataModule = module {
|
||||
single {
|
||||
get<ContactsDatabase>().contactDao
|
||||
}
|
||||
|
||||
single {
|
||||
get<ContactsDatabase>().groupDao
|
||||
}
|
||||
|
||||
singleOf(::DatabaseSeeder)
|
||||
|
||||
singleOf(::RoomContactDataSource) bind ContactDataSource::class
|
||||
singleOf(::RoomGroupDataSource) bind GroupDataSource::class
|
||||
|
||||
singleOf(::ContactRepositoryImpl) bind ContactRepository::class
|
||||
singleOf(::GroupRepositoryImpl) bind GroupRepository::class
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.assessment.contacts.di
|
||||
|
||||
import io.assessment.contacts.contacts.di.contactsModule
|
||||
import io.assessment.contacts.groups.di.groupsModule
|
||||
import org.koin.dsl.module
|
||||
|
||||
val sharedAppModule = module {
|
||||
includes(
|
||||
coreDataModule,
|
||||
contactsModule,
|
||||
groupsModule,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.assessment.contacts.groups.data
|
||||
|
||||
import io.assessment.contacts.core.data.database.GroupEntity
|
||||
import io.assessment.contacts.core.domain.group.Group
|
||||
|
||||
fun GroupEntity.toDomain(): Group {
|
||||
return Group(
|
||||
id = id,
|
||||
name = name,
|
||||
memberCount = memberCount,
|
||||
)
|
||||
}
|
||||
|
||||
fun Group.toEntity(): GroupEntity {
|
||||
return GroupEntity(
|
||||
id = id,
|
||||
name = name,
|
||||
memberCount = memberCount,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.assessment.contacts.groups.data
|
||||
|
||||
import io.assessment.contacts.core.domain.group.Group
|
||||
import io.assessment.contacts.core.domain.group.GroupError
|
||||
import io.assessment.contacts.core.domain.group.GroupRepository
|
||||
import io.assessment.contacts.core.domain.result.Result
|
||||
import io.assessment.contacts.groups.domain.ObserveGroupsUseCase
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class ObserveGroupsUseCaseImpl(
|
||||
private val groupRepository: GroupRepository,
|
||||
) : ObserveGroupsUseCase {
|
||||
|
||||
override fun invoke(): Flow<Result<List<Group>, GroupError>> {
|
||||
return groupRepository.observeGroups()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.assessment.contacts.groups.di
|
||||
|
||||
import io.assessment.contacts.groups.domain.ObserveGroupsUseCase
|
||||
import io.assessment.contacts.groups.data.ObserveGroupsUseCaseImpl
|
||||
import io.assessment.contacts.groups.presentation.GroupsViewModel
|
||||
import org.koin.core.module.dsl.factoryOf
|
||||
import org.koin.core.module.dsl.viewModelOf
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
|
||||
val groupsModule = module {
|
||||
factoryOf(::ObserveGroupsUseCaseImpl) bind ObserveGroupsUseCase::class
|
||||
viewModelOf(::GroupsViewModel)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.assessment.contacts.groups.domain
|
||||
|
||||
import io.assessment.contacts.core.domain.group.Group
|
||||
import io.assessment.contacts.core.domain.group.GroupError
|
||||
import io.assessment.contacts.core.domain.usecase.NoParamsFlowUseCase
|
||||
|
||||
interface ObserveGroupsUseCase : NoParamsFlowUseCase<List<Group>, GroupError>
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.assessment.contacts.groups.presentation
|
||||
|
||||
import io.assessment.contacts.core.domain.group.Group
|
||||
|
||||
data class GroupVO(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val memberCount: Int,
|
||||
)
|
||||
|
||||
fun Group.toVO(): GroupVO {
|
||||
return GroupVO(
|
||||
id = id,
|
||||
name = name,
|
||||
memberCount = memberCount,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package io.assessment.contacts.groups.presentation
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
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.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Group
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import contactsassessment.composeapp.generated.resources.Res
|
||||
import contactsassessment.composeapp.generated.resources.groups_member_count
|
||||
import contactsassessment.composeapp.generated.resources.groups_title
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
import org.koin.compose.viewmodel.koinViewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun GroupsScreen(
|
||||
viewModel: GroupsViewModel = koinViewModel(),
|
||||
) {
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
|
||||
GroupsScreenContent(state = state)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
internal fun GroupsScreenContent(
|
||||
state: GroupsState,
|
||||
) {
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(Res.string.groups_title)) },
|
||||
)
|
||||
},
|
||||
) { paddingValues ->
|
||||
if (state.isLoading) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
items(
|
||||
items = state.groups,
|
||||
key = { it.id },
|
||||
) { group ->
|
||||
GroupListItem(group = group)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun GroupListItem(
|
||||
group: GroupVO,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(48.dp)
|
||||
.clip(CircleShape)
|
||||
.background(MaterialTheme.colorScheme.secondaryContainer),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Group,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onSecondaryContainer,
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
|
||||
Column(
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
Text(
|
||||
text = group.name,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
Text(
|
||||
text = stringResource(Res.string.groups_member_count, group.memberCount),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package io.assessment.contacts.groups.presentation
|
||||
|
||||
data class GroupsState(
|
||||
val groups: List<GroupVO> = emptyList(),
|
||||
val isLoading: Boolean = true,
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.assessment.contacts.groups.presentation
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import io.assessment.contacts.core.domain.result.filterSuccess
|
||||
import io.assessment.contacts.core.presentation.stateInWhileSubscribed
|
||||
import io.assessment.contacts.groups.domain.ObserveGroupsUseCase
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
class GroupsViewModel(
|
||||
observeGroupsUseCase: ObserveGroupsUseCase,
|
||||
) : ViewModel() {
|
||||
|
||||
val state = observeGroupsUseCase()
|
||||
.filterSuccess()
|
||||
.map { groups ->
|
||||
GroupsState(
|
||||
groups = groups.map { it.toVO() },
|
||||
isLoading = false,
|
||||
)
|
||||
}
|
||||
.stateInWhileSubscribed(
|
||||
scope = viewModelScope,
|
||||
initialValue = GroupsState(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.assessment.contacts.groups.presentation.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
object GroupsNavDestinations {
|
||||
@Serializable
|
||||
object GroupsList
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.assessment.contacts.navigation
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Group
|
||||
import androidx.compose.material.icons.filled.Person
|
||||
import androidx.compose.material.icons.outlined.Group
|
||||
import androidx.compose.material.icons.outlined.Person
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import contactsassessment.composeapp.generated.resources.Res
|
||||
import contactsassessment.composeapp.generated.resources.nav_contacts
|
||||
import contactsassessment.composeapp.generated.resources.nav_groups
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jetbrains.compose.resources.StringResource
|
||||
|
||||
enum class BottomNavDestItem(
|
||||
val route: Any,
|
||||
val labelRes: StringResource,
|
||||
val selectedIcon: ImageVector,
|
||||
val unselectedIcon: ImageVector,
|
||||
) {
|
||||
Groups(
|
||||
route = BottomNavRoute.Groups,
|
||||
labelRes = Res.string.nav_groups,
|
||||
selectedIcon = Icons.Filled.Group,
|
||||
unselectedIcon = Icons.Outlined.Group,
|
||||
),
|
||||
Contacts(
|
||||
route = BottomNavRoute.Contacts,
|
||||
labelRes = Res.string.nav_contacts,
|
||||
selectedIcon = Icons.Filled.Person,
|
||||
unselectedIcon = Icons.Outlined.Person,
|
||||
),
|
||||
}
|
||||
|
||||
@Serializable
|
||||
sealed interface BottomNavRoute {
|
||||
@Serializable
|
||||
data object Groups : BottomNavRoute
|
||||
@Serializable
|
||||
data object Contacts : BottomNavRoute
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package io.assessment.contacts.navigation
|
||||
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.NavigationBar
|
||||
import androidx.compose.material3.NavigationBarItem
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import io.assessment.contacts.contacts.presentation.ContactsScreen
|
||||
import io.assessment.contacts.groups.presentation.GroupsScreen
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
|
||||
@Composable
|
||||
fun NavigationRoot() {
|
||||
val navController = rememberNavController()
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentDestination = navBackStackEntry?.destination
|
||||
|
||||
Scaffold(
|
||||
bottomBar = {
|
||||
NavigationBar {
|
||||
BottomNavDestItem.entries.forEach { item ->
|
||||
val selected = currentDestination?.hasRoute(item.route::class) == true
|
||||
|
||||
NavigationBarItem(
|
||||
selected = selected,
|
||||
onClick = {
|
||||
navController.navigate(item.route) {
|
||||
popUpTo(navController.graph.findStartDestination().id) {
|
||||
saveState = true
|
||||
}
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
},
|
||||
icon = {
|
||||
Icon(
|
||||
imageVector = if (selected) item.selectedIcon else item.unselectedIcon,
|
||||
contentDescription = stringResource(item.labelRes),
|
||||
)
|
||||
},
|
||||
label = { Text(stringResource(item.labelRes)) },
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
) { paddingValues ->
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = BottomNavRoute.Groups,
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
) {
|
||||
composable<BottomNavRoute.Groups> {
|
||||
GroupsScreen()
|
||||
}
|
||||
composable<BottomNavRoute.Contacts> {
|
||||
ContactsScreen()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||