Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed solution for Book list challenge #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 65 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,60 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

flavorDimensions += "environment"

productFlavors {
create("dev") {
dimension = "environment"
applicationIdSuffix = ".dev"
versionNameSuffix = "-dev"
buildConfigField("String", "BASE_URL", "\"https://api.nytimes.com\"")
buildConfigField("String", "API_KEY", project.property("API_KEY_DEV") as String)
resValue("string", "app_name", "OTAssignment-Dev")
}
create("staging") {
dimension = "environment"
applicationIdSuffix = ".staging"
versionNameSuffix = "-staging"
buildConfigField("String", "BASE_URL", "\"https://api.nytimes.com\"")
buildConfigField("String", "API_KEY", project.property("API_KEY_STAGING") as String)
resValue("string", "app_name", "OTAssignment-Staging")
}
create("production") {
dimension = "environment"
buildConfigField("String", "BASE_URL", "\"https://api.nytimes.com\"")
buildConfigField("String", "API_KEY", project.property("API_KEY_PROD") as String)
resValue("string", "app_name", "OTAssignment")
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
getByName("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
debuggable true
minifyEnabled = false
}
getByName("release") {
minifyEnabled = true
debuggable false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = "1.8"
}
buildFeatures {
buildConfig = true
viewBinding = true
}
}

Expand All @@ -40,6 +82,15 @@ dependencies {
implementation libs.material
implementation libs.androidx.activity
implementation libs.androidx.constraintlayout
implementation(libs.androidx.navigation.fragment.ktx)
implementation(libs.androidx.navigation.ui.ktx)
//gson
implementation(libs.converterGson)

//room
implementation(libs.androidx.room.ktx)
kapt(libs.room.compiler)

// dagger
implementation libs.dagger
kapt libs.dagger.compiler
Expand All @@ -51,6 +102,14 @@ dependencies {
//glide
implementation libs.glide

//Mockito
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.kotlinx.coroutines.test)

//Mockk
testImplementation(libs.mockk)

//reactive x
implementation libs.rx.android
implementation libs.rx.java
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

<application
android:name=".MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/java/com/example/otchallenge/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@ import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.otchallenge.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
(application as MyApplication).appComponent.inject(this)
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

setSupportActionBar(binding.toolbar)

val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
setupActionBarWithNavController(navController)
}

override fun onSupportNavigateUp(): Boolean {
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/example/otchallenge/MyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.otchallenge

import android.app.Application
import com.example.otchallenge.di.AppComponent
import com.example.otchallenge.di.AppModule
import com.example.otchallenge.di.DaggerAppComponent

class MyApplication : Application() {
Expand All @@ -10,6 +11,7 @@ class MyApplication : Application() {

override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder().build()
// Initialize the Dagger component with the application instance
appComponent = DaggerAppComponent.factory().create(AppModule(this))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.otchallenge.common.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

interface DelegateAdapter<VH : RecyclerView.ViewHolder, T : ItemModel> {

fun onCreateViewHolder(parent: ViewGroup): VH

fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: ItemModel)

fun isForViewType(item: ItemModel): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.otchallenge.common.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class DelegationAdapter(private val delegates: List<DelegateAdapter<out RecyclerView.ViewHolder, out ItemModel>>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var items: List<ItemModel> = listOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return delegates[viewType].onCreateViewHolder(parent)
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = items[position]
delegates[getItemViewType(position)].onBindViewHolder(holder, item)
}

override fun getItemViewType(position: Int): Int {
return delegates.indexOfFirst { it.isForViewType(items[position]) }
}

override fun getItemCount(): Int = items.size

fun setItems(items: List<ItemModel>) {
this.items = items
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.otchallenge.common.adapter

/**
* Marker interface of items for [AdapterDelegate]
*/
interface ItemModel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.otchallenge.common.util

import android.content.Context
import javax.inject.Inject

class ConnectivityProviderImpl @Inject constructor(
private val context: Context
) : ConnectivityProvider {

override fun isNetworkAvailable(): Boolean {
return NetworkUtil.isNetworkAvailable(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.otchallenge.common.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build

object NetworkUtil {

fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val network = connectivityManager.activeNetwork ?: return false
val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
return when {
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
else -> false
}
} else {
@Suppress("DEPRECATION")
val networkInfo = connectivityManager.activeNetworkInfo
return networkInfo != null && networkInfo.isConnected
}
}
}

interface ConnectivityProvider {
fun isNetworkAvailable(): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.otchallenge.data.local.entity

import android.net.Uri
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import androidx.room.TypeConverters

@Entity(tableName = "book")
@TypeConverters(UriConverter::class)
data class BookEntity(
@PrimaryKey
val bookUri: Uri,
val author: String,
val bookImage: String,
val bookImageHeight: Int,
val bookImageWidth: Int,
val contributor: String,
val contributorNote: String,
val description: String,
val price: String,
val publisher: String,
val rank: Int,
val rankLastWeek: Int,
val title: String,
val weeksOnList: Int
)

class UriConverter {

@TypeConverter
fun fromUri(uri: Uri): String {
return uri.toString()
}

@TypeConverter
fun toUri(uriString: String): Uri {
return Uri.parse(uriString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.otchallenge.data.local.room

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.otchallenge.data.local.entity.BookEntity

@Dao
interface BookDao {
@Query("SELECT * FROM book")
fun getBooks(): List<BookEntity>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertBooks(anime: List<BookEntity>)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.otchallenge.data.local.room

import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.otchallenge.data.local.entity.BookEntity

@Database(entities = [BookEntity::class], version = 1)
abstract class BookDatabase : RoomDatabase() {
abstract fun bookDao(): BookDao
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/otchallenge/data/remote/BookApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.otchallenge.data.remote

import com.example.otchallenge.data.remote.dto.BookResultDto
import retrofit2.http.GET
import retrofit2.http.Query

interface BookApi {
@GET("svc/books/v3/lists/current/hardcover-fiction.json?")
suspend fun getBooks(
@Query("api-key") apikey: String,
@Query("offset") offset: Int,
) : BookResultDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.otchallenge.data.remote.dto

import com.google.gson.annotations.SerializedName

data class BookDto(
val author: String,
@SerializedName("book_image")
val bookImage: String,
@SerializedName("book_image_height")
val bookImageHeight: Int,
@SerializedName("book_image_width")
val bookImageWidth: Int,
@SerializedName("book_uri")
val bookUri: String,
val contributor: String,
@SerializedName("contributor_note")
val contributorNote: String,
val description: String,
val price: String,
val publisher: String,
val rank: Int,
@SerializedName("rank_last_week")
val rankLastWeek: Int,
val title: String,
@SerializedName("weeks_on_list")
val weeksOnList: Int
)
Loading