Skip to content

Commit

Permalink
- Applied previous commits from develop
Browse files Browse the repository at this point in the history
- Token feature + Record Activity, cannot cast Activity[]
- ListOf instead of ArrayOf + post<String>
  • Loading branch information
Joaquim Puyo authored and Joaquim Puyo committed Jun 23, 2020
1 parent 16bdfa1 commit 9c9a394
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CommonLocalDataSource(private val settings: Settings) : LocalDataSource {

override suspend fun saveToken(registerDataResponse: RegisterDataResponse): Either<Error, Success> {
return try {
settings.putString(ACCESS_TOKEN, Json.stringify(RegisterDataResponse.serializer(), registerDataResponse))
settings.putString(ACCESS_TOKEN, registerDataResponse.token)
Either.Right(Success)
} catch (e: Exception) {
Either.Left(Error.TokenNotSaved)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.wordline.helios.rewarding.sdk.data.datasource.remote

import com.wordline.helios.rewarding.sdk.data.datasource.local.LocalDataSource
import com.wordline.helios.rewarding.sdk.domain.model.Activity
import com.wordline.helios.rewarding.sdk.domain.model.Either
import com.wordline.helios.rewarding.sdk.domain.model.Error
import com.wordline.helios.rewarding.sdk.domain.model.Success
import io.ktor.client.HttpClient
import io.ktor.client.features.ClientRequestException
import io.ktor.client.features.json.JsonFeature
Expand All @@ -13,11 +16,13 @@ import io.ktor.client.features.logging.SIMPLE
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.post
import io.ktor.http.HttpStatusCode
import io.ktor.http.takeFrom

class CommonRemoteDataSource : RemoteDataSource {
class CommonRemoteDataSource(localDataSource: LocalDataSource) : RemoteDataSource {

companion object {
const val END_POINT_HELIOS = "https://devel3.tempos21.com"
private const val TOKEN_HEADER = "Authorization"
}

private val client = HttpClient {
Expand All @@ -36,23 +41,32 @@ class CommonRemoteDataSource : RemoteDataSource {
install(JsonFeature) {
serializer = KotlinxSerializer()
}

install(TokenFeature) {
tokenHeaderName = TOKEN_HEADER
tokenProvider = localDataSource
}
}

override suspend fun registerUser(userID: String, context: String): Either<Error, RegisterDataResponse> = execute {
override suspend fun registerUser(
userID: String,
context: String
): Either<Error, RegisterDataResponse> = execute {
client.post<RegisterDataResponseDto> {
call("/hrm-api/auth/networkUser/register")
val json = io.ktor.client.features.json.defaultSerializer()
body = json.write(RegisterData(userID = userID, context = context))
body = json.write(RegisterData(userID = userID, context = context))
}.toModel()
}

/*override suspend fun registerActivity(action: String, date: String): Either<Error, Success> = execute {
client.post<Success> {
call("/hrm-api/activities/record")
val json = io.ktor.client.features.json.defaultSerializer()
body = json.write(Activity(action = action, date = date))
override suspend fun registerActivity(action: String, date: String): Either<Error, Success> =
execute {
client.post<String> {
call("/hrm-api/activities/record")
val json = io.ktor.client.features.json.defaultSerializer()
body = json.write(listOf(Activity(action = action, date = date)))
}.toSuccess()
}
}*/

private suspend fun <R> execute(f: suspend () -> R): Either<Error, R> =
try {
Expand All @@ -74,9 +88,12 @@ class CommonRemoteDataSource : RemoteDataSource {

private fun HttpRequestBuilder.call(path: String) {
url {
//FIX ME
//takeFrom(END_POINT)
//encodedPath = "$path&appid=$API_KEY"
takeFrom(END_POINT_HELIOS)
encodedPath = "$path"
}
}

private fun String.toSuccess(): Success {
return Success
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.wordline.helios.rewarding.sdk.data.datasource.remote

import com.wordline.helios.rewarding.sdk.domain.model.Either
import com.wordline.helios.rewarding.sdk.domain.model.Error
import com.wordline.helios.rewarding.sdk.domain.model.Success

interface RemoteDataSource {
suspend fun registerUser(userID: String, context: String): Either<Error, RegisterDataResponse>
suspend fun registerActivity(action: String, date: String): Either<Error, Success>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.wordline.helios.rewarding.sdk.data.datasource.remote

import com.wordline.helios.rewarding.sdk.data.datasource.local.LocalDataSource
import io.ktor.client.HttpClient
import io.ktor.client.features.HttpClientFeature
import io.ktor.client.request.HttpRequestPipeline
import io.ktor.client.request.header
import io.ktor.util.AttributeKey

class TokenFeature private constructor(
private val tokenHeaderName: String,
private val tokenProvider: LocalDataSource
) {

class Config {
var tokenHeaderName: String? = null
var tokenProvider: LocalDataSource? = null
fun build() = TokenFeature(
tokenHeaderName ?: throw IllegalArgumentException("HeaderName should be contain"),
tokenProvider ?: throw IllegalArgumentException("TokenProvider should be contain")
)
}

companion object Feature : HttpClientFeature<Config, TokenFeature> {
override val key = AttributeKey<TokenFeature>("TokenFeature")

override fun prepare(block: Config.() -> Unit) = Config().apply(block).build()

override fun install(feature: TokenFeature, scope: HttpClient) {
scope.requestPipeline.intercept(HttpRequestPipeline.State) {
feature.tokenProvider.getToken().apply {
this.fold(
error = {},
success = { context.header(feature.tokenHeaderName, "Bearer $it") }
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ class CommonRepository(
private val local: LocalDataSource
) : Repository {

override suspend fun registerUser(userID: String, context: String): Either<Error, RegisterDataResponse> =
remote.registerUser(userID, context)
.flatMap {registerDataResponse ->
override suspend fun registerUser(
userID: String,
heliosContext: String
): Either<Error, RegisterDataResponse> =
remote.registerUser(userID, heliosContext)
.flatMap { registerDataResponse ->
local.saveToken(registerDataResponse.success)
.flatMap { registerDataResponse }

}

override suspend fun registerActivity(action: String, date: String): Either<Error, Success> =
remote.registerActivity(action, date)

override suspend fun getToken(): Either<Error, String> {
return local.getToken()
return try {
local.getToken()
} catch (e: Exception) {
Either.Left(Error.IO(e.message ?: ""))
}
}

override suspend fun removeToken(): Either<Error, Success> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import com.wordline.helios.rewarding.sdk.domain.model.Error
import com.wordline.helios.rewarding.sdk.domain.model.Success

interface Repository {
suspend fun registerUser(userID: String, heliosContext: String): Either<Error, RegisterDataResponse>
suspend fun registerUser(
userID: String,
heliosContext: String
): Either<Error, RegisterDataResponse>

suspend fun registerActivity(action: String, date: String): Either<Error, Success>
suspend fun removeToken(): Either<Error, Success>
suspend fun getToken(): Either<Error, String>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wordline.helios.rewarding.sdk.domain.model

import kotlinx.serialization.Serializable

@Serializable
data class Activity(
val action: String,
val date: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.worldline.helios.sampleapp.ui.app
import android.app.Application
import android.content.Context
import com.russhwolf.settings.AndroidSettings
import com.wordline.helios.rewarding.sdk.HeliosSdkImpl
import com.wordline.helios.rewarding.sdk.HeliosSdk
import com.wordline.helios.rewarding.sdk.HeliosSdkInterface
import com.wordline.helios.rewarding.sdk.data.datasource.local.CommonLocalDataSource
import com.wordline.helios.rewarding.sdk.data.datasource.local.LocalDataSource
import com.wordline.helios.rewarding.sdk.data.datasource.remote.CommonRemoteDataSource
Expand Down Expand Up @@ -45,7 +45,7 @@ val domainModule = Kodein.Module("domainModule") {

val dataModule = Kodein.Module("dataModule") {
bind<HeliosSdk>() with singleton { HeliosSdkImpl() }
bind<RemoteDataSource>() with singleton { CommonRemoteDataSource() }
bind<RemoteDataSource>() with singleton { CommonRemoteDataSource(localDataSource = instance()) }
bind<LocalDataSource>() with singleton {
val context: Context = instance()
CommonLocalDataSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.worldline.helios.sampleapp.ui.presenter
import com.wordline.helios.rewarding.sdk.data.repository.Repository
import com.worldline.helios.sampleapp.ui.error.ErrorHandler
import com.worldline.helios.sampleapp.ui.executor.Executor
import kotlinx.coroutines.launch

class RecordPresenter(
private val repository: Repository,
Expand All @@ -14,8 +15,17 @@ class RecordPresenter(
override fun attach() {

}

fun registerActivity(action: String, date: String) {
scope.launch {
execute { repository.registerActivity(action, date) }.fold(
error = { println("error") },
success = { view.showSuccess() }
)
}
}
}

interface RecordView : View {

fun showSuccess()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.worldline.helios.sampleapp.ui.view.activity

import android.content.Context
import android.content.Intent
import android.widget.Toast
import com.worldline.helios.sampleapp.ui.app.ACTIVITY_MODULE
import com.worldline.helios.sampleapp.ui.extension.toast
import com.worldline.helios.sampleapp.ui.presenter.AuthPresenter
import com.worldline.helios.sampleapp.ui.presenter.AuthView
import kotlinx.android.synthetic.main.activity_auth.*
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
Expand Down Expand Up @@ -35,12 +38,16 @@ class AuthActivity : RootActivity<AuthView>(), AuthView {
}

override fun registerListeners() {
// Do nothing

button_sendAuth.setOnClickListener() { v ->
presenter.registerUser(
userID = editUserID.text.toString(),
context = editContext.text.toString()
);
}
}

override fun showSuccess() {

toast("User registered.", Toast.LENGTH_LONG)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.worldline.helios.sampleapp.ui.view.activity

import android.content.Context
import android.content.Intent
import android.widget.Toast
import com.worldline.helios.sampleapp.R
import com.worldline.helios.sampleapp.ui.app.ACTIVITY_MODULE
import com.worldline.helios.sampleapp.ui.extension.toast
import com.worldline.helios.sampleapp.ui.presenter.RecordPresenter
import com.worldline.helios.sampleapp.ui.presenter.RecordView
import kotlinx.android.synthetic.main.activity_record.*
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
Expand Down Expand Up @@ -36,8 +39,16 @@ class RecordActivity : RootActivity<RecordView>(), RecordView {
}

override fun registerListeners() {
// Do nothing
button_sendActivity.setOnClickListener { v ->
presenter.registerActivity(
action = editAction.text.toString(),
date = editDate.text.toString()
);
}
}

override fun showSuccess() {
toast("Activity registered.", Toast.LENGTH_SHORT)
}

}
6 changes: 3 additions & 3 deletions helios-sampleapp/src/main/res/layout/activity_record.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@
app:layout_constraintTop_toTopOf="parent">

<EditText
android:id="@+id/editTextTextPersonName3"
android:id="@+id/editAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/edit_action" />

<EditText
android:id="@+id/editTextTextPersonName4"
android:id="@+id/editDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/edit_date" />

<Button
android:id="@+id/button2"
android:id="@+id/button_sendActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_send" />
Expand Down

0 comments on commit 9c9a394

Please sign in to comment.