Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed May 9, 2021
0 parents commit 958ba17
Show file tree
Hide file tree
Showing 59 changed files with 1,522 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.iml
.gradle
/local.properties
/.idea/
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
78 changes: 78 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlinx-serialization'
id 'kotlin-kapt'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "dev.androidbroadcast.sample.paging3"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField 'String', 'NEWS_API_KEY', '"a3aebd96c7c94de09b552a2aee1675d4"'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
viewBinding true
}
}

dependencies {
//noinspection GradleDependency
implementation 'androidx.core:core-ktx:1.5.0-rc02'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.activity:activity-ktx:1.2.3'
implementation 'androidx.fragment:fragment-ktx:1.3.3'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

def lifecycleVersion = '2.4.0-alpha01'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.0"

implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0-RC'

implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0"
implementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.4.6'
implementation "io.coil-kt:coil:1.2.1"

def roomVersion = "2.3.0"
implementation "androidx.room:room-runtime:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"

def daggerVersion = "2.35.1"
implementation "com.google.dagger:dagger:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"

testImplementation 'junit:junit:4.13.2'

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.androidbroadcast.sample.paging3

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dev.androidbroadcast.sample.paging3", appContext.packageName)
}
}
28 changes: 28 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.androidbroadcast.sample.paging3"
>

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".NewsApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.News"
>

<activity android:name=".ui.home.HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.androidbroadcast.sample.paging3

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

class Dispatchers(
val default: CoroutineDispatcher,
val io: CoroutineDispatcher,
val main: CoroutineDispatcher,
) {

companion object {

val Default = Dispatchers(
default = Dispatchers.Default,
io = Dispatchers.IO,
main = Dispatchers.Main.immediate,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.androidbroadcast.sample.paging3

import android.app.Application
import android.content.Context
import dev.androidbroadcast.sample.paging3.di.AppComponent
import dev.androidbroadcast.sample.paging3.di.DaggerAppComponent


class NewsApplication : Application() {

private var _appComponent: AppComponent? = null

val appComponent: AppComponent
get() = checkNotNull(_appComponent)

override fun onCreate() {
super.onCreate()
_appComponent = DaggerAppComponent.builder()
.application(this)
.create()
}
}

val Context.appComponent: AppComponent
get() = when (this) {
is NewsApplication -> appComponent
else -> (applicationContext as NewsApplication).appComponent
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.androidbroadcast.sample.paging3.data

import dev.androidbroadcast.sample.paging3.data.model.Article
import dev.androidbroadcast.sample.paging3.data.model.Source
import dev.androidbroadcast.sample.paging3.data.network.model.ArticleDto
import dev.androidbroadcast.sample.paging3.data.network.model.SourceDto
import dev.androidbroadcast.sample.paging3.data.storage.model.ArticleWithSourceDbo
import dev.androidbroadcast.sample.paging3.data.storage.model.SourceDbo

internal fun ArticleWithSourceDbo.toArticle(): Article {
return Article(
source = this.source.toSource(),
title = article.title,
url = article.url,
description = article.description,
author = article.author,
urlToImage = article.urlToImage,
publishedAt = article.publishedAt,
content = article.content
)
}

internal fun ArticleDto.toArticle(): Article {
return Article(
source = this.source?.toSource(),
title = title,
url = url,
description = description,
author = author,
urlToImage = urlToImage,
publishedAt = publishedAt,
content = content
)
}

private fun SourceDto.toSource(): Source {
return Source(id = id, name = name)
}

internal fun SourceDbo.toSource(): Source {
return Source(id = sourceId, name = name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.androidbroadcast.sample.paging3.data

import dev.androidbroadcast.sample.paging3.data.model.Article
import dev.androidbroadcast.sample.paging3.data.network.NewsService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class NewsRepository @Inject constructor(
private val newsService: NewsService,
) {

fun queryAll(query: String): Flow<List<Article>> {
if (query.isBlank()) return flowOf(emptyList())

return flow {
val response = newsService.everything(query)
emit(response.body()!!.articles)
}
.map { articles ->
articles.map { article ->
article.toArticle()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.androidbroadcast.sample.paging3.data.model

data class Article(
val source: Source?,
val title: String,
val url: String?,
val description: String?,
val author: String?,
val urlToImage: String?,
val publishedAt: String?,
val content: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.androidbroadcast.sample.paging3.data.model

data class Source(
val id: String?,
val name: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.androidbroadcast.sample.paging3.data.network

import okhttp3.Interceptor
import okhttp3.Response

class AuthInterceptor(private val apiKey: String) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.addHeader(API_KEY_HEADER, apiKey)
.build()

return chain.proceed(request)
}

private companion object {

private const val API_KEY_HEADER = "X-Api-Key"
}
}
Loading

0 comments on commit 958ba17

Please sign in to comment.