Skip to content

Commit

Permalink
[1.2.0] KindaViewModelTest 향상
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdohun0104 committed Oct 4, 2020
1 parent 0ef1810 commit 4856ad8
Show file tree
Hide file tree
Showing 48 changed files with 755 additions and 19 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
classpath Deps.Gradle.gradle
classpath Deps.Gradle.kotlinGradlePlugin
classpath Deps.Gradle.androidMavenGradlePlugin
classpath Deps.Gradle.hiltAndroidGradlePlugin
}
}

Expand Down
19 changes: 19 additions & 0 deletions buildSrc/src/main/java/dependencies/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object Deps {
const val gradle = "com.android.tools.build:gradle:3.6.3"
const val kotlinGradlePlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Kotlin.version}"
const val androidMavenGradlePlugin = "com.github.dcendents:android-maven-gradle-plugin:2.1"
const val hiltAndroidGradlePlugin = "com.google.dagger:hilt-android-gradle-plugin:${Hilt.version}"
}

object Kotlin {
Expand All @@ -20,6 +21,7 @@ object Deps {
}

object AndroidX {
const val activityKtx = "androidx.activity:activity-ktx:1.1.0"
const val appCompat = "androidx.appcompat:appcompat:1.1.0"
const val constraintLayout = "androidx.constraintlayout:constraintlayout:2.0.0-beta4"
const val recyclerView = "androidx.recyclerview:recyclerview:1.1.0"
Expand All @@ -28,6 +30,18 @@ object Deps {
const val viewModelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
const val liveDataKtx = "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
}

object Hilt {
const val version = "1.0.0-alpha02"
const val lifecycleViewModel = "androidx.hilt:hilt-lifecycle-viewmodel:$version"
const val compiler = "androidx.hilt:hilt-compiler:$version"
}
}

object Hilt {
const val version = "2.28.3-alpha"
const val android = "com.google.dagger:hilt-android:$version"
const val compiler = "com.google.dagger:hilt-android-compiler:$version"
}

object Glide {
Expand All @@ -39,8 +53,13 @@ object Deps {
const val gson = "com.squareup.retrofit2:converter-gson:2.8.1"
}

object OkHttp {
const val logging = "com.squareup.okhttp3:logging-interceptor:4.9.0"
}

object Test {
const val junit4 = "junit:junit:4.13"
const val coreTesting = "androidx.arch.core:core-testing:2.1.0"
const val mockitoCore = "org.mockito:mockito-core:3.5.13"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import kotlinx.coroutines.delay
class CountViewModel : KindaViewModel<CountState, CountEvent, CountSideEffect>(
initialState = CountState(),
reducer = buildReducer {
whenEvent(CountEvent.AttemptMagic) {
whenEvent<CountEvent.AttemptMagic> {
dispatch(CountSideEffect.Magic)
}

whenEvent(CountEvent.Decrease) {
whenEvent<CountEvent.Decrease> {
next(copy(count = count - 1))
}

whenEvent(CountEvent.Increase) {
whenEvent<CountEvent.Increase> {
next(copy(count = count + 1))
}

whenEvent(CountEvent.Increase1000) {
whenEvent<CountEvent.Increase1000> {
next(copy(count = count + 1000))
}
},
sideEffectHandler = buildSideEffectHandler {
whenSideEffect(CountSideEffect.Magic) {
whenSideEffect<CountSideEffect.Magic> {
delay(1000)
CountEvent.Increase1000
}
Expand Down
1 change: 1 addition & 0 deletions example/hilt-retrofit-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
66 changes: 66 additions & 0 deletions example/hilt-retrofit-test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import dependencies.Deps

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "dohun.kim.kinda.hilt_retrofit_test"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
implementation project(path: ':kinda-core')
implementation project(path: ':kinda-dsl')
implementation project(path: ':kinda-android')

implementation Deps.Glide.glide

implementation Deps.Kotlin.kotlin

implementation Deps.AndroidX.activityKtx
implementation Deps.AndroidX.appCompat
implementation Deps.AndroidX.constraintLayout
implementation Deps.AndroidX.recyclerView

implementation Deps.AndroidX.Hilt.lifecycleViewModel
kapt Deps.AndroidX.Hilt.compiler

implementation Deps.Hilt.android
kapt Deps.Hilt.compiler

implementation Deps.Retrofit.retrofit
implementation Deps.Retrofit.gson

testImplementation project(path: ':kinda-android-test')
testImplementation Deps.Test.junit4
testImplementation Deps.Test.mockitoCore
}
21 changes: 21 additions & 0 deletions example/hilt-retrofit-test/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
24 changes: 24 additions & 0 deletions example/hilt-retrofit-test/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dohun.kim.kinda.hilt_retrofit_test">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".ExampleApplication">
<activity android:name=".github.GithubActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dohun.kim.kinda.hilt_retrofit_test

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class ExampleApplication : Application()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dohun.kim.kinda.hilt_retrofit_test.data

import dohun.kim.kinda.hilt_retrofit_test.model.UserModel
import retrofit2.http.GET

interface GithubApi {

@GET("users")
suspend fun getUsers(): List<UserModel>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dohun.kim.kinda.hilt_retrofit_test.data

import dohun.kim.kinda.hilt_retrofit_test.data.exception.getCustomHttpException
import dohun.kim.kinda.hilt_retrofit_test.model.UserModel
import javax.inject.Inject

interface GithubRepository {

suspend fun getUsers(): List<UserModel>
}

class DefaultGithubRepository @Inject constructor(
private val githubApi: GithubApi
) : GithubRepository {

override suspend fun getUsers(): List<UserModel> =
try {
githubApi.getUsers()
} catch (e: Exception) {
throw e.getCustomHttpException()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dohun.kim.kinda.hilt_retrofit_test.data.exception

import retrofit2.HttpException

fun Exception.getCustomHttpException(): Exception =
when (this) {
is HttpException -> {
when (code()) {
403 -> ForbiddenException()
500 -> InternalErrorException()
else -> InternalErrorException()
}
}
else -> InternalErrorException()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dohun.kim.kinda.hilt_retrofit_test.data.exception

class ForbiddenException : RuntimeException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dohun.kim.kinda.hilt_retrofit_test.data.exception

class InternalErrorException : RuntimeException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dohun.kim.kinda.hilt_retrofit_test.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dohun.kim.kinda.hilt_retrofit_test.data.GithubApi
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(ApplicationComponent::class)
class NetworkModule {

@Provides
@Singleton
fun provideGithubApi(): GithubApi =
Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://api.github.com/")
.build()
.create(GithubApi::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dohun.kim.kinda.hilt_retrofit_test.di

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.hilt.android.scopes.ActivityRetainedScoped
import dohun.kim.kinda.hilt_retrofit_test.data.DefaultGithubRepository
import dohun.kim.kinda.hilt_retrofit_test.data.GithubRepository

@Module
@InstallIn(ActivityRetainedComponent::class)
abstract class RepositoryModule {

@Binds
@ActivityRetainedScoped
abstract fun bindGithubRepository(githubRepository: DefaultGithubRepository): GithubRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dohun.kim.kinda.hilt_retrofit_test.github

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
import dohun.kim.kinda.hilt_retrofit_test.R
import dohun.kim.kinda.kinda_android.KindaActivity
import kotlinx.android.synthetic.main.activity_github.*

@AndroidEntryPoint
class GithubActivity : KindaActivity<GithubState, GithubEvent, GithubSideEffect>() {

override val viewModel: GithubViewModel by viewModels()

private val userListAdapter = UserListAdapter()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_github)

rv_user.adapter = userListAdapter
}

override fun render(state: GithubState) {
pb_loading.visibility = if (state.isLoading) View.VISIBLE else View.INVISIBLE

userListAdapter.submitList(state.users)

state.toastEvent.getData()?.let { strRes ->
Toast.makeText(this, strRes, Toast.LENGTH_SHORT).show()
}
}
}
Loading

0 comments on commit 4856ad8

Please sign in to comment.