├── app ├── .gitignore ├── keystore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── styles.xml │ │ │ │ └── strings.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ ├── repo_item.xml │ │ │ │ ├── activity_repositories.xml │ │ │ │ └── activity_login.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── epam │ │ │ │ └── talks │ │ │ │ └── github │ │ │ │ ├── search │ │ │ │ ├── RepositoriesView.kt │ │ │ │ ├── RepositoriesPresenter.kt │ │ │ │ ├── RxRepositoriesPresenter.kt │ │ │ │ └── SuspendingRepositoriesPresenter.kt │ │ │ │ ├── GithubRepository.kt │ │ │ │ ├── GithubUser.kt │ │ │ │ ├── presenters │ │ │ │ ├── LoginPresenter.kt │ │ │ │ ├── LoginPresenterImpl.kt │ │ │ │ └── SuspendingLoginPresenterImpl.kt │ │ │ │ ├── utils │ │ │ │ └── AndroidJob.kt │ │ │ │ ├── model │ │ │ │ ├── RepositoryUtils.kt │ │ │ │ ├── SuspendingApiClient.kt │ │ │ │ ├── ApiClientRx.kt │ │ │ │ └── ApiClient.kt │ │ │ │ ├── LoginActivity.kt │ │ │ │ └── RepositoriesActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── epam │ │ │ └── talks │ │ │ └── github │ │ │ ├── ExampleUnitTest.kt │ │ │ ├── StateFlowTest.kt │ │ │ ├── presenters │ │ │ ├── LoginPresenterTest.kt │ │ │ └── SuspendingLoginPresenterTest.kt │ │ │ └── model │ │ │ ├── ApiClientRxImplTest.kt │ │ │ ├── SuspendingApiClientImplTest.kt │ │ │ └── ApiClientImplTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── epam │ │ └── talks │ │ └── github │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── local.properties ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/keystore -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vvsevolodovich/github-kotlin-coroutines/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/search/RepositoriesView.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.search 2 | 3 | interface RepositoriesView { 4 | fun showRepositoryList(repos: List) 5 | 6 | } -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/GithubRepository.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github 2 | 3 | data class GithubRepository( 4 | val id : Int, 5 | val name : String, 6 | val full_name : String 7 | ) 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/GithubUser.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github 2 | 3 | data class GithubUser ( 4 | val login : String, 5 | val id : Int, 6 | val repos_url : String, 7 | val name : String 8 | ) 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/search/RepositoriesPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.search 2 | 3 | interface RepositoriesPresenter { 4 | 5 | suspend fun searchRepositories(query: String) 6 | 7 | } -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/presenters/LoginPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.presenters 2 | 3 | interface LoginPresenter { 4 | 5 | suspend fun doLogin(login: String, pass: String) : List 6 | 7 | 8 | } -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jan 02 12:24:03 MSK 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/search/RxRepositoriesPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.search 2 | 3 | public class RxRepositoriesPresenter(private val view: RepositoriesView) : RepositoriesPresenter { 4 | 5 | override suspend fun searchRepositories(query: String) { 6 | 7 | } 8 | 9 | } -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file must *NOT* be checked into Version Control Systems, 2 | # as it contains information specific to your local configuration. 3 | # 4 | # Location of the SDK. This is only used by Gradle. 5 | # For customization when using a Version Control System, please read the 6 | # header note. 7 | #Wed Jul 01 23:07:08 MSK 2020 8 | sdk.dir=/Users/vladimir_ivanov4/Library/Android/sdk 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/search/SuspendingRepositoriesPresenter.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.search 2 | 3 | import com.epam.talks.github.model.ApiClient 4 | 5 | class SuspendingRepositoriesPresenter(private val view: RepositoriesView, val apiClient: ApiClient) : RepositoriesPresenter { 6 | 7 | override suspend fun searchRepositories(query: String) { 8 | val foundRepositories = apiClient.searchRepositories(query).await() 9 | val list = foundRepositories.map { it.full_name } 10 | view.showRepositoryList(list) 11 | } 12 | } -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/utils/AndroidJob.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.utils 2 | 3 | import android.arch.lifecycle.Lifecycle 4 | import android.arch.lifecycle.LifecycleObserver 5 | import android.arch.lifecycle.OnLifecycleEvent 6 | import android.util.Log 7 | import kotlinx.coroutines.Job 8 | 9 | class AndroidJob(lifecycle: Lifecycle) : Job by Job(), LifecycleObserver { 10 | 11 | init { 12 | lifecycle.addObserver(this) 13 | } 14 | 15 | @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 16 | fun destroy() { 17 | Log.d("AndroidJob", "Cancelling a coroutine") 18 | cancel() 19 | } 20 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/repo_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/presenters/LoginPresenterImpl.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.presenters 2 | 3 | import com.epam.talks.github.model.ApiClient 4 | import com.epam.talks.github.model.Authorization 5 | 6 | class LoginPresenterImpl(val apiClient: ApiClient) : LoginPresenter { 7 | 8 | fun showProgress(show: Boolean) { 9 | 10 | } 11 | 12 | override suspend fun doLogin(login: String, pass: String): List { 13 | val auth = Authorization(login, pass) 14 | val userInfo = apiClient.login(auth).await() 15 | val repoUrl = userInfo.repos_url 16 | val list = apiClient.getRepositories(repoUrl, auth).await() 17 | return list.map { it -> it.full_name } 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/epam/talks/github/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.epam.talks.github", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Github 3 | 4 | 5 | Email 6 | Password (optional) 7 | Sign in or register 8 | Sign in 9 | This email address is invalid 10 | This password is too short 11 | This password is incorrect 12 | This field is required 13 | "Contacts permissions are needed for providing email completions." 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/model/RepositoryUtils.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import com.epam.talks.github.GithubRepository 4 | import com.github.kittinunf.fuel.json.FuelJson 5 | import org.json.JSONArray 6 | import java.util.ArrayList 7 | 8 | fun FuelJson.toRepos(): ArrayList { 9 | val array = this.array() 10 | return array.toRepos(); 11 | } 12 | 13 | fun JSONArray.toRepos(): ArrayList { 14 | val jsonArray = this 15 | val repos = ArrayList(jsonArray.length()) 16 | for (i in 0..(jsonArray.length() - 1)) { 17 | val item = jsonArray.getJSONObject(i) 18 | with(item) { 19 | repos.add(GithubRepository(getInt("id"), 20 | getString("name"), 21 | getString("full_name") 22 | ) 23 | ) 24 | } 25 | } 26 | return repos 27 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/StateFlowTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github 2 | 3 | import android.util.Base64 4 | import junit.framework.Assert.assertTrue 5 | import kotlinx.coroutines.CoroutineScope 6 | import kotlinx.coroutines.flow.MutableStateFlow 7 | import kotlinx.coroutines.flow.collect 8 | import kotlinx.coroutines.launch 9 | import kotlinx.coroutines.test.TestCoroutineDispatcher 10 | import org.junit.Test 11 | 12 | class StateFlowTest { 13 | 14 | 15 | @Test 16 | fun shouldListenToEvents() { 17 | val flow = MutableStateFlow("") 18 | var read = false 19 | flow.value = "Hello!" 20 | 21 | CoroutineScope(TestCoroutineDispatcher()).launch { 22 | flow.collect { it -> 23 | print(it) 24 | read = true 25 | } 26 | } 27 | 28 | assertTrue(read) 29 | } 30 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # github-kotlin-coroutines 2 | 3 | This project demonstrate how to use coroutines of Kotlin to perform async work in Android application. 4 | The best thing about them is that you can write async code in a sync style. The excerpt you're most interested in is: 5 | 6 | ```kotlin 7 | launch(UI) { 8 | showProgress(true) 9 | val auth = BasicAuthorization(login, pass) 10 | try { 11 | val userInfo = login(auth).await() 12 | val repoUrl = userInfo!!.repos_url 13 | val list = getRepositories(repoUrl, auth).await() 14 | showRepositories(this@LoginActivity, list!!.map { it -> it.full_name }) 15 | } catch (e: RuntimeException) { 16 | Toast.makeText(this@LoginActivity, e.message, LENGTH_LONG).show() 17 | } finally { 18 | showProgress(false) 19 | } 20 | } 21 | ``` 22 | 23 | login() and getRepositories() method are performed off the main thread, but the only thing you need to bother about is using .await() call after. 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/presenters/SuspendingLoginPresenterImpl.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.presenters 2 | 3 | import com.epam.talks.github.model.Authorization 4 | import com.epam.talks.github.model.SuspendingApiClient 5 | import kotlinx.coroutines.newSingleThreadContext 6 | import kotlinx.coroutines.withContext 7 | import kotlin.coroutines.CoroutineContext 8 | 9 | class SuspendingLoginPresenterImpl(val apiClient: SuspendingApiClient, val context: CoroutineContext = newSingleThreadContext("LoginPresenterPool")) : LoginPresenter { 10 | 11 | fun showProgress(show: Boolean) { 12 | 13 | } 14 | 15 | override suspend fun doLogin(login: String, pass: String): List { 16 | val auth = Authorization(login, pass) 17 | val userInfo = withContext(context) { apiClient.login(auth) } 18 | val repoUrl = userInfo.repos_url 19 | val list = withContext(context) { apiClient.getRepositories(repoUrl, auth) } 20 | return list.map { it -> it.full_name } 21 | } 22 | } -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/model/SuspendingApiClient.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import com.epam.talks.github.GithubRepository 4 | import com.epam.talks.github.GithubUser 5 | import com.github.kittinunf.fuel.httpGet 6 | 7 | interface SuspendingApiClient { 8 | 9 | suspend fun login(auth: Authorization) : GithubUser 10 | suspend fun getRepositories(reposUrl: String, auth: Authorization) : List 11 | suspend fun searchRepositories(searchQuery: String) : List 12 | 13 | class SuspendingApiClientImpl : SuspendingApiClient { 14 | 15 | override suspend fun searchRepositories(query: String): List = 16 | ArrayList() 17 | //"https://api.github.com/search/repositories?q=${query}".httpGet() 18 | 19 | 20 | override suspend fun login(auth: Authorization): GithubUser = 21 | GithubUser("login", 1, 22 | "repos_url","name") 23 | 24 | 25 | 26 | override suspend fun getRepositories(reposUrl: String, auth: Authorization): List 27 | = ArrayList() 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/model/ApiClientRx.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import com.epam.talks.github.GithubRepository 4 | import com.epam.talks.github.GithubUser 5 | import io.reactivex.Observable 6 | import io.reactivex.Single 7 | 8 | interface ApiClientRx { 9 | 10 | fun login(auth: Authorization) : Single 11 | fun getRepositories(reposUrl: String, auth: Authorization) : Single> 12 | fun searchRepositories(query: String) : Observable> 13 | 14 | class ApiClientRxImpl : ApiClientRx { 15 | override fun searchRepositories(query: String): Observable> { 16 | return Observable.fromCallable { 17 | ArrayList() 18 | } 19 | } 20 | 21 | override fun login(auth: Authorization): Single = Single.fromCallable { 22 | GithubUser("dummy", 1, 23 | "dummy_url", "dummy_name") 24 | } 25 | 26 | override fun getRepositories(reposUrl: String, auth: Authorization): Single> { 27 | return Single.fromCallable({ 28 | ArrayList() 29 | }) 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/presenters/LoginPresenterTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.presenters 2 | 3 | import com.epam.talks.github.GithubRepository 4 | import com.epam.talks.github.GithubUser 5 | import com.epam.talks.github.model.ApiClient 6 | import io.mockk.coEvery 7 | import io.mockk.mockk 8 | import kotlinx.coroutines.runBlocking 9 | import org.junit.Assert.assertNotNull 10 | import org.junit.Test 11 | import java.util.* 12 | 13 | class LoginPresenterTest { 14 | 15 | @Test 16 | fun testLogin() { 17 | val apiClient = mockk() 18 | val githubUser = GithubUser("login", 1, "url", "name") 19 | val repositories = GithubRepository(1, "repos_name", "full_repos_name") 20 | 21 | coEvery { apiClient.login(any()).await() } returns githubUser 22 | coEvery { apiClient.getRepositories(any(), any()).await() } returns Arrays.asList(repositories) 23 | 24 | val loginPresenterImpl = LoginPresenterImpl(apiClient) 25 | runBlocking { 26 | val repos = loginPresenterImpl.doLogin("login", "password") 27 | assertNotNull(repos) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/presenters/SuspendingLoginPresenterTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.presenters 2 | 3 | import com.epam.talks.github.GithubRepository 4 | import com.epam.talks.github.GithubUser 5 | import com.epam.talks.github.model.ApiClient 6 | import com.epam.talks.github.model.SuspendingApiClient 7 | import io.mockk.coEvery 8 | import io.mockk.every 9 | import io.mockk.mockk 10 | import kotlinx.coroutines.newSingleThreadContext 11 | import kotlinx.coroutines.runBlocking 12 | import org.junit.Assert.assertNotNull 13 | import org.junit.Test 14 | import java.util.* 15 | 16 | class SuspendingLoginPresenterTest { 17 | 18 | @Test 19 | fun testLogin() = runBlocking { 20 | val apiClient = mockk() 21 | val githubUser = GithubUser("login", 1, "url", "name") 22 | val repositories = GithubRepository(1, "repos_name", "full_repos_name") 23 | 24 | coEvery { apiClient.login(any()) } returns githubUser 25 | coEvery { apiClient.getRepositories(any(), any()) } returns Arrays.asList(repositories) 26 | 27 | val loginPresenterImpl = SuspendingLoginPresenterImpl(apiClient, newSingleThreadContext("testPoolSuspending")) 28 | runBlocking { 29 | val repos = loginPresenterImpl.doLogin("login", "password") 30 | assertNotNull(repos) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_repositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 16 | 17 | 25 | 26 | 31 | 32 | 33 | 34 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/model/ApiClientRxImplTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import io.mockk.every 4 | import io.mockk.mockk 5 | import io.mockk.staticMockk 6 | import io.mockk.use 7 | import khttp.get 8 | import khttp.responses.GenericResponse 9 | import khttp.structures.authorization.BasicAuthorization 10 | import org.json.JSONObject 11 | import org.junit.Assert 12 | import org.junit.Test 13 | 14 | 15 | class ApiClientRxImplTest { 16 | 17 | private val loginJson = "{ \"login\": \"login\", \"id\": 1, \"repos_url\": \"url\", \"name\": \"name\" }" 18 | 19 | @Test 20 | fun login() { 21 | val apiClientImpl = ApiClientRx.ApiClientRxImpl() 22 | val genericResponse = mockLoginResponse() 23 | 24 | staticMockk("khttp.KHttp").use { 25 | every { get("https://api.github.com/user", auth = any()) } returns genericResponse 26 | 27 | val githubUser = 28 | apiClientImpl 29 | .login(BasicAuthorization("login", "pass")) 30 | 31 | githubUser.subscribe({ githubUser -> 32 | Assert.assertNotNull(githubUser) 33 | Assert.assertEquals("name", githubUser.name) 34 | Assert.assertEquals("url", githubUser.repos_url) 35 | }) 36 | 37 | } 38 | } 39 | 40 | private fun mockLoginResponse(): GenericResponse { 41 | val genericResponse = mockk() 42 | every { genericResponse.jsonObject } returns JSONObject(loginJson) 43 | every { genericResponse.statusCode } returns 200 44 | return genericResponse 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/model/SuspendingApiClientImplTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import io.mockk.every 4 | import io.mockk.mockk 5 | import io.mockk.staticMockk 6 | import io.mockk.use 7 | import khttp.get 8 | import khttp.responses.GenericResponse 9 | import khttp.structures.authorization.BasicAuthorization 10 | import kotlinx.coroutines.runBlocking 11 | import org.json.JSONObject 12 | import org.junit.Assert.assertEquals 13 | import org.junit.Assert.assertNotNull 14 | import org.junit.Test 15 | 16 | class SuspendingApiClientImplTest { 17 | 18 | private val loginJson = "{ \"login\": \"login\", \"id\": 1, \"repos_url\": \"url\", \"name\": \"name\" }" 19 | 20 | @Test 21 | fun login() = runBlocking { 22 | val apiClientImpl = SuspendingApiClient.SuspendingApiClientImpl() 23 | val genericResponse = mockLoginResponse() 24 | 25 | staticMockk("khttp.KHttp").use { 26 | every { get("https://api.github.com/user", auth = any()) } returns genericResponse 27 | 28 | val githubUser = 29 | apiClientImpl 30 | .login(BasicAuthorization("login", "pass")) 31 | 32 | assertNotNull(githubUser) 33 | assertEquals("name", githubUser.name) 34 | assertEquals("url", githubUser.repos_url) 35 | } 36 | } 37 | 38 | private fun mockLoginResponse(): GenericResponse { 39 | val genericResponse = mockk() 40 | every { genericResponse.jsonObject } returns JSONObject(loginJson) 41 | every { genericResponse.statusCode } returns 200 42 | return genericResponse 43 | } 44 | 45 | @Test 46 | fun getRepositories() { 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /app/src/test/java/com/epam/talks/github/model/ApiClientImplTest.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import io.mockk.every 4 | import io.mockk.mockk 5 | import io.mockk.staticMockk 6 | import io.mockk.use 7 | import khttp.get 8 | import khttp.responses.GenericResponse 9 | import khttp.structures.authorization.BasicAuthorization 10 | import kotlinx.coroutines.newSingleThreadContext 11 | import kotlinx.coroutines.runBlocking 12 | import org.json.JSONObject 13 | import org.junit.Assert.assertEquals 14 | import org.junit.Assert.assertNotNull 15 | import org.junit.Test 16 | 17 | class ApiClientImplTest { 18 | 19 | private val loginJson = "{ \"login\": \"login\", \"id\": 1, \"repos_url\": \"url\", \"name\": \"name\" }" 20 | 21 | @Test 22 | fun login() { 23 | val apiClientImpl = ApiClient.ApiClientImpl(newSingleThreadContext("testPool")) 24 | val genericResponse = mockLoginResponse() 25 | 26 | staticMockk("khttp.KHttp").use { 27 | every { get("https://api.github.com/user", auth = any()) } returns genericResponse 28 | 29 | runBlocking { 30 | val githubUser = 31 | apiClientImpl 32 | .login(BasicAuthorization("login", "pass")) 33 | .await() 34 | 35 | assertNotNull(githubUser) 36 | assertEquals("name", githubUser.name) 37 | assertEquals("url", githubUser.repos_url) 38 | } 39 | } 40 | } 41 | 42 | private fun mockLoginResponse(): GenericResponse { 43 | val genericResponse = mockk() 44 | every { genericResponse.jsonObject } returns JSONObject(loginJson) 45 | every { genericResponse.statusCode } returns 200 46 | return genericResponse 47 | } 48 | 49 | @Test 50 | fun getRepositories() { 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | buildToolsVersion "29.0.1" 8 | 9 | defaultConfig { 10 | applicationId "com.epam.talks.github" 11 | minSdkVersion 21 12 | targetSdkVersion 28 13 | versionCode 2 14 | versionName "2.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | packagingOptions { 24 | exclude 'META-INF/AL2.0' 25 | exclude 'META-INF/LGPL2.1' 26 | } 27 | 28 | lintOptions { 29 | checkReleaseBuilds false 30 | //abortOnError false 31 | } 32 | } 33 | 34 | dependencies { 35 | implementation fileTree(dir: 'libs', include: ['*.jar']) 36 | 37 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' 38 | implementation "io.reactivex.rxjava2:rxjava:2.1.9" 39 | 40 | 41 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 42 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" 43 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutines_version" 44 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version" 45 | implementation "com.android.support:recyclerview-v7:28.0.0" 46 | implementation 'com.github.kittinunf.fuel:fuel:2.2.3' 47 | implementation 'com.github.kittinunf.fuel:fuel-android:2.2.3' 48 | implementation 'com.github.kittinunf.fuel:fuel-coroutines:2.2.3' 49 | implementation 'com.github.kittinunf.fuel:fuel-json:2.2.3' 50 | 51 | implementation 'com.android.support:appcompat-v7:28.0.0' 52 | implementation 'com.android.support:design:28.0.0' 53 | 54 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 55 | testImplementation 'junit:junit:4.12' 56 | testImplementation "io.mockk:mockk:1.8.13.kotlin13" 57 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 58 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' 59 | } 60 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/epam/talks/github/model/ApiClient.kt: -------------------------------------------------------------------------------- 1 | package com.epam.talks.github.model 2 | 3 | import android.util.Base64 4 | import android.util.Base64.NO_WRAP 5 | import android.util.Log 6 | import com.epam.talks.github.GithubRepository 7 | import com.epam.talks.github.GithubUser 8 | import com.github.kittinunf.fuel.core.Request 9 | import kotlinx.coroutines.CoroutineScope 10 | import kotlinx.coroutines.Deferred 11 | import kotlinx.coroutines.async 12 | 13 | import com.github.kittinunf.fuel.httpGet 14 | import com.github.kittinunf.fuel.json.FuelJson 15 | import com.github.kittinunf.fuel.json.responseJson 16 | import kotlinx.coroutines.Dispatchers 17 | import java.lang.Exception 18 | 19 | import kotlin.coroutines.CoroutineContext 20 | 21 | data class Authorization( 22 | val login: String, 23 | val password: String 24 | ) 25 | 26 | interface ApiClient: CoroutineScope { 27 | 28 | fun login(auth: Authorization) : Deferred 29 | fun getRepositories(reposUrl: String, auth: Authorization) : Deferred> 30 | fun searchRepositories(searchQuery: String) : Deferred> 31 | 32 | class ApiClientImpl(override val coroutineContext: CoroutineContext) : ApiClient { 33 | 34 | private val ioScope = CoroutineScope(Dispatchers.IO) 35 | 36 | override fun searchRepositories(query: String): Deferred> = ioScope.async { 37 | val request = "https://api.github.com/search/repositories?q=${query}".httpGet() 38 | val (req, response, result) = request.responseJson() 39 | val (data, error) = result 40 | try { 41 | data?.let { 42 | return@async it.obj().getJSONArray("items").toRepos() 43 | } 44 | } catch (e: Exception) { 45 | Log.e("TAG", "Failed to read data", e); 46 | } 47 | return@async emptyList() 48 | } 49 | 50 | 51 | override fun login(auth: Authorization): Deferred = ioScope.async { 52 | val request = "https://api.github.com/user".httpGet() 53 | addAuth(request, auth) 54 | val (req, response, result) = request.responseJson() 55 | val (data, error) = result 56 | val fuelJson = data as FuelJson 57 | 58 | with(fuelJson.obj()) { 59 | return@async GithubUser(getString("login"), getInt("id"), 60 | getString("repos_url"), getString("name")) 61 | } 62 | } 63 | 64 | 65 | override fun getRepositories(reposUrl: String, auth: Authorization): Deferred> = ioScope.async { 66 | val request = reposUrl.httpGet() 67 | addAuth(request, auth) 68 | val (req, response, result) = request.responseJson() 69 | val (data, error) = result 70 | return@async (data as FuelJson).toRepos() 71 | } 72 | 73 | private fun addAuth(request: Request, auth: Authorization) { 74 | request.header("Authorization", "Basic " + Base64.encodeToString("${auth.login}:${auth.password}".toByteArray(), NO_WRAP)) 75 | } 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 21 | 22 | 26 | 27 | 32 | 33 | 37 | 38 | 41 | 42 | 50 | 51 | 52 | 53 | 56 | 57 | 68 | 69 | 70 | 71 |