Skip to content

Commit

Permalink
update: create simple fetch api using flow coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
amirisback committed Jan 17, 2024
1 parent c528dbc commit a294932
Show file tree
Hide file tree
Showing 28 changed files with 637 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.qomunal.opensource.androidresearch.common.ext

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
* Created by faisalamircs on 14/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


inline fun <reified T> fromJson(json: String?): T {
return Gson().fromJson(json, object : TypeToken<T>() {}.type)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent

import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -32,16 +30,13 @@ class NetworkModule {
@Provides
fun provideOkHTTPClient(
loggingInterceptor: HttpLoggingInterceptor,
cache: Cache,
@ApplicationContext context: Context
): OkHttpClient.Builder {

// Create the Interceptor
val chuckerInterceptor = ChuckerInterceptor.Builder(context)
.build()
val chuckerInterceptor = ChuckerInterceptor.Builder(context).build()

return OkHttpClient.Builder()
.cache(cache)
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.qomunal.opensource.androidresearch.di

import com.qomunal.opensource.androidresearch.domain.news.repository.NewsRepository
import com.qomunal.opensource.androidresearch.domain.news.repository.NewsRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

/**
* Created by faisalamircs on 17/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


@Module(includes = [ServiceModule::class])
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {

@Binds
abstract fun provideNewsRepository(repository: NewsRepositoryImpl): NewsRepository

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.qomunal.opensource.androidresearch.di

import com.qomunal.opensource.androidresearch.domain.meal.MealApiService
import com.qomunal.opensource.androidresearch.domain.news.NewsApiService
import com.qomunal.opensource.androidresearch.domain.news.remote.NewsApiService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -27,16 +26,15 @@ class ServiceModule {
httpClient: OkHttpClient.Builder,
converterFactory: Converter.Factory
): NewsApiService {
return NewsApiService.Creator().createApi(httpClient, converterFactory)
}

@Provides
fun provideMealApiService(
httpClient: OkHttpClient.Builder,
converterFactory: Converter.Factory
): MealApiService {
return MealApiService.Creator().createApi(httpClient, converterFactory)
return NewsApiService.Creator(httpClient, converterFactory).createApi()
}

// @Provides
// fun provideMealApiService(
// httpClient: OkHttpClient.Builder,
// converterFactory: Converter.Factory
// ): MealApiService {
// return MealApiService.Creator().createApi(httpClient, converterFactory)
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.qomunal.opensource.androidresearch.di

import com.qomunal.opensource.androidresearch.domain.news.usecase.NewsUseCase
import com.qomunal.opensource.androidresearch.domain.news.usecase.NewsUseCaseImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.scopes.ViewModelScoped

/**
* Created by faisalamircs on 17/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


@Module
@InstallIn(ViewModelComponent::class)
abstract class UseCaseModule {

@Binds
@ViewModelScoped
abstract fun provideNewsUseCase(useCase: NewsUseCaseImpl): NewsUseCase

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.qomunal.opensource.androidresearch.domain

sealed class Resource<T>(val data: T? = null, val message: String? = null) {
class Success<T>(data: T) : Resource<T>(data)
class Loading<T>(data: T? = null) : Resource<T>(data)
class Error<T>(message: String, data: T? = null) : Resource<T>(data, message)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.qomunal.opensource.androidresearch.domain.meal
package com.qomunal.opensource.androidresearch.domain.meal.remote


import com.google.gson.GsonBuilder
import com.qomunal.opensource.androidresearch.domain.meal.response.*
import com.qomunal.opensource.androidresearch.domain.meal.util.MealConstant
import com.qomunal.opensource.androidresearch.domain.meal.util.MealUrl
import okhttp3.OkHttpClient
import retrofit2.Converter
import retrofit2.Response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.qomunal.opensource.androidresearch.model

import androidx.annotation.Keep
package com.qomunal.opensource.androidresearch.domain.meal.remote

/**
* Created by faisalamircs on 13/01/2024
* Created by faisalamircs on 14/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
Expand All @@ -12,7 +10,5 @@ import androidx.annotation.Keep
*/


@Keep
data class MainModel(
val tag: String
)
class MealRemoteDataSource {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.qomunal.opensource.androidresearch.domain.meal
package com.qomunal.opensource.androidresearch.domain.meal.util

/**
* Created by Faisal Amir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.qomunal.opensource.androidresearch.domain.meal
package com.qomunal.opensource.androidresearch.domain.meal.util

/*
* Created by faisalamir on 27/07/21
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.qomunal.opensource.androidresearch.domain.news.mapper

import com.qomunal.opensource.androidresearch.domain.news.response.ArticleResponse
import com.qomunal.opensource.androidresearch.domain.news.response.SourceResponse
import com.qomunal.opensource.androidresearch.model.ArticleModel
import com.qomunal.opensource.androidresearch.model.SourceModel

/**
* Created by faisalamircs on 15/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


object NewsMapper {

object Source {
fun mapResponseToDomain(input: SourceResponse): SourceModel {
return SourceModel(
id = input.id ?: "",
name = input.name ?: "",
description = input.description ?: "",
url = input.url ?: "",
category = input.category ?: "",
language = input.language ?: "",
country = input.country ?: ""
)
}
}

object Article {

fun mapResponseToDomain(input: ArticleResponse): ArticleModel {
return ArticleModel(
source = Source.mapResponseToDomain(input.source ?: SourceResponse()),
author = input.author ?: "",
title = input.title ?: "",
description = input.description ?: "",
url = input.url ?: "",
urlToImage = input.urlToImage ?: "",
publishedAt = input.publishedAt ?: "",
content = input.content ?: ""
)
}

}


}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.qomunal.opensource.androidresearch.domain.news
package com.qomunal.opensource.androidresearch.domain.news.remote

import com.google.gson.GsonBuilder
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_API_KEY
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_CATEGORY
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_COUNTRY
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_DOMAINS
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_EXCLUDE_DOMAINS
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_FROM
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_LANGUAGE
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_PAGE
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_PAGE_SIZE
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_Q
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_Q_IN_TITLE
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_SORT_BY
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_SOURCES
import com.qomunal.opensource.androidresearch.domain.news.NewsConstant.QUERY_TO
import com.qomunal.opensource.androidresearch.domain.news.NewsUrl.URL_EVERYTHING
import com.qomunal.opensource.androidresearch.domain.news.NewsUrl.URL_SOURCES
import com.qomunal.opensource.androidresearch.domain.news.NewsUrl.URL_TOP_HEADLINE
import com.qomunal.opensource.androidresearch.domain.news.response.*
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_API_KEY
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_CATEGORY
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_COUNTRY
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_DOMAINS
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_EXCLUDE_DOMAINS
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_FROM
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_LANGUAGE
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_PAGE
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_PAGE_SIZE
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_Q
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_Q_IN_TITLE
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_SORT_BY
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_SOURCES
import com.qomunal.opensource.androidresearch.domain.news.util.NewsConstant.QUERY_TO
import com.qomunal.opensource.androidresearch.domain.news.util.NewsUrl
import com.qomunal.opensource.androidresearch.domain.news.util.NewsUrl.URL_EVERYTHING
import com.qomunal.opensource.androidresearch.domain.news.util.NewsUrl.URL_SOURCES
import com.qomunal.opensource.androidresearch.domain.news.util.NewsUrl.URL_TOP_HEADLINE
import okhttp3.OkHttpClient
import retrofit2.Converter
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
Expand Down Expand Up @@ -55,7 +55,7 @@ interface NewsApiService {
@Query(QUERY_COUNTRY) country: String?,
@Query(QUERY_PAGE_SIZE) pageSize: Int?,
@Query(QUERY_PAGE) page: Int?
): Response<ArticlesResponse>
): ArticlesResponse

// Get Everythings
@GET(URL_EVERYTHING)
Expand All @@ -72,7 +72,7 @@ interface NewsApiService {
@Query(QUERY_SORT_BY) sortBy: String?,
@Query(QUERY_PAGE_SIZE) pageSize: Int?,
@Query(QUERY_PAGE) page: Int?
): Response<ArticlesResponse>
): ArticlesResponse

// Get Sources
@GET(URL_SOURCES)
Expand All @@ -81,11 +81,13 @@ interface NewsApiService {
@Query(QUERY_LANGUAGE) language: String,
@Query(QUERY_COUNTRY) country: String,
@Query(QUERY_CATEGORY) category: String
): Response<SourcesResponse>
): SourcesResponse

class Creator {
@Inject
fun createApi(httpClient: OkHttpClient.Builder, converterFactory: Converter.Factory): NewsApiService {
class Creator @Inject constructor(
private val httpClient: OkHttpClient.Builder,
private val converterFactory: Converter.Factory
) {
fun createApi(): NewsApiService {
val retrofit = Retrofit.Builder().baseUrl(NewsUrl.BASE_URL)
.addConverterFactory(converterFactory)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
Expand Down
Loading

0 comments on commit a294932

Please sign in to comment.