Skip to content

Commit

Permalink
Extracted data module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Akashkamble committed Jul 2, 2020
1 parent 915e07e commit 24d6de7
Show file tree
Hide file tree
Showing 25 changed files with 200 additions and 72 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 7 additions & 19 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ android {
applicationId "com.akash.newzz_compose"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def apiKey = properties.getProperty("api_key")
buildConfigField("String", "API_KEY", apiKey)
versionCode 2
versionName "0.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -45,6 +41,7 @@ android {

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation project(':data')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
Expand All @@ -60,19 +57,6 @@ dependencies {
/* ---------------------LifeCycle Extension-----------------------------*/
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"


/* ---------------------Kotlin-coroutines---------------------------*/
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

/*---------------------Network and Moshi----------------------------*/
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
implementation 'com.squareup.retrofit2:converter-moshi:2.6.2'
implementation 'com.squareup.moshi:moshi:1.9.1'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.9.1'


/*-----------------------Koin-------------------------------------*/
implementation "org.koin:koin-android:$koin_version"
implementation "org.koin:koin-android-viewmodel:$koin_version"
Expand All @@ -83,6 +67,10 @@ dependencies {
/*---------------------CoilImageLoader------------------------------*/
implementation "dev.chrisbanes.accompanist:accompanist-coil:0.1.6"

/*----------------------------Moshi-------------------------------------*/
implementation 'com.squareup.moshi:moshi:1.9.1'


testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".NewsApplication"
android:name=".NewzzApplication"
android:theme="@style/AppTheme">
<activity
android:name=".ui.NewzzActivity"
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/akash/newzz_compose/NewzzApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.akash.newzz_compose

import com.akash.newzz.data.BaseApplication
import com.akash.newzz_compose.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

/**
* Created by Akash on 06/06/20
*/
class NewzzApplication : BaseApplication() {

override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@NewzzApplication)
modules(appModule)
}
}
}
8 changes: 4 additions & 4 deletions app/src/main/java/com/akash/newzz_compose/di/module.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.akash.newzz_compose.di

import com.akash.newzz_compose.data.apiservice.NewsApiService
import com.akash.newzz_compose.data.repository.NewsRepository
import com.akash.newzz_compose.data.repository.NewsRepositoryImpl
import com.akash.newzz.data.apiservice.NewzzApiService
import com.akash.newzz.data.repository.NewsRepository
import com.akash.newzz.data.repository.NewsRepositoryImpl
import com.akash.newzz_compose.viewmodel.NewzzViewModel
import com.squareup.moshi.Moshi
import org.koin.android.viewmodel.dsl.viewModel
Expand All @@ -15,6 +15,6 @@ import org.koin.dsl.module
val appModule = module {
viewModel { NewzzViewModel(get()) }
single { NewsRepositoryImpl(get(), get()) as NewsRepository }
single { NewsApiService() }
single { NewzzApiService() }
single { Moshi.Builder().build() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import androidx.ui.layout.preferredWidth
import androidx.ui.material.Divider
import androidx.ui.text.style.TextOverflow
import androidx.ui.unit.dp
import com.akash.newzz_compose.data.response.NewsArticle
import com.akash.newzz.data.response.NewsArticle
import com.akash.newzz_compose.style.articleTitleStyle
import com.akash.newzz_compose.style.dateTextStyle
import com.akash.newzz_compose.style.dividerColor
Expand Down Expand Up @@ -50,7 +50,7 @@ fun ArticleRow(article: NewsArticle, isDark: State<Boolean>, onClick: () -> Unit
Column {
if (!article.source.name.isNullOrEmpty()) {
Text(
text = article.source.name,
text = article.source.name!!,
style = if (isDark.value) sourceTextStyle.copy(color = sourceTextColorDark) else sourceTextStyle
)
HeightSpacer(value = 4.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.akash.newzz_compose.viewmodel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.akash.newzz.data.Result
import com.akash.newzz.data.repository.NewsRepository
import com.akash.newzz.data.response.NewsArticle
import com.akash.newzz_compose.Category
import com.akash.newzz_compose.data.repository.NewsRepository
import com.akash.newzz_compose.data.response.NewsArticle
import com.akash.newzz_compose.utils.Result
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
Expand Down
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
56 changes: 56 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def apiKey = properties.getProperty("api_key")
buildConfigField("String", "API_KEY", apiKey)
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

/* ---------------------Kotlin-coroutines---------------------------*/
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

/*---------------------Network and Moshi----------------------------*/
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
implementation 'com.squareup.retrofit2:converter-moshi:2.6.2'
implementation 'com.squareup.moshi:moshi:1.9.1'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.9.1'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}
Empty file added data/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions data/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,22 @@
package com.akash.newzz.data

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
* 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("com.akash.newzz.data.test", appContext.packageName)
}
}
6 changes: 6 additions & 0 deletions data/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.newzz.data">

/
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package com.akash.newzz_compose
package com.akash.newzz.data

import android.app.Application
import android.content.Context
import android.net.ConnectivityManager
import com.akash.newzz_compose.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin

/**
* Created by Akash on 06/06/20
* Created by Akash on 02/07/20
*/
class NewsApplication : Application() {

open class BaseApplication : Application() {

override fun onCreate() {
super.onCreate()
instances = this
startKoin {
androidLogger()
androidContext(this@NewsApplication)
modules(appModule)
}
}

companion object {
lateinit var instances: NewsApplication
lateinit var instances: BaseApplication

fun isNetworkConnected(): Boolean {
val connectivityManager =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.akash.newzz_compose
package com.akash.newzz.data

/**
* Created by Akash on 06/06/20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.akash.newzz_compose.utils
package com.akash.newzz.data

/**
* Created by Akash on 06/06/20
* Created by Akash on 02/07/20
*/

sealed class Result<out R> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.akash.newzz_compose.data.apiservice
package com.akash.newzz.data.apiservice

/**
* Created by Akash on 06/06/20
*/
import com.akash.newzz_compose.Constants
import com.akash.newzz_compose.NewsApplication
import com.akash.newzz_compose.data.response.NewsResponse
import com.akash.newzz.data.BaseApplication
import com.akash.newzz.data.Constants
import com.akash.newzz.data.response.NewsResponse
import java.io.File
import java.util.concurrent.TimeUnit
import okhttp3.Cache
Expand All @@ -18,8 +15,10 @@ import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query

interface NewsApiService {

/**
* Created by Akash on 02/07/20
*/
interface NewzzApiService {
@GET("top-headlines?sortBy=publishedAt&pageSize=50")
suspend fun getArticlesByCateGoryAsync(
@Query("category") category: String,
Expand All @@ -32,7 +31,7 @@ interface NewsApiService {
private const val HEADER_PRAGMA = "Pragma"
private const val cacheSize = (5 * 1024 * 1024).toLong() // 5 MB

operator fun invoke(): NewsApiService {
operator fun invoke(): NewzzApiService {
val requestInterceptor = Interceptor { chain ->

val response = chain.proceed(chain.request())
Expand All @@ -49,7 +48,7 @@ interface NewsApiService {
}

val cache = Cache(
File(NewsApplication.instances.cacheDir, "networkCache"),
File(BaseApplication.instances.cacheDir, "networkCache"),
cacheSize
)

Expand All @@ -63,7 +62,7 @@ interface NewsApiService {

var request = chain.request()

if (NewsApplication.isNetworkConnected()) {
if (BaseApplication.isNetworkConnected()) {

request = request
.newBuilder()
Expand Down Expand Up @@ -98,7 +97,7 @@ interface NewsApiService {
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(NewsApiService::class.java)
.create(NewzzApiService::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.akash.newzz_compose.data.repository
package com.akash.newzz.data.repository

import com.akash.newzz_compose.data.response.NewsResponse
import com.akash.newzz_compose.utils.Result
import com.akash.newzz.data.Result
import com.akash.newzz.data.response.NewsResponse

/**
* Created by Akash on 06/06/20
Expand Down
Loading

0 comments on commit 24d6de7

Please sign in to comment.