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

Modules with dagger 2 #8

Open
wants to merge 5 commits into
base: dev
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
9 changes: 3 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation project(':domain')
implementation project(':base:core')
implementation project(':features:feature_onboarding')


// Google libs
Expand All @@ -58,9 +60,6 @@ dependencies {
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Glide
implementation 'com.github.bumptech.glide:glide:4.10.0'

// AdapterDelegates
implementation 'com.hannesdorfmann:adapterdelegates4:4.0.0'

Expand All @@ -80,7 +79,7 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
implementation "androidx.lifecycle:lifecycle-reactivestreams:2.1.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
kapt "androidx.lifecycle:lifecycle-compiler:2.1.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.1.0"

// Room
implementation "androidx.room:room-runtime:2.2.2"
Expand All @@ -92,9 +91,7 @@ dependencies {

// Dagger 2
implementation dagger2.core
// implementation dagger2.androidSupport
kapt dagger2.daggerCompiler
// kapt dagger2.androidCompiler
implementation dagger2.jsr330

}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<activity android:name=".features.newsDetail.NewsDetailsActivity" />

<activity
android:name=".features.onboarding.OnBoardingActivity"
android:name=".feature_onboarding.view.OnBoardingActivity"
android:theme="@style/AppThemeNoActionBar"
android:noHistory="true">
<intent-filter>
Expand Down
31 changes: 14 additions & 17 deletions app/src/main/java/com/dmity/androidacademy/NewsApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ package com.dmity.androidacademy
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.util.Log
import com.dmity.androidacademy.di.AppComponent
import com.dmity.androidacademy.di.ContextModule
import com.dmity.androidacademy.di.DaggerAppComponent
import com.dmity.androidacademy.core.di.AppWithFacade
import com.dmity.androidacademy.core.di.ProvidersFacade
import com.dmity.androidacademy.di.FacadeComponent
import io.reactivex.plugins.RxJavaPlugins
import timber.log.Timber


class NewsApp: Application() {

lateinit var appComponent: AppComponent
class NewsApp : Application(), AppWithFacade {

override fun onCreate() {
super.onCreate()
context = this


appComponent = DaggerAppComponent
.builder()
.contextModule(ContextModule(this))
.build()

(getFacade() as FacadeComponent).inject(this)

setGlobalRxJavaErrorHandler()
}

override fun getFacade(): ProvidersFacade {
return facadeComponent ?: FacadeComponent.init(this).also {
facadeComponent = it
}
}

@SuppressLint("LongLogTag")
private fun setGlobalRxJavaErrorHandler() {
RxJavaPlugins.setErrorHandler { e ->
Expand All @@ -40,15 +39,13 @@ class NewsApp: Application() {
Thread.currentThread().uncaughtExceptionHandler.uncaughtException(Thread.currentThread(), e)
}

Log.w("Undeliverable exception received", e.message)
Timber.w(e)
}
}

companion object {


fun getAppComponent() = (context as NewsApp).appComponent

private var facadeComponent: FacadeComponent? = null

@SuppressLint("StaticFieldLeak")
lateinit var context: Context
Expand Down
38 changes: 28 additions & 10 deletions app/src/main/java/com/dmity/androidacademy/di/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
package com.dmity.androidacademy.di

import com.dmity.androidacademy.features.onboarding.OnBoardingActivity
import com.dmity.androidacademy.features.onboarding.OnBoardingViewModel
import com.dmity.androidacademy.features.onboarding.di.OnBoardingModule
import android.app.Application
import android.content.Context
import com.dmity.androidacademy.core.di.AppProvider

import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@Singleton
@Component(
modules = [
ContextModule::class,
OnBoardingModule::class,
ViewModelModule::class]
modules = [AppModule::class]
)
interface AppComponent {
interface AppComponent : AppProvider {

companion object {

private var appComponent: AppProvider? = null

fun create(application: Application): AppProvider {
return appComponent ?: DaggerAppComponent
.builder()
.application(application.applicationContext)
.build().also {
appComponent = it
}
}
}

@Component.Builder
interface Builder {

fun inject(onBoardingViewModel: OnBoardingViewModel)
fun inject(onBoardingActivity: OnBoardingActivity)
@BindsInstance
fun application(context: Context): Builder

fun build(): AppComponent
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/com/dmity/androidacademy/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.dmity.androidacademy.di

import dagger.Module

@Module
abstract class AppModule {

@Module
companion object {

// Пока не используется
// private const val PREFS_NAME = "HABITS_SP"
//
// @JvmStatic
// @Provides
// @Singleton
// fun provideSharedPreferences(context: Context): SharedPreferences =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Закоменченный код лучше не добавлять в гит, это засоряет историю. Нужно либо выпилить сейчас, либо оставить провайдинг.

// context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
}
}
16 changes: 0 additions & 16 deletions app/src/main/java/com/dmity/androidacademy/di/ContextModule.kt

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/main/java/com/dmity/androidacademy/di/FacadeComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dmity.androidacademy.di

import android.app.Application
import com.dmity.androidacademy.NewsApp
import com.dmity.androidacademy.core.di.AppProvider
import com.dmity.androidacademy.core.di.ProvidersFacade
import dagger.Component

@Component(
dependencies = [AppProvider::class]
)
interface FacadeComponent : ProvidersFacade {

companion object {

fun init(application: Application): FacadeComponent =
DaggerFacadeComponent.builder()
.appProvider(AppComponent.create(application))
.build()
}

fun inject(app: NewsApp)
}
14 changes: 0 additions & 14 deletions app/src/main/java/com/dmity/androidacademy/di/ViewModelModule.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.dmity.androidacademy.features.about
import android.content.Context
import android.content.Intent
import com.dmity.androidacademy.R
import com.dmity.androidacademy.base.BaseActivity
import com.dmity.androidacademy.base.Layout
import com.dmity.androidacademy.core.BaseActivity
import com.dmity.androidacademy.core.Layout
import com.dmity.androidacademy.utils.ThirdPartyIntentUtils
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.content_activity_about.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.dmity.androidacademy.R
import com.dmity.androidacademy.base.BaseFragment
import com.dmity.androidacademy.core.BaseFragment
import com.dmity.androidacademy.core.extensions.setRetryListener
import com.dmity.androidacademy.core.extensions.showError
import com.dmity.androidacademy.core.extensions.showProgress
import com.dmity.androidacademy.features.newsList.model.NewsEntity
import com.dmity.androidacademy.utils.visible
import kotlinx.android.synthetic.main.fragment_news_details.*
import kotlinx.android.synthetic.main.view_error_stub.*

class NewsDetailsFragment : BaseFragment() {

Expand All @@ -26,14 +27,10 @@ class NewsDetailsFragment : BaseFragment() {
}

override fun initUx() {
btnRetry.setOnClickListener { requireActivity().onBackPressed() }
setRetryListener { requireActivity().onBackPressed() }
toolbar?.setNavigationOnClickListener { requireActivity().onBackPressed() }
}

fun showError(errorMessage: String, show: Boolean) {
errorStub.visible(show)
}

private fun initObservers() {
viewModel.showProgress.observe(this, Observer {
showProgress(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import android.content.Context
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.dmity.androidacademy.base.SubscriptionsHolder
import com.dmity.androidacademy.core.SubscriptionsHolder
import com.dmity.androidacademy.database.AppDatabase
import com.dmity.androidacademy.features.newsList.NewsRepo
import com.dmity.androidacademy.features.newsList.model.NewsEntity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

class NewsDetailsViewModel(application: Application) : AndroidViewModel(application), SubscriptionsHolder {
class NewsDetailsViewModel(application: Application) : AndroidViewModel(application),
SubscriptionsHolder {

override val disposables: CompositeDisposable = CompositeDisposable()
private val newsRepo: NewsRepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.lifecycle.ViewModelProviders
import com.dmity.androidacademy.R
import com.dmity.androidacademy.base.BaseActivity
import com.dmity.androidacademy.base.Layout
import com.dmity.androidacademy.core.BaseActivity
import com.dmity.androidacademy.core.Layout
import com.dmity.androidacademy.core.extensions.addOnClickListener
import com.dmity.androidacademy.features.newsDetail.NewsDetailsFragment
import com.dmity.androidacademy.utils.DisplayMetricsUtils.isPhone
import com.dmity.androidacademy.utils.DisplayMetricsUtils.isTablet
import com.dmity.androidacademy.utils.addOnClickListener
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar_main_tablet.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dmity.androidacademy.R
import com.dmity.androidacademy.base.BaseFragment
import com.dmity.androidacademy.core.BaseFragment
import com.dmity.androidacademy.core.extensions.addOnClickListener
import com.dmity.androidacademy.core.extensions.setRetryListener
import com.dmity.androidacademy.core.extensions.showError
import com.dmity.androidacademy.core.extensions.showProgress
import com.dmity.androidacademy.core.extensions.showSnack
import com.dmity.androidacademy.core.extensions.visible
import com.dmity.androidacademy.features.about.AboutActivity
import com.dmity.androidacademy.features.newsList.adapter.NewsAdapter
import com.dmity.androidacademy.features.newsList.model.DisplayableItem
import com.dmity.androidacademy.features.newsList.model.NewsEntity
import com.dmity.androidacademy.utils.DisplayMetricsUtils.isPhone
import com.dmity.androidacademy.utils.DisplayMetricsUtils.isPortrait
import com.dmity.androidacademy.utils.addOnClickListener
import com.dmity.androidacademy.utils.showSnack
import com.dmity.androidacademy.utils.visible
import kotlinx.android.synthetic.main.fragment_news_list.*
import kotlinx.android.synthetic.main.view_error_stub.*
import kotlinx.android.synthetic.main.view_progress_stub.*

class NewListFragment : BaseFragment() {

Expand Down Expand Up @@ -58,7 +59,7 @@ class NewListFragment : BaseFragment() {
viewModel.getNews(retry = true)
}

btnRetry.setOnClickListener(clickListener)
setRetryListener(clickListener)
fab?.setOnClickListener(clickListener)
aboutBtn?.setOnClickListener {
AboutActivity.display(requireContext())
Expand All @@ -71,20 +72,11 @@ class NewListFragment : BaseFragment() {
inflater.inflate(R.menu.menu_list, menu)
}

override fun showProgress(show: Boolean) = progress?.visible(show)

override fun onDetach() {
clickListener = null
super.onDetach()
}

private fun showError(errorMessage: String, show: Boolean) {
if(errorMessage.isNotBlank()) {
tvErrorMessage.text = errorMessage
}
errorStub.visible(show)
}

private fun showSnackBar(text: String, show: Boolean) {
if (show && text.isNotBlank()) {
rvNews.showSnack(text)
Expand Down
Loading