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
11 changes: 8 additions & 3 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 @@ -34,9 +35,13 @@ android {
dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

implementation 'com.google.dagger:dagger:2.39.1'
kapt 'com.google.dagger:dagger-compiler:2.39.1'

testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<application
android:allowBackup="true"
android:name=".example2.ExampleApp"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand All @@ -18,6 +19,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".example2.presentation.MainActivity2" />
</application>

</manifest>
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 {
DaggerMyComponent.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,60 @@
package com.example.dependencyinjectionstart.example1

import dagger.Module
import dagger.Provides


// Если у нас нет доступа к классу (например, из библиотеки), то с помощью модуля мы можем создать
// экземпляр класса
@Module
class ComputerModule {

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

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

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

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

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

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

@Provides
fun provideComputerTower(
storage: Storage,
memory: Memory,
processor: Processor
): 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
@@ -1,6 +1,6 @@
package com.example.dependencyinjectionstart.example1

class ComputerTower(
class ComputerTower (
val storage: Storage,
val memory: Memory,
val processor: Processor
Expand Down
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 MyComponent {

fun inject(activity: Activity)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.dependencyinjectionstart.example2

import android.app.Application
import com.example.dependencyinjectionstart.example2.di.DaggerApplicationComponent

class ExampleApp: Application() {
val component by lazy {
DaggerApplicationComponent.factory()
.create(this, System.currentTimeMillis())
}
}

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,22 @@
package com.example.dependencyinjectionstart.example2.data.database

import android.content.Context
import android.util.Log
import com.example.dependencyinjectionstart.R
import javax.inject.Inject

class ExampleDatabase @Inject constructor(
private val context: Context,
private val timeMillis: Long
) {

fun method() {
Log.d(
LOG_TAG,
"ExampleDatabase ${context.getString(R.string.app_name)} $timeMillis $this")
}

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,23 @@
package com.example.dependencyinjectionstart.example2.data.network

import android.content.Context
import android.util.Log
import com.example.dependencyinjectionstart.R
import javax.inject.Inject

class ExampleApiService @Inject constructor(
private val context: Context,
private val timeMillis: Long
) {

fun method() {
Log.d(
LOG_TAG,
"ExampleApiService ${context.getString(R.string.app_name)} $timeMillis $this"
)
}

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()
}
}
Loading