Skip to content

Commit

Permalink
[MIN-44] feat: Retrofit 및 Datastore 구현(#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDW03 committed Feb 8, 2023
1 parent 8d98cdb commit cdefdf3
Show file tree
Hide file tree
Showing 32 changed files with 459 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.xml

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

8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ android {
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -50,8 +49,7 @@ dependencies {
// Hilt
implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$rootProject.hiltVersion"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$rootProject.hiltVersion"
androidTestImplementation "com.google.dagger:hilt-android-testing:$rootProject.hiltVersion"


implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
Expand All @@ -60,4 +58,8 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

kapt {
correctErrorTypes true
}
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ buildscript {
google()
gradlePluginPortal()
mavenCentral()
maven { url 'https://devrepo.kakao.com/nexus/content/groups/public/'}
}

ext {
hiltVersion = '2.44'
nav_version = '2.5.3'
fragment_version = "1.5.5"
okHttpVersion = '4.10.0'
hiltVersion = '2.44'
kakao_version = '2.12.1'
dataStorePreferences_version = '1.0.0'
retrofit2Version = '2.9.0'
}

dependencies {
Expand All @@ -20,6 +26,7 @@ buildscript {
}

plugins {
id 'com.google.dagger.hilt.android' version '2.44' apply false
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
Expand Down
7 changes: 7 additions & 0 deletions common-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
}

android {
Expand Down Expand Up @@ -50,6 +52,11 @@ dependencies {
api "androidx.fragment:fragment:$rootProject.fragment_version"
api "androidx.fragment:fragment-ktx:$rootProject.fragment_version"


// Hilt
implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$rootProject.hiltVersion"

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
Expand Down
1 change: 1 addition & 0 deletions common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
55 changes: 55 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
}

android {
namespace 'com.example.common'
compileSdk 33

defaultConfig {
minSdk 23
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

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 = '1.8'
}
}

dependencies {

api project(path: ':foundation')

// Preferences DataStore
implementation("androidx.datastore:datastore-preferences:$rootProject.dataStorePreferences_version")


api "com.kakao.sdk:v2-all:$rootProject.kakao_version"

// Hilt
implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$rootProject.hiltVersion"

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
Empty file added common/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions common/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.common

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.common.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.common.data.dto

data class LoginRequest(
val accessToken: String,
val providerType: String,
val userName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.common.data.dto

import com.example.common.data.entity.LoginData

data class LoginResponse(
val success: Boolean,
val data: LoginData
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.common.data.entity

data class LoginData(
val jwt: String,
val code: String
)
11 changes: 11 additions & 0 deletions common/src/main/java/com/example/common/data/source/AuthService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.common.data.source

import com.example.common.data.dto.LoginRequest
import com.example.common.data.dto.LoginResponse
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthService {
@POST("api/v1/oauth/login")
suspend fun login(@Body loginRequest: LoginRequest): LoginResponse
}
30 changes: 30 additions & 0 deletions common/src/main/java/com/example/common/di/DataStoreModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.common.di

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import com.example.common.domain.usecase.DataStoreUseCase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
class DataStoreModule {

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
name = DataStoreUseCase.DATA_STORE_NAME
)



@Singleton
@Provides
fun providePreferencesDataStore(@ApplicationContext context: Context) =
DataStoreUseCase(context.dataStore)

}
23 changes: 23 additions & 0 deletions common/src/main/java/com/example/common/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.common.di

import com.example.common.data.source.AuthService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.create
import javax.inject.Singleton

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

@Singleton
@Provides
fun provideAuthService(retrofit: Retrofit): AuthService {
return retrofit.create(AuthService::class.java)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.common.domain.usecase

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import androidx.datastore.preferences.core.Preferences
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class DataStoreUseCase @Inject constructor(
private val dataStore: DataStore<Preferences>
) {


val bearerJsonWebToken: Flow<String?> =
dataStore.data.map { preferences -> preferences[jsonWebToken_key]?.toBearerToken() }


val permissionNum: Flow<Int?> =
dataStore.data.map { preferences -> preferences[permission_num] }

private fun String.toBearerToken(): String = "Bearer $this"


suspend fun editJsonWebToken(jwt: String) {
dataStore.edit {
it[jsonWebToken_key] = jwt
}
}

suspend fun editPermissionNum(num: Int) {
dataStore.edit {
it[permission_num] = num
}
}


companion object {
const val DATA_STORE_NAME = "app"

private val jsonWebToken_key = stringPreferencesKey("JSON_WEB_TOKEN")
private val permission_num = intPreferencesKey("PERMISSION_NUM")
}

}
3 changes: 3 additions & 0 deletions common/src/main/java/com/example/common/util/Constant.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.common.util

const val NATIVE_APP_KEY = "5162225c94277a85e12566f227145303"
17 changes: 17 additions & 0 deletions common/src/test/java/com/example/common/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.common

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
1 change: 1 addition & 0 deletions foundation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading

0 comments on commit cdefdf3

Please sign in to comment.