Skip to content

Instantly share code, notes, and snippets.

View skywall's full-sized avatar

Lukáš Sztefek skywall

View GitHub Profile
@skywall
skywall / DatabaseModule.kt
Last active October 24, 2020 20:11
DatabaseModule
@Module
class DatabaseModule {
@Provides
fun database(application: Application) : Database {
return Database(application)
}
}
@skywall
skywall / DatabaseContract.kt
Created October 24, 2020 20:14
DatabaseContract
interface DatabaseContract {
fun database(): Database
}
@skywall
skywall / CoreComponent.kt
Created October 24, 2020 20:28
CoreComponent
@Singleton
@Component(
modules = [
CoreModule::class,
DatabaseModule::class,
NetworkModule::class
]
)
interface CoreComponent : DatabaseContract, NetworkContract {
@skywall
skywall / CoreComponentHolder.kt
Created October 24, 2020 20:53
CoreComponentHolder
object CoreComponentHolder {
private object Component : SingletonHolder<CoreComponent, Application>({ application ->
DaggerCoreComponent.factory().create(application)
})
fun getInstance(application: Application): CoreComponent {
return Component.getInstance(application)
}
}
@skywall
skywall / SingletonHolder.kt
Last active October 25, 2020 12:30
SingletonHolder
open class SingletonHolder<out T, in A>(private val constructor: (A) -> T) {
@Volatile private var instance: T? = null
fun getInstance(arg: A): T {
return when {
instance != null -> instance!!
else -> synchronized(this) {
if (instance == null) {
instance = constructor(arg)
@skywall
skywall / HomeModuleInjection.kt
Created October 24, 2020 21:15
HomeModuleInjection
@HomeScope
@Component(
modules = [
HomeModule::class
],
dependencies = [
CoreComponent::class
]
)
interface HomeComponent {
@skywall
skywall / BaseFragment.kt
Created October 24, 2020 21:16
BaseFragment in Home module
abstract class BaseFragment : Fragment() {
private object ModuleComponent : SingletonHolder<HomeComponent, Context>({ context ->
DaggerHomeComponent
.factory()
.create(CoreComponentHolder.getInstance(context.applicationContext as App))
})
val homeComponent: HomeComponent
get() = ModuleComponent.getInstance(requireContext())
@skywall
skywall / HomeFragment.kt
Last active October 25, 2020 12:20
HomeFragment
class HomeFragment : BaseFragment(), HomeView {
@Inject lateinit var homeAdapter: HomeAdapter // :home
@Inject lateinit var sharedPreferences: SharedPreferences // :core
@Inject lateinit var apiService: ApiService // :network
@Inject lateinit var dummyRepository: DummyRepository // :home - constructor provided
@Inject lateinit var homeDummyRepository: HomeDummyRepository // :home - component provided
override fun inject() {
homeComponent.plus(HomeFragmentModule(this)).inject(this)
@skywall
skywall / HomeFragmentInjection.kt
Created October 25, 2020 12:22
HomeFragmentInjection
@Subcomponent(
modules = [HomeFragmentModule::class]
)
interface HomeFragmentComponent {
fun inject(homeFragment: HomeFragment)
}
@Module
class HomeFragmentModule(private val homeFragment: HomeFragment) {
@skywall
skywall / TicketMachineParts.kt
Created November 7, 2020 20:55
TicketMachineParts
enum class TicketType(val coins: Int) {
ADULT(2),
CHILD(1),
}
class Printer(
private val display: Display,
) {
fun printTicket(selectedTicketType: TicketType) {
display.show("Ticket printed: ${selectedTicketType.name}")