Skip to content

Commit

Permalink
Dagger2 setup & network bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulshah2010 committed Jul 17, 2018
1 parent 637df4d commit dcce63d
Show file tree
Hide file tree
Showing 68 changed files with 1,280 additions and 118 deletions.
2 changes: 2 additions & 0 deletions .idea/modules.xml

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

56 changes: 54 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28
Expand All @@ -26,13 +25,42 @@ androidExtensions {
experimental = true
}

ext {
androidSupportVersion = "27.1.1"
timberVersion = "4.6.1"
butterknifeVersion = "8.8.1"
rxJavaVersion = "2.1.9"
rxandroidVersion = "2.0.2"
rxBindingVersion = "2.1.1"
picassoVersion = "2.5.2"
gsonVersion = "2.7"
roomVersion = "2.0.0-beta01"
retrofitVersion = "2.3.0"
daggerVersion = "2.11"
okhttpVersion = "3.9.1"
debugDatabaseVersion = "1.0.3"
circleIndicator = "1.2.2@aar"
photoView = "2.1.3"
rxbinding = "2.1.1"
junitVersion = "4.12"
testRunnerVersion = "1.0.1"
espressoVersion = "3.0.1"
mockitoVersion = "2.10.0"
}

dependencies {

def anko_version = '0.10.5'

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

implementation "androidx.room:room-runtime:${roomVersion}"
implementation "androidx.room:room-rxjava2:${roomVersion}"
kapt "androidx.room:room-compiler:${roomVersion}"

implementation 'androidx.appcompat:appcompat:1.0.0-beta01'

implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
testImplementation 'junit:junit:4.12'
Expand All @@ -42,4 +70,28 @@ dependencies {
implementation "org.jetbrains.anko:anko:$anko_version"
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.palette:palette:1.0.0-beta01'

implementation "com.jakewharton.timber:timber:${timberVersion}"

implementation "io.reactivex.rxjava2:rxjava:${rxJavaVersion}"
implementation "io.reactivex.rxjava2:rxandroid:${rxandroidVersion}"
implementation "com.google.code.gson:gson:${gsonVersion}"

implementation "com.squareup.retrofit2:retrofit:${retrofitVersion}"
implementation "com.squareup.retrofit2:converter-gson:${retrofitVersion}"
implementation "com.squareup.retrofit2:converter-scalars:${retrofitVersion}"
implementation "com.squareup.retrofit2:adapter-rxjava2:${retrofitVersion}"
implementation "com.squareup.okhttp3:okhttp:${okhttpVersion}"
implementation "com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
implementation "com.google.dagger:dagger:${daggerVersion}"
implementation "com.google.dagger:dagger-android-support:${daggerVersion}"
implementation "com.jakewharton.rxbinding2:rxbinding-appcompat-v7:${rxbinding}"
kapt "com.jakewharton:butterknife-compiler:${butterknifeVersion}"
kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
kapt "com.google.dagger:dagger-android-processor:${daggerVersion}"
debugImplementation "com.amitshekhar.android:debug-db:${debugDatabaseVersion}"
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
compile project(path: ':domain')
compile project(path: ':data')

}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".FoodApp"
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"
tools:ignore="GoogleAppIndexingWarning">
tools:ignore="AllowBackup,GoogleAppIndexingWarning">

<activity android:name=".dashboard.ui.MainActivity">
<intent-filter>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/AppConstants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foodapp.com.foodapp

object AppConstants {

val BASE_URL = "http://www.json-generator.com/api/json/get/"
}
24 changes: 24 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/ErrorType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package foodapp.com.foodapp

enum class ErrorType(val icon: Int, val title: Int, val subtitle: Int) {

CONNECTION_ERROR(
R.drawable.ic_svg_network_error,
R.string.inv_connection_error_title,
R.string.inv_connection_error_subtitle),

TIMEOUT_ERROR(
R.drawable.ic_svg_network_error,
R.string.inv_timeout_error_title,
R.string.inv_timeout_error_subtitle),

SERVER_ERROR(
R.drawable.ic_svg_network_error,
R.string.inv_generic_error_title,
R.string.inv_generic_error_subtitle),

CLIENT_ERROR(
R.drawable.ic_svg_network_error,
R.string.inv_generic_error_title,
R.string.inv_generic_error_subtitle)
}
30 changes: 30 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/FoodApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package foodapp.com.foodapp

import android.app.Activity
import android.app.Application
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import foodapp.com.foodapp.di.DaggerAppComponent
import timber.log.Timber
import javax.inject.Inject

class FoodApp : Application(), HasActivityInjector {

@Inject
lateinit var mDispatchingAndroidActivityInjector: DispatchingAndroidInjector<Activity>

override fun onCreate() {
super.onCreate()

DaggerAppComponent.builder().create(this).inject(this)

if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
}

override fun activityInjector(): AndroidInjector<Activity>? {
return mDispatchingAndroidActivityInjector
}
}
45 changes: 45 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/base/BasePresenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package foodapp.com.foodapp.base;

import foodapp.com.data.shared.utils.ErrorUtils
import foodapp.com.foodapp.ErrorType
import timber.log.Timber

open class BasePresenter<V : MvpView> : MvpPresenter<V> {

private var mMvpView: V? = null

val mvpView: V?
get() {
checkViewAttached()
return mMvpView
}

private val isViewAttached: Boolean
get() = mMvpView != null

override fun onAttach(mvpView: V) {
mMvpView = mvpView
}

override fun onDetach() {
mMvpView = null
}

private fun checkViewAttached() {
if (!isViewAttached) {
Timber.e("Please call Presenter.onAttach(MvpView) before requesting data to the Presenter")
}
}

fun handleError(throwable: Throwable) {
if (ErrorUtils.isConnectionError(throwable)) {
mvpView!!.onError(ErrorType.CONNECTION_ERROR)
} else if (ErrorUtils.isTimeOut(throwable)) {
mvpView!!.onError(ErrorType.TIMEOUT_ERROR)
} else if (ErrorUtils.isServerError(throwable)) {
mvpView!!.onError(ErrorType.SERVER_ERROR)
} else if (ErrorUtils.isClientError(throwable)) {
mvpView!!.onError(ErrorType.CLIENT_ERROR)
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/base/MvpPresenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foodapp.com.foodapp.base

interface MvpPresenter<V : MvpView> {
fun onAttach(mvpView: V)

fun onDetach()
}
13 changes: 13 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/base/MvpView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foodapp.com.foodapp.base;

import foodapp.com.foodapp.ErrorType

interface MvpView {

fun showLoading()

fun hideLoading()

fun onError(type: ErrorType)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import foodapp.com.foodapp.dashboard.ui.HeroImageFragment

class HeroImageViewsAdapter(private var imagesRes: ArrayList<Int>? = null, private var imagesUrls: ArrayList<String>? = null, manager: FragmentManager) : FragmentStatePagerAdapter(manager) {
class HeroImageViewsAdapter(private var imagesRes: ArrayList<Int>? = null,
private var imagesUrls: ArrayList<String>? = null,
manager: FragmentManager) : FragmentStatePagerAdapter(manager) {

override fun getCount(): Int {
imagesRes?.let {
Expand Down
58 changes: 14 additions & 44 deletions app/src/main/java/foodapp/com/foodapp/dashboard/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package foodapp.com.foodapp.dashboard.ui
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.view.ViewAnimationUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import foodapp.com.foodapp.R
import foodapp.com.foodapp.dashboard.adapter.HeroImageViewsAdapter
import foodapp.com.foodapp.foods.ui.FoodListActivity
Expand All @@ -33,47 +31,31 @@ class MainActivity : AppCompatActivity() {
manager = fragmentManager
)

heroImagesViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
// do nothing
}

override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
// do nothing
}

override fun onPageSelected(position: Int) {

}
})

tabLayout.setupWithViewPager(heroImagesViewPager, true)

startButton.setOnClickListener {
animateButton()

startAnimation()
}
}
fadeTextAndShowProgress()

private fun startAnimation() {
animateButtonWidth();

fadeOutTextAndShowProgressDialog()

nextAction()
handleNext()
}
}

private fun nextAction() {
private fun handleNext() {
Handler().postDelayed({
revealButton()
startRevealAnimation()

fadeOutProgressDialog()
progressBar.animate().alpha(0f).setDuration(200).start()

delayedStartNextActivity()
Handler().postDelayed(
{
startActivity(FoodListActivity.newInstance(this@MainActivity))
}, 100)
}, 2000)
}

private fun revealButton() {
private fun startRevealAnimation() {
startButton.elevation = 0f

reveal.visibility = View.VISIBLE
Expand All @@ -91,10 +73,6 @@ class MainActivity : AppCompatActivity() {
circularReveal.duration = 350
circularReveal.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
reset(animation)
}

private fun reset(animation: Animator) {
super.onAnimationEnd(animation)
reveal.visibility = View.INVISIBLE
buttonText.visibility = View.VISIBLE
Expand All @@ -109,19 +87,11 @@ class MainActivity : AppCompatActivity() {
circularReveal.start()
}

private fun fadeOutProgressDialog() {
progressBar.animate().alpha(0f).setDuration(200).start()
}

private fun delayedStartNextActivity() {
Handler().postDelayed({ startActivity(Intent(this@MainActivity, FoodListActivity::class.java)) }, 100)
}

private fun getFabWidth(): Int {
return resources.getDimension(R.dimen.fab_size).toInt()
}

private fun fadeOutTextAndShowProgressDialog() {
private fun fadeTextAndShowProgress() {
buttonText.animate().alpha(0f)
.setDuration(250)
.setListener(object : AnimatorListenerAdapter() {
Expand All @@ -141,7 +111,7 @@ class MainActivity : AppCompatActivity() {
progressBar.visibility = View.VISIBLE
}

private fun animateButtonWidth() {
private fun animateButton() {
val anim = ValueAnimator.ofInt(startButton.measuredWidth, getFabWidth())

anim.addUpdateListener { valueAnimator ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import foodapp.com.data.model.FoodItem
import foodapp.com.foodapp.R
import foodapp.com.foodapp.dashboard.adapter.HeroImageViewsAdapter
import foodapp.com.foodapp.model.FoodItem
import foodapp.com.foodapp.views.AnimationEndListener
import foodapp.com.foodapp.views.CircleTransform
import foodapp.com.foodapp.views.DepthPageTransformer
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/foodapp/com/foodapp/di/ActivityBindingModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foodapp.com.foodapp.di

import dagger.Module
import dagger.android.ContributesAndroidInjector
import foodapp.com.foodapp.foods.ui.FoodListActivity
import foodapp.com.foodapp.foods.ui.FoodListActivityModule

@Module
abstract class ActivityBindingModule {

@ContributesAndroidInjector(modules = [(FoodListActivityModule::class)])
abstract fun foodListActivity(): FoodListActivity
}
Loading

0 comments on commit dcce63d

Please sign in to comment.