Skip to content

Commit

Permalink
Merge pull request #1 from Roaim/lorem_picksum
Browse files Browse the repository at this point in the history
lorem_picsum
  • Loading branch information
Roaim authored Aug 18, 2019
2 parents 62f1ed2 + 4990edf commit 6f268ea
Show file tree
Hide file tree
Showing 29 changed files with 191 additions and 425 deletions.
5 changes: 5 additions & 0 deletions lib_pin_downloader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ android {
testOptions {
unitTests.returnDefaultValues = true
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
Expand All @@ -49,6 +53,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.retrofit2:adapter-guava:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.14.0'
/*
// Basic Dagger 2 (required)
implementation "com.google.dagger:dagger:$dagger2_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.roaim.pindownloader

import com.google.gson.Gson
import com.google.gson.JsonElement
import com.google.gson.stream.JsonReader
import com.roaim.pindownloader.core.CacheDataSource
import com.roaim.pindownloader.core.PinDownloader
import com.roaim.pindownloader.core.RemoteDataSource
import okhttp3.ResponseBody
import java.io.StringReader

object JsonCacheDataSource : CacheDataSource<JsonElement>() {
@ExperimentalStdlibApi
Expand All @@ -17,7 +19,9 @@ object JsonCacheDataSource : CacheDataSource<JsonElement>() {
object JsonRemoteDataSource : RemoteDataSource<JsonElement>() {
override suspend fun convert(response: ResponseBody?): JsonElement? =
response?.string().let {
Gson().fromJson(it, JsonElement::class.java)
val reader = JsonReader(StringReader(it))
reader.isLenient = true
Gson().fromJson(reader, JsonElement::class.java)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.roaim.pindownloader.core

import com.roaim.pindownloader.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
Expand All @@ -16,12 +18,23 @@ interface RemoteApi {
suspend fun getContent(@Url url: String): ResponseBody //TODO replace ResponseBody with Response<ResponseBody> for better control

companion object Factory {
fun create(): RemoteApi {
fun create(client: OkHttpClient = httpClient()): RemoteApi {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.baseUrl(BuildConfig.BASE_URL)
.build()
return retrofit.create(RemoteApi::class.java)
}

fun httpClient(loggingInterceptor : HttpLoggingInterceptor = loggingInterceptor()) : OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
}

fun loggingInterceptor() : HttpLoggingInterceptor {
return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.roaim.pindownloader

import android.graphics.Bitmap
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.roaim.pindownloader.core.RemoteApi
import com.roaim.pindownloader.mock.MockBitmapRemoteDataSource
import kotlinx.coroutines.Dispatchers
Expand All @@ -12,12 +13,14 @@ import org.hamcrest.CoreMatchers.instanceOf
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.*

class BitmapRemoteDataSourceTest {
private val url = "https://images.unsplash.com"

@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
@ExperimentalCoroutinesApi
private val testDispatcher = TestCoroutineDispatcher()
@ExperimentalCoroutinesApi
Expand Down
6 changes: 5 additions & 1 deletion sample_pinboard_app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionName "2.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "String", "SAMPLE_JSON_URL", SAMPLE_JSON_URL
}
Expand All @@ -24,6 +24,10 @@ android {
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
Expand Down
1 change: 0 additions & 1 deletion sample_pinboard_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".TestActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.roaim.kotlinpinboard

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import kotlinx.android.synthetic.main.activity_pin_board.*

class PinActivity : AppCompatActivity() {
Expand All @@ -11,20 +10,6 @@ class PinActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pin_board)
setSupportActionBar(toolbar)

fab.setOnClickListener {
findNavController(R.id.fragmentNavHost).apply {
navigate(
when (currentDestination?.id) {
R.id.pinBoardFragment2 -> R.id.action_pinBoardFragment2_to_testActivity2
R.id.pinDetailsFragment -> R.id.action_pinDetailsFragment2_to_testActivity2
else -> {
R.id.action_pinBoardFragment2_to_testActivity2
}
}
)
}
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
package com.roaim.kotlinpinboard.data

import android.os.Handler
import android.os.Looper
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.Transformations
import androidx.paging.ItemKeyedDataSource
import com.roaim.kotlinpinboard.BuildConfig
import com.roaim.kotlinpinboard.data.model.Pin
import androidx.paging.PageKeyedDataSource
import com.roaim.kotlinpinboard.data.model.LoremPicksum
import com.roaim.kotlinpinboard.utils.Constants
import com.roaim.kotlinpinboard.utils.observeOnceInMain
import com.roaim.pindownloader.BitmapPinDownloader
import com.roaim.pindownloader.JsonPinDownloader
import com.roaim.pindownloader.toPoJo

class PinDataSource(
private val jsonPinDownloader: JsonPinDownloader = JsonPinDownloader(),
private val bitmapPinDownloader: BitmapPinDownloader = BitmapPinDownloader()
) : ItemKeyedDataSource<String, Pin>() {
override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<Pin>) {
) : PageKeyedDataSource<Int, LoremPicksum>() {
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, LoremPicksum>) {
}

override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<Pin>) {
getPin(params.key).apply {
Handler(Looper.getMainLooper()).post {
observeForever {
callback.onResult(it)
removeObserver {}
}
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, LoremPicksum>) {
getPin(Constants.getImageListUrl(params.key, params.requestedLoadSize)).apply {
observeOnceInMain(Observer {
it?.also { callback.onResult(it, params.key.inc()) }
})
}
}

override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<Pin>) {
getPin(BuildConfig.SAMPLE_JSON_URL).apply {
Handler(Looper.getMainLooper()).post {
observeForever {
callback.onResult(it)
removeObserver {}
}
}
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, LoremPicksum>) {
getPin(Constants.getImageListUrl()).apply {
observeOnceInMain(Observer {
it?.also { callback.onResult(it, 0, 2) }
})
}
}

override fun getKey(item: Pin): String = BuildConfig.SAMPLE_JSON_URL

private fun getPin(url: String) = Transformations.switchMap(
jsonPinDownloader.download(url)
) {
it?.toPoJo(Array<Pin>::class.java)?.run {
MutableLiveData<List<Pin>>(asList())
it?.toPoJo(Array<LoremPicksum>::class.java)?.run {
MutableLiveData<List<LoremPicksum>>(asList())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.roaim.kotlinpinboard.data

import androidx.paging.DataSource
import com.roaim.kotlinpinboard.data.model.Pin
import com.roaim.kotlinpinboard.data.model.LoremPicksum

class PinDataSourceFactory(private val pinDataSource: PinDataSource) : DataSource.Factory<String, Pin>() {
override fun create(): DataSource<String, Pin> {
class PinDataSourceFactory(private val pinDataSource: PinDataSource) : DataSource.Factory<Int, LoremPicksum>() {
override fun create(): DataSource<Int, LoremPicksum> {
return pinDataSource
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.roaim.kotlinpinboard.data
import androidx.lifecycle.LiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import com.roaim.kotlinpinboard.data.model.Pin
import com.roaim.kotlinpinboard.data.model.LoremPicksum

class PinRepository(private val pinDataSource: PinDataSource = PinDataSource()) {
private val dataSourceFactory = PinDataSourceFactory(pinDataSource)

fun getBitmap(url: String) = pinDataSource.getBitmap(url)

fun getPins(): LiveData<PagedList<Pin>> {
fun getPins(): LiveData<PagedList<LoremPicksum>> {
val config = PagedList.Config.Builder()
.setPageSize(10)
.setEnablePlaceholders(false)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.roaim.kotlinpinboard.data.model

data class LoremPicksum(
val author: String? = null,
val width: Int? = null,
val downloadUrl: String? = null,
val id: String? = null,
val url: String? = null,
val height: Int? = null
)
Loading

0 comments on commit 6f268ea

Please sign in to comment.