Created
May 25, 2018 03:09
-
-
Save guuilp/7084e32ffb0c9ff4712d247515f494aa to your computer and use it in GitHub Desktop.
Dagger 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(modules = [RepositoryModule::class]) | |
interface LanchoneteComponent { | |
fun inject(app: MainActivity) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity(), MainContract.View { | |
@Inject override lateinit var presenter: MainContract.Presenter | |
@Inject lateinit var repository: LanchoneteRepository | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener) | |
DaggerLanchoneteComponent.builder() | |
.repositoryModule(RepositoryModule(this)) | |
.build() | |
.inject(this) | |
presenter.start() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainPresenter @Inject constructor(val lanchoneteRepository: LanchoneteRepository, | |
val mainView: MainContract.View) : MainContract.Presenter{ | |
override fun start() { | |
loadIngredientesELanches() | |
} | |
fun loadIngredientesELanches(){ | |
lanchoneteRepository.listaDeIngredientes(object: LanchoneteDataSource.ListaDeIngredientesCallback{ | |
override fun onListaDeIngredientesSuccess(listaDeIngredientes: List<Ingrediente>) { | |
loadLanches(listaDeIngredientes) | |
} | |
override fun onListaDeIngredientesError() { | |
mainView.showAPIIngredienteError() | |
} | |
}) | |
} | |
fun loadLanches(listaDeIngredientes: List<Ingrediente>){ | |
lanchoneteRepository.listaDeLanches(object: LanchoneteDataSource.ListaDeLanchesCallback{ | |
override fun onListaDeLanchesSuccess(listaDeLanches: List<Lanche>) { | |
val listaDeLanchesComPreco = lanchoneteRepository.atualizaListaComPrecoEIngrediente(listaDeLanches, listaDeIngredientes) | |
mainView.getLanchesEIngredientes(listaDeIngredientes, listaDeLanchesComPreco) | |
} | |
override fun onListaDeLanchesError() { | |
mainView.showAPILancheError() | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
class RepositoryModule(private var view: MainContract.View) { | |
@Provides | |
fun providesLanchoneteRemoteDataSource(): LanchoneteDataSource{ | |
return LanchoneteRemoteDataSource | |
} | |
@Provides | |
fun providesLanchoneteRepository(dataSource: LanchoneteDataSource): LanchoneteRepository{ | |
return LanchoneteRepository(dataSource) | |
} | |
@Provides | |
fun providesMainView(): MainContract.View{ | |
return view | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment