From 8dae5179435060e6228fc201987194f45b8f955c Mon Sep 17 00:00:00 2001 From: norangebit Date: Thu, 28 Nov 2019 12:08:37 +0100 Subject: [PATCH] add util - add PreferenceReader interface - add PreferenceWriter interface - add lazy singleton ServicesFactory - add retrofit dependencies into app module --- app/build.gradle | 4 ++ .../wedroid/app/util/PreferenceReader.java | 7 +++ .../wedroid/app/util/PreferenceWriter.java | 7 +++ .../ids/wedroid/app/util/ServicesFactory.kt | 59 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceReader.java create mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceWriter.java create mode 100644 app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/ServicesFactory.kt diff --git a/app/build.gradle b/app/build.gradle index c0fc443..5fb3211 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,6 +24,10 @@ android { } dependencies { + // retrofit + implementation "com.squareup.retrofit2:retrofit:2.6.2" + implementation "com.squareup.retrofit2:converter-gson:2.6.2" + implementation project(':wrapper') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceReader.java b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceReader.java new file mode 100644 index 0000000..3fe9de0 --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceReader.java @@ -0,0 +1,7 @@ +package it.unisannio.ding.ids.wedroid.app.util; + +public interface PreferenceReader { + String getBaseUrl(); + String getUserId(); + String getToken(); +} diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceWriter.java b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceWriter.java new file mode 100644 index 0000000..264b507 --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/PreferenceWriter.java @@ -0,0 +1,7 @@ +package it.unisannio.ding.ids.wedroid.app.util; + +public interface PreferenceWriter { + void setBaseUrl(String baseUrl); + void setUserId(String userId); + void setToken(String token); +} diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/ServicesFactory.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/ServicesFactory.kt new file mode 100644 index 0000000..aa0c3d2 --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/ServicesFactory.kt @@ -0,0 +1,59 @@ +package it.unisannio.ding.ids.wedroid.app.util + +import it.unisannio.ding.ids.wedroid.wrapper.api.* +import okhttp3.OkHttpClient +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory + +object ServicesFactory { + private val retrofit: Retrofit + private lateinit var reader: PreferenceReader //TODO + + init { + val httpClient = OkHttpClient.Builder() + .addInterceptor { + val request = it.request().newBuilder() + .addHeader("Authorization", "Bearer ${reader.token}") + .addHeader("Accept", "application/json") + .addHeader("Content-type", "application/json") + .build() + + it.proceed(request) + }.build() + + retrofit = Retrofit.Builder() + .baseUrl(reader.baseUrl) + .addConverterFactory(GsonConverterFactory.create()) + .client(httpClient) + .build() + } + + val boardService by lazy { + retrofit.create(BoardService::class.java) + } + + val cardCommentService by lazy { + retrofit.create(CardCommentService::class.java) + } + + val cardService by lazy { + retrofit.create(CardService::class.java) + } + + val checklistService by lazy { + retrofit.create(ChecklistService::class.java) + } + + val listService by lazy { + retrofit.create(ListService::class.java) + } + + val swimlanesService by lazy { + retrofit.create(SwimlanesService::class.java) + } + + val userService by lazy { + retrofit.create(UserService::class.java) + } +} +