Skip to content

Commit

Permalink
Merge pull request #17 from iamport/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kjh5833 authored Jan 28, 2021
2 parents 2affd1a + d5465c1 commit 56f1745
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 87 deletions.
76 changes: 72 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
```

> app build.gradle
[$SDK_VERSION][5]
[$SDK-VERSION][5]
```gradle
implementation 'com.github.iamport:iamport-android:$SDK_VERSION'
implementation 'com.github.iamport:iamport-android:$SDK-VERSION'
```

[5]: https://github.com/iamport/iamport-android/releases
Expand All @@ -45,6 +45,31 @@
### KOTLIN usage

> 필수구현 사항
```kotlin

// 일반적인 경우
// 사용하시는 안드로이드 어플리케이션 클래스에 추가하세요
class BaseApplication : Application() {
override fun onCreate() {
..
Iamport.create(this)
}
}

// DI 로 koin 을 사용하시는 경우
// 생성된 koinApplication 을 파라미터로 넘겨주셔야 합니다
class BaseApplication : Application() {
override fun onCreate() {
..
val koinApp = startKoin { .. }
Iamport.create(this, koinApp)
}

// KoinApplication 이 필요한 경우
Iamport.getKoinApplition()
}

```
```kotlin

// SDK 초기화
Expand Down Expand Up @@ -139,16 +164,41 @@ Iamport.isPolling()?.observe 에서 true 전달 받을 시점에, 직접 포그
### JAVA usage

> 자바 프로젝트에선 app build.gradle 에서 kotin-stblib 추가가 필요합니다
[$코틀린_버전][4]
[$코틀린-버전][4]

```gradle
implementation "org.jetbrains.kotlin:kotlin-stdlib:$코틀린_버전"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$코틀린-버전"
```

> 필수구현 사항. SDK 제공 api 별 설명은 위의 [KOTLIN usage][6] 를 참고하세요.
[6]:https://github.com/iamport/iamport-android#kotlin-usage

```java
// 일반적인 경우
// 사용하시는 안드로이드 어플리케이션 클래스에 추가하세요
public class BaseApplication extends Application {
@Override
public void onCreate() {
..
Iamport.INSTANCE.create(this, null);
}
}

// DI 로 koin 을 사용하시는 경우
// 생성된 koinApplication 을 파라미터로 넘겨주셔야 합니다
public class BaseApplication extends Application {
@Override
public void onCreate() {
..
KoinApplication koinApp = ..
Iamport.INSTANCE.create(this, koinApp);
}
}

```


```java

@Override
Expand Down Expand Up @@ -237,6 +287,24 @@ Iamport.isPolling()?.observe 에서 true 전달 받을 시점에, 직접 포그

---

[BaseApplication.kt (SDK 생성)](./app/src/main/java/com/iamport/sampleapp/BaseApplication.kt)

```kotlin
override fun onCreate() {
super.onCreate()
Iamport.create(this)

/**
* DI 로 KOIN 사용시 아래와 같이 사용
val koinApp = startKoin {
logger(AndroidLogger(Level.DEBUG))
androidContext(this@BaseApplication)
}
Iamport.create(this, koinApp)
*/
}
```

[PaymentFragment.kt (결제 화면)](./app/src/main/java/com/iamport/sampleapp/ui/PaymentFragment.kt)

```kotlin
Expand Down
16 changes: 13 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation "com.google.android.material:material:1.3.0-beta01"
implementation "com.google.android.material:material:1.3.0-rc01"

// multidex
implementation 'androidx.multidex:multidex:2.0.1'
Expand All @@ -78,8 +78,18 @@ dependencies {
// Logger
implementation 'com.orhanobut:logger:2.2.0'

// implementation project(':sdk')
implementation 'com.github.iamport:iamport-android:0.0.3-dev01'
implementation "org.koin:koin-androidx-scope:$koin_version"
// Koin AndroidX ViewModel features
implementation "org.koin:koin-androidx-viewmodel:$koin_version"
// Koin AndroidX Fragment features
implementation "org.koin:koin-androidx-fragment:$koin_version"
// Koin AndroidX WorkManager
implementation "org.koin:koin-androidx-workmanager:$koin_version"
// Koin AndroidX Jetpack Compose
implementation "org.koin:koin-androidx-compose:$koin_version"
// Koin AndroidX Experimental features
implementation "org.koin:koin-androidx-ext:$koin_version"

// implementation 'com.github.iamport:iamport-android:dev-SNAPSHOT'
}

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/com/iamport/sampleapp/BaseApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.iamport.sampleapp

import android.app.Application
import com.iamport.sdk.domain.core.Iamport
import org.koin.android.ext.koin.androidContext
import org.koin.android.logger.AndroidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level


class BaseApplication : Application() {
override fun onCreate() {
super.onCreate()
Iamport.create(this)

/**
* DI 로 KOIN 사용시 아래와 같이 사용
val koinApp = startKoin {
logger(AndroidLogger(Level.DEBUG))
androidContext(this@BaseApplication)
}
Iamport.create(this, koinApp)
*/
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
ext.gradle_versions_plugin = "0.36.0"
ext.gradle_version = "4.1.1"
ext.kotlin_version = "1.4.21"
ext.koin_version = '2.2.1'
ext.koin_version = '2.2.2'

repositories {
google()
Expand Down
4 changes: 2 additions & 2 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 210125001 // yymmdd000
versionName "0.0.3" // prod(x.y.z), dev(x.y.z-dev00)
versionCode 210128001 // yymmdd000
versionName "0.0.4" // prod(x.y.z), dev(x.y.z-dev00)
multiDexEnabled true

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
1 change: 0 additions & 1 deletion sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application
android:name=".presentation.App"
android:allowBackup="true"
android:supportsRtl="true"
android:usesCleartextTraffic="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import com.iamport.sdk.data.sdk.PG
data class UserData(
val pg_provider: PG?, // TODO: 2020-12-15 015 nullable 로 오는데.. 확인필요..
val pg_id: String,
val sandbox: Boolean,
val sandbox: Boolean?,
val type: String,
)
4 changes: 2 additions & 2 deletions sdk/src/main/java/com/iamport/sdk/domain/JsNativeInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import android.webkit.JavascriptInterface
import com.google.gson.Gson
import com.iamport.sdk.data.sdk.IamPortResponse
import com.iamport.sdk.data.sdk.Payment
import com.iamport.sdk.domain.di.IamportKoinComponent
import com.iamport.sdk.domain.utils.Event
import com.iamport.sdk.domain.utils.WebViewLiveDataEventBus
import com.orhanobut.logger.Logger
import org.koin.core.component.KoinApiExtension
import org.koin.core.component.KoinComponent

@KoinApiExtension
class JsNativeInterface(val payment: Payment, val gson: Gson, private val bus: WebViewLiveDataEventBus) : KoinComponent {
class JsNativeInterface(val payment: Payment, val gson: Gson, private val bus: WebViewLiveDataEventBus) : IamportKoinComponent {

/**
* 아임포트 JS SDK 에게 유저코드 전달§
Expand Down
85 changes: 81 additions & 4 deletions sdk/src/main/java/com/iamport/sdk/domain/core/Iamport.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
package com.iamport.sdk.domain.core

import android.app.Application
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.iamport.sdk.BuildConfig.DEBUG
import com.iamport.sdk.data.sdk.IamPortApprove
import com.iamport.sdk.data.sdk.IamPortRequest
import com.iamport.sdk.data.sdk.IamPortResponse
import com.iamport.sdk.data.sdk.Payment
import com.iamport.sdk.domain.di.IamportKoinContext
import com.iamport.sdk.domain.di.IamportKoinContext.koinApp
import com.iamport.sdk.domain.di.apiModule
import com.iamport.sdk.domain.di.appModule
import com.iamport.sdk.domain.di.httpClientModule
import com.iamport.sdk.domain.service.ChaiService
import com.iamport.sdk.domain.utils.PreventOverlapRun
import com.iamport.sdk.domain.utils.CONST
import com.iamport.sdk.domain.utils.Event
import com.iamport.sdk.domain.utils.Foreground
import com.iamport.sdk.domain.utils.PreventOverlapRun
import com.iamport.sdk.presentation.activity.IamportSdk
import com.iamport.sdk.presentation.contract.WebViewActivityContract
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.orhanobut.logger.Logger.d
import com.orhanobut.logger.PrettyFormatStrategy
import org.koin.android.ext.koin.androidContext
import org.koin.android.logger.AndroidLogger
import org.koin.core.KoinApplication
import org.koin.core.component.KoinApiExtension
import org.koin.core.context.startKoin
import org.koin.core.logger.Level


object Iamport {
Expand All @@ -32,10 +50,12 @@ object Iamport {
private var close = MutableLiveData<Event<Unit>>()
private var finish = MutableLiveData<Event<Unit>>()

var activity: ComponentActivity? = null
private var activity: ComponentActivity? = null
private var fragment: Fragment? = null
private var preventOverlapRun: PreventOverlapRun? = null

private var isCreated = false

private fun clear() {
fragment = null
activity = null
Expand All @@ -49,12 +69,63 @@ object Iamport {
this.preventOverlapRun = PreventOverlapRun()
}

private fun iamportCreated(): Boolean {
if (!isCreated) {
Log.e(CONST.IAMPORT_LOG, "IAMPORT SDK was not created. Please initialize it in Application class")
}
return isCreated
}

fun getKoinApplition(): KoinApplication? {
return koinApp
}

/**
* Application instance 를 통해 SDK 생명주기 감지, DI 초기화
*/
fun create(app: Application, koinApp: KoinApplication? = null) {

IamportKoinContext.koinApp = koinApp
?: startKoin {
logger(AndroidLogger(Level.DEBUG))
androidContext(app)
modules(httpClientModule, apiModule, appModule)
}

Foreground.init(app)

val logBuilder = PrettyFormatStrategy.newBuilder()
val formatStrategy: PrettyFormatStrategy = if (DEBUG) {
logBuilder
.methodCount(3)
.tag(CONST.IAMPORT_LOG)
.build()
} else {
logBuilder
.showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
.methodCount(0) // (Optional) How many method line to show. Default 2
.methodOffset(5) // (Optional) Hides internal method calls up to offset. Default 5
.tag(CONST.IAMPORT_LOG)
.build()
}
Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))

isCreated = true
d("Create IAMPORT SDK")
}

/**
* SDK Activity 열기 위한 Contract for Activity
* @param componentActivity : Host Activity
*/
fun init(componentActivity: ComponentActivity) {
d("init")

if (!iamportCreated()) {
return
}

d("INITIALIZE IAMPORT SDK for activity")

clear()
createInitialData()

Expand All @@ -78,7 +149,13 @@ object Iamport {
* @param fragment : Host Fragment
*/
fun init(fragment: Fragment) {
d("init")

if (!iamportCreated()) {
return
}

d("INITIALIZE IAMPORT SDK for fragment")

clear()
createInitialData()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.iamport.sdk.domain.di

import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.component.KoinComponent

// lib module
object IamportKoinContext {
var koinApp: KoinApplication? = null
}

interface IamportKoinComponent : KoinComponent {
override fun getKoin(): Koin = IamportKoinContext.koinApp?.koin!!
}
Loading

0 comments on commit 56f1745

Please sign in to comment.