Skip to content
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
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -33,6 +34,8 @@ android {

dependencies {

implementation 'com.google.dagger:dagger:2.35.1'
kapt 'com.google.dagger:dagger-compiler:2.35.1'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.example.dependencyinjectionstart.example1

import javax.inject.Inject

class Activity {

val monitor = Monitor()
val keyboard = Keyboard()
val mouse = Mouse()
val computerTower = ComputerTower(
Storage(),
Memory(),
Processor()
)
val computer = Computer(monitor, computerTower, keyboard, mouse)
}
@Inject
lateinit var computer: Computer

init {
DaggerNewComponent.create().inject(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.dependencyinjectionstart.example1

class Component {

private fun getComputer(): Computer {
val monitor = Monitor()
val keyboard = Keyboard()
val mouse = Mouse()
val computerTower = ComputerTower(
Storage(),
Memory(),
Processor()
)
return Computer(monitor, computerTower, keyboard, mouse)
}

fun inject(activity: Activity) {
// activity.keyboard = Keyboard()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.dependencyinjectionstart.example1

class Computer(
class Computer (
val monitor: Monitor,
val computerTower: ComputerTower,
val keyboard: Keyboard,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.dependencyinjectionstart.example1

import dagger.Module
import dagger.Provides

@Module
class ComputerModule {

@Provides
fun provideMonitor(): Monitor {
return Monitor()
}

@Provides
fun provideKeyboard(): Keyboard {
return Keyboard()
}

@Provides
fun provideMouse(): Mouse {
return Mouse()
}

@Provides
fun provideStorage(): Storage {
return Storage()
}

@Provides
fun provideMemory(): Memory {
return Memory()
}

@Provides
fun provideProcessor(): Processor {
return Processor()
}

@Provides
fun provideComputerTower(
memory: Memory,
processor: Processor,
storage: Storage
): ComputerTower {
return ComputerTower(storage, memory, processor)
}

@Provides
fun provideComputer(
monitor: Monitor,
computerTower: ComputerTower,
keyboard: Keyboard,
mouse: Mouse,
): Computer {
return Computer(monitor, computerTower, keyboard, mouse)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.dependencyinjectionstart.example1

import dagger.Component

@Component(modules = [ComputerModule::class])
interface NewComponent {

fun inject(activity: Activity)
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.dependencyinjectionstart.example2.data.database

import android.util.Log
import javax.inject.Inject

class ExampleDatabase @Inject constructor(){

fun method() {
Log.d(LOG_TAG, "ExampleDatabase")
}

companion object {

private const val LOG_TAG = "EXAMPLE_TEST"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.dependencyinjectionstart.example2.data.datasource

interface ExampleLocalDataSource {

fun method()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.dependencyinjectionstart.example2.data.datasource

import com.example.dependencyinjectionstart.example2.data.database.ExampleDatabase
import javax.inject.Inject

class ExampleLocalDataSourceImpl @Inject constructor(
private val database: ExampleDatabase
) : ExampleLocalDataSource {

override fun method() {
database.method()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.dependencyinjectionstart.example2.data.datasource

interface ExampleRemoteDataSource {

fun method()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.dependencyinjectionstart.example2.data.datasource

import com.example.dependencyinjectionstart.example2.data.network.ExampleApiService
import javax.inject.Inject

class ExampleRemoteDataSourceImpl @Inject constructor(
private val apiService: ExampleApiService
) : ExampleRemoteDataSource {

override fun method() {
apiService.method()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.dependencyinjectionstart.example2.data.mapper

import javax.inject.Inject

class ExampleMapper @Inject constructor(){

fun map() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.dependencyinjectionstart.example2.data.network

import android.util.Log
import javax.inject.Inject

class ExampleApiService @Inject constructor(){

fun method() {
Log.d(LOG_TAG, "ExampleApiService")
}

companion object {

private const val LOG_TAG = "EXAMPLE_TEST"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.dependencyinjectionstart.example2.data.repository

import com.example.dependencyinjectionstart.example2.data.datasource.ExampleLocalDataSource
import com.example.dependencyinjectionstart.example2.data.datasource.ExampleRemoteDataSource
import com.example.dependencyinjectionstart.example2.data.mapper.ExampleMapper
import com.example.dependencyinjectionstart.example2.domain.ExampleRepository
import javax.inject.Inject

class ExampleRepositoryImpl @Inject constructor(
private val localDataSource: ExampleLocalDataSource,
private val remoteDataSource: ExampleRemoteDataSource,
private val mapper: ExampleMapper
) : ExampleRepository {

override fun method() {
mapper.map()
localDataSource.method()
remoteDataSource.method()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.dependencyinjectionstart.example2.di

import com.example.dependencyinjectionstart.example2.presentation.MainActivity
import dagger.Component

@Component(modules = [DataModule::class, DomainModule::class])
interface AppComponent {

fun inject(mainActivity: MainActivity)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.dependencyinjectionstart.example2.di

import com.example.dependencyinjectionstart.example2.data.datasource.ExampleLocalDataSource
import com.example.dependencyinjectionstart.example2.data.datasource.ExampleLocalDataSourceImpl
import com.example.dependencyinjectionstart.example2.data.datasource.ExampleRemoteDataSource
import com.example.dependencyinjectionstart.example2.data.datasource.ExampleRemoteDataSourceImpl
import dagger.Module
import dagger.Provides

@Module
class DataModule {

@Provides
fun provideLocalDataSource(impl : ExampleLocalDataSourceImpl) : ExampleLocalDataSource {
return impl
}

@Provides
fun provideRemoteDataSource(impl : ExampleRemoteDataSourceImpl) : ExampleRemoteDataSource {
return impl
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.dependencyinjectionstart.example2.di

import com.example.dependencyinjectionstart.example2.data.repository.ExampleRepositoryImpl
import com.example.dependencyinjectionstart.example2.domain.ExampleRepository
import dagger.Module
import dagger.Provides

@Module
class DomainModule {

@Provides
fun provideExampleRepository(impl : ExampleRepositoryImpl) : ExampleRepository {
return impl
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.dependencyinjectionstart.example2.domain

class ExampleUseCase(
import javax.inject.Inject

class ExampleUseCase @Inject constructor(
private val repository: ExampleRepository
) {

operator fun invoke() {

repository.method()
}
}
Loading