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

Android Developer Nexaas - Ricarlo Silva #39

Open
wants to merge 24 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
11 changes: 11 additions & 0 deletions Nexaas/.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
gradlew
gradlew.bat
35 changes: 35 additions & 0 deletions Nexaas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Test Android Developer [Nexaas](https://nexaas.com/)


### Code Features
----------
- [Kotlin](https://kotlinlang.org/)
- [Coroutines](https://developer.android.com/kotlin/coroutines)
- Dependency injection in multi modules with [Koin](https://insert-koin.io/)
- [Retrofit](https://square.github.io/retrofit/) + [Gson](https://github.com/google/gson).
- [MVVM](https://developer.android.com/jetpack/guide)
- [Android Architecture Components](https://developer.android.com/topic/libraries/architecture)
- [Data Binding](https://developer.android.com/topic/libraries/data-binding)
- [LiveData](https://developer.android.com/topic/libraries/architecture/livedata)
- [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel)
- [Navigation](https://developer.android.com/topic/libraries/architecture/navigation)
- [Room](https://developer.android.com/topic/libraries/architecture/room)
- Unit Testing with [JUnit4](https://github.com/junit-team/junit4), [Mockito](https://site.mockito.org/) and [Hamcrest](http://hamcrest.org/JavaHamcrest/tutorial)
- Load images with [Glide](https://github.com/bumptech/glide)

### App Modules
----------
- app
- common
- network
- data
- domain
- storage

### Screens
----------
<img width="300" src="/Nexaas/screenshots/screenshot_cart.png"> <img width="300" src="/Nexaas/screenshots/screenshot_product_details.png">

### Author
----------
Ricarlo Silva - [LinkedIn](https://www.linkedin.com/in/ricarlo-silva/)
1 change: 1 addition & 0 deletions Nexaas/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
60 changes: 60 additions & 0 deletions Nexaas/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply from: "$rootDir/flavors.gradle"

android {
compileSdkVersion versions.compileSdkVersion
buildToolsVersion versions.buildToolsVersion

defaultConfig {
applicationId "br.com.nexaas"
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode versions.versionCode
versionName versions.versionName

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

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

buildFeatures {
dataBinding = true
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])

implementation(project(":common"))
implementation(project(":data"))
implementation(project(":domain"))
implementation(project(":network"))

implementation deps.androidx.lifecycle.livedata
implementation deps.androidx.lifecycle.viewmodel
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

// Navigation
implementation deps.androidx.navigation.fragment
implementation deps.androidx.navigation.ui

// Test
testImplementation deps.junit
testImplementation deps.mockito.core
testImplementation deps.hamcrest.library
testImplementation deps.koin.test
testImplementation deps.coroutines.test
testImplementation deps.androidx.test.arch
androidTestImplementation deps.androidx.test.junit
androidTestImplementation deps.androidx.test.espresso

}
40 changes: 40 additions & 0 deletions Nexaas/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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


#### Coroutines ####
# ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepnames class kotlinx.coroutines.android.AndroidExceptionPreHandler {}
-keepnames class kotlinx.coroutines.android.AndroidDispatcherFactory {}

# Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembernames class kotlinx.* {
volatile <fields>;
}

# Same story for the standard library's SafeContinuation that also uses AtomicReferenceFieldUpdater
-keepclassmembernames class kotlin.coroutines.SafeContinuation {
volatile <fields>;
}
###################
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package br.com.nexaas

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("br.com.nexaas", appContext.packageName)
}
}
25 changes: 25 additions & 0 deletions Nexaas/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.nexaas">

<application
android:name=".App"
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/AppTheme">
<activity android:name=".features.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".features.product.ProductDetailsActivity"
android:theme="@style/ProductTheme" />
</application>

</manifest>
33 changes: 33 additions & 0 deletions Nexaas/app/src/main/java/br/com/nexaas/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package br.com.nexaas

import android.app.Application
import br.com.nexaas.common.di.CommonModule
import br.com.nexaas.data.di.DataModule
import br.com.nexaas.di.PresentationModule
import br.com.nexaas.domain.di.DomainModule
import br.com.nexaas.network.di.NetworkModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin

class App : Application() {

override fun onCreate() {
super.onCreate()
setupDI()
}

private fun setupDI() {
startKoin {
androidContext(this@App)
modules(
listOf(
NetworkModule.module,
CommonModule.module,
DataModule.module,
DomainModule.module,
PresentationModule.module
)
)
}
}
}
18 changes: 18 additions & 0 deletions Nexaas/app/src/main/java/br/com/nexaas/di/PresentationModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.nexaas.di

import br.com.nexaas.features.cart.CartViewModel
import br.com.nexaas.features.product.ProductDetailsViewModel
import org.koin.android.viewmodel.dsl.viewModel
import org.koin.dsl.module

object PresentationModule {
val module = module {
viewModel {
CartViewModel(getCartUseCase = get(), dispatcher = get())
}

viewModel {
ProductDetailsViewModel()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package br.com.nexaas.features.cart

import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import br.com.nexaas.BR
import br.com.nexaas.R
import br.com.nexaas.common.ui.base.adapter.BaseAdapter
import br.com.nexaas.common.ui.base.adapter.BaseViewHolder
import br.com.nexaas.features.cart.data.entity.CartItemVO

class CartAdapter(private val listener: OnClickListener) : BaseAdapter<CartItemVO, CartAdapter.ViewHolder>(DiffCallback) {

object DiffCallback : DiffUtil.ItemCallback<CartItemVO>() {

override fun areItemsTheSame(oldItem: CartItemVO, newItem: CartItemVO): Boolean {
return oldItem.name == newItem.name
}

override fun areContentsTheSame(oldItem: CartItemVO, newItem: CartItemVO): Boolean {
return oldItem == newItem
}
}

inner class ViewHolder(private val binding: ViewDataBinding, private val listener: OnClickListener) : BaseViewHolder<CartItemVO>(binding) {
override fun onBind(item: CartItemVO, position: Int) {
binding.setVariable(BR.item, item)
binding.setVariable(BR.listener, listener)
binding.executePendingBindings()
}
}

override fun instantiateViewHolder(view: ViewDataBinding, viewType: Int): ViewHolder {
return ViewHolder(view, listener)
}

override fun getItemView(viewType: Int): Int {
return R.layout.item_list_cart
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package br.com.nexaas.features.cart

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import br.com.nexaas.R
import br.com.nexaas.common.ui.base.BaseFragment
import br.com.nexaas.common.ui.base.ViewState
import br.com.nexaas.databinding.FragmentCartBinding
import br.com.nexaas.features.cart.data.entity.CartItemVO
import br.com.nexaas.features.cart.data.mapper.CartItemToProductMapper
import com.google.android.material.snackbar.Snackbar
import org.koin.android.viewmodel.ext.android.viewModel
import java.io.IOException

class CartFragment : BaseFragment<FragmentCartBinding>(), OnClickListener {

private val viewModel: CartViewModel by viewModel()

private val cartAdapter by lazy { CartAdapter(this) }

override fun getLayoutRes(): Int {
return R.layout.fragment_cart
}

override fun initView(savedInstanceState: Bundle?) {
binding.apply {
this.viewmodel = viewModel
}
binding.rvItemsCart.apply {
adapter = cartAdapter
addItemDecoration(
DividerItemDecoration(
context,
DividerItemDecoration.VERTICAL
)
)
}
subscribeUi()
}

private fun subscribeUi() {
viewModel.items.observe(viewLifecycleOwner, Observer {
when (it) {
is ViewState.Success -> {
cartAdapter.submitList(it.data.items)
}
is ViewState.Error -> {
val message = when (it.error) {
is IOException -> {
getString(R.string.network_error)
}
else -> {
getString(R.string.generic_error)
}
}
Snackbar.make(binding.root, message, Snackbar.LENGTH_INDEFINITE)
.setAction(getString(R.string.action_try_again)) { _ ->
it.retry?.invoke()
}.show()
}
is ViewState.Loading -> {
}
}
})
}

override fun onClickProduct(itemVO: CartItemVO) {
val product = CartItemToProductMapper().transform(itemVO)

val destinations = CartFragmentDirections.navigateToProductDetails(product)
findNavController().navigate(destinations)
}
}
Loading