Skip to content

Commit

Permalink
Android number info app
Browse files Browse the repository at this point in the history
  • Loading branch information
tberchanov committed Nov 6, 2024
1 parent e8ebcc2 commit b06f900
Show file tree
Hide file tree
Showing 52 changed files with 1,350 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
63 changes: 63 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "com.example.androidtokmp"
compileSdk = 35

defaultConfig {
applicationId = "com.example.androidtokmp"
minSdk = 24
targetSdk = 35
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
buildConfig = true
viewBinding = true
}
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)

implementation(libs.koin.android)
implementation(libs.androidx.datastore.preferences)

implementation(libs.retrofit)
implementation(libs.converter.gson)
implementation(libs.logging.interceptor)
implementation(libs.okhttp)

implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.android)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.androidtokmp

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.androidtokmp", appContext.packageName)
}
}
30 changes: 30 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidToKMP"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".presentation.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
20 changes: 20 additions & 0 deletions app/src/main/java/com/example/androidtokmp/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.androidtokmp

import android.app.Application
import com.example.androidtokmp.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.GlobalContext.startKoin

class MainApplication : Application() {

override fun onCreate() {
super.onCreate()

startKoin {
androidLogger()
androidContext(this@MainApplication)
modules(appModule)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.androidtokmp.data

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.firstOrNull

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "numbersDataStore")
private const val NUMBER_INFO_KEY = "NUMBER_INFO_KEY"
private const val SAVED_NUMBER_KEY = "SAVED_NUMBER_KEY"

class NumbersLocalDataSource(
private val context: Context
) {

suspend fun getSavedNumber(): Int? {
return context.dataStore
.data
.firstOrNull()
?.get(intPreferencesKey(name = SAVED_NUMBER_KEY))
}

suspend fun saveNumber(number: Int) {
context.dataStore.edit { preferences ->
preferences[intPreferencesKey(name = SAVED_NUMBER_KEY)] = number
}
}

suspend fun getNumberInfo(number: Int): String? {
return context.dataStore
.data
.firstOrNull()
?.get(stringPreferencesKey(name = NUMBER_INFO_KEY + number))
}

suspend fun saveNumberInfo(number: Int, info: String) {
context.dataStore.edit { preferences ->
preferences[stringPreferencesKey(name = NUMBER_INFO_KEY + number)] = info
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.androidtokmp.data

import com.example.androidtokmp.data.remote.NumbersRemoteDataSource
import com.example.androidtokmp.domain.NumbersRepository

class NumbersRepositoryImpl(
private val localDataSource: NumbersLocalDataSource,
private val remoteDataSource: NumbersRemoteDataSource,
) : NumbersRepository {

override suspend fun getNumberInfo(number: Int): String? {
val localNumberInfo = localDataSource.getNumberInfo(number)
if (localNumberInfo != null) {
return localNumberInfo
}

return remoteDataSource.getNumberInfo(number)
}

override suspend fun saveNumberInfo(number: Int, info: String) {
localDataSource.saveNumberInfo(number, info)
}

override suspend fun getSavedNumber(): Int? {
return localDataSource.getSavedNumber()
}

override suspend fun saveNumber(number: Int) {
localDataSource.saveNumber(number)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.androidtokmp.data.remote

import com.google.gson.annotations.SerializedName

data class NumberInfoResponse(
@SerializedName("text")
val text: String,
@SerializedName("number")
val number: Int,
@SerializedName("found")
val found: Boolean,
@SerializedName("type")
val type: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.androidtokmp.data.remote

import retrofit2.http.GET
import retrofit2.http.Path

interface NumbersApi {

@GET("/{number}?json")
suspend fun getNumberInfo(@Path("number") number: Int): NumberInfoResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.androidtokmp.data.remote

class NumbersRemoteDataSource(
private val numbersApi: NumbersApi,
) {

suspend fun getNumberInfo(number: Int): String {
return numbersApi.getNumberInfo(number).text
}
}
62 changes: 62 additions & 0 deletions app/src/main/java/com/example/androidtokmp/di/modules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.androidtokmp.di

import com.example.androidtokmp.data.remote.NumbersApi
import com.example.androidtokmp.data.NumbersLocalDataSource
import com.example.androidtokmp.data.remote.NumbersRemoteDataSource
import com.example.androidtokmp.data.NumbersRepositoryImpl
import com.example.androidtokmp.domain.NumbersRepository
import com.example.androidtokmp.domain.usecase.GetNumberInfoUseCase
import com.example.androidtokmp.domain.usecase.GetSavedNumberInfoUseCase
import com.example.androidtokmp.domain.usecase.SaveNumberUseCase
import com.example.androidtokmp.presentation.NumbersInfoViewModel
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.android.BuildConfig
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

val dataModule = module {
single {
getApiClient("http://numbersapi.com", NumbersApi::class.java)
}
single { NumbersLocalDataSource(androidContext()) }
single { NumbersRemoteDataSource(get()) }
single<NumbersRepository> { NumbersRepositoryImpl(get(), get()) }
}

val domainModule = module {
factory { GetNumberInfoUseCase(get()) }
factory { GetSavedNumberInfoUseCase(get()) }
factory { SaveNumberUseCase(get()) }
}

val presentationModule = module {
viewModel { NumbersInfoViewModel(get(), get(), get()) }
}

val appModule = module {
includes(dataModule, domainModule, presentationModule)
}

private fun <T> getApiClient(baseUrl: String, api: Class<T>): T {
val httpClient = getOkHttpClient()
return Retrofit.Builder()
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.build()
.create(api)
}

private fun getOkHttpClient(): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
val httpClient = OkHttpClient.Builder()
interceptor.level = HttpLoggingInterceptor.Level.BODY
if (BuildConfig.DEBUG) {
httpClient.addInterceptor(interceptor)
}
return httpClient.build()
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/example/androidtokmp/domain/LCE.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.androidtokmp.domain

sealed class LCE<T> {
class Loading<T> : LCE<T>()
data class Content<T>(val data: T) : LCE<T>()
data class Error<T>(val message: String) : LCE<T>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.androidtokmp.domain

interface NumbersRepository {

suspend fun getNumberInfo(number: Int): String?

suspend fun saveNumberInfo(number: Int, info: String)

suspend fun getSavedNumber(): Int?

suspend fun saveNumber(number: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.androidtokmp.domain.model

data class NumberInfo(
val number: Int,
val info: String,
)
Loading

0 comments on commit b06f900

Please sign in to comment.