Skip to content

Instantly share code, notes, and snippets.

View gilgoldzweig's full-sized avatar

Gil Goldzweig gilgoldzweig

View GitHub Profile
@gilgoldzweig
gilgoldzweig / GenericRecyclerAdapter.kt
Last active January 9, 2019 10:07
GenericRecyclerAdapter blog
abstract class GenericRecyclerAdapter<E: Any>(context: Context,
genericList: MutableList<E> = ArrayList(),
@LayoutRes private val layoutRes: Int) :
GenericRecyclerViewAdapterExtensions<E, GenericRecyclerAdapter<E>.GenericViewHolder>(genericList) {
private var layoutInflater: LayoutInflater = LayoutInflater.from(context)
//region setup and binding
inner class GenericViewHolder(private val v: View) : RecyclerView.ViewHolder(v) {
/**
* A base presenter class that include some functionality and helping
*/
abstract class BasePresenter<V : BaseContract.View> :
CoroutineScope, LifecycleObserver, BaseContract.Presenter<V> {
open var job: Job = Job()
var lifecycle: Lifecycle? = null
interface ExampleContract : BaseContract {
interface View : BaseContract.View {
/**
* example of successful response
*/
fun onProfileNameReceived(name: String)
/**
* example of a failure response
interface BaseContract {
@UiThread
interface View
interface Presenter<V : View> {
fun attach(view: V)
fun detach()
interface ExampleContract : BaseContract {
interface View : BaseContract.View {
fun onProfileNameReceived(name: String)
fun onProfileNameRequestFailed(exception: Exception)
}
interface Presenter : BaseContract.Presenter<View> {
class ExampleActivity : AppCompatActivity(), ExampleContract.View {
lateinit var presenter: ExampleContract.Presenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Inject the presenter using Dependecy injection
@gilgoldzweig
gilgoldzweig / ExamplePresenterTest.kt
Created February 3, 2019 13:02
The first part of the ExamplePresenterTest
@RunWith(MockitoJUnitRunner::class)
class ExamplePresenterTest {
@Mock
private lateinit var view: ExampleContract.View
@Spy
private var presenter: ExamplePresenter = ExamplePresenter()
@Before
class ExamplePresenterTest {
//Setup code...
@Test
fun `test_fetchProfileName_calles_onProfileNameReceived_on_positive_response`() {
Mockito.`when`(presenter.fetchProfileNameFromRepository()))
.thenReturn("Gil Goldzweig")
open class ExamplePresenter : ExampleContract.Presenter {
var view: ExampleContract.View? = null
override fun attach(view: ExampleContract.View) {
this.view = view
}
override fun fetchProfileName() {
class ExamplePresenterTest {
//Setup code...
@Test
fun `test_fetchProfileName_calles_onProfileRequestFailed_on_exception`() {
val fakeException = Mockito.mock(IOException::class.java)
Mockito.`when`(presenter.fetchProfileNameFromRepository()))