Skip to content

Commit

Permalink
Add ViewModel into presenter to reduce method count a litte bit. ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamwee committed Nov 9, 2017
1 parent a2f9dee commit 19c4145
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.iamwee.kotlinmvpstructure.base

/**
* Created by zeon on 9/11/2017 AD.
*/
open class BaseViewModel {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package com.github.iamwee.kotlinmvpstructure.base.presenter


import kotlin.properties.Delegates

/**
* Created by zeon on 8/24/2017 AD.
*/

open class BasePresenter<out REPOSITORY : MvpRepository, out VIEW : IBaseView>(val repository: REPOSITORY, val view: VIEW)
@Suppress("LeakingThis")
abstract class BasePresenter<out View : IBaseView<ViewModel>, ViewModel : Any>(val view: View) {

var viewModel by Delegates.observable(onCreateViewModel(), { _, _, newViewModel ->
view.onViewModelChanged(newViewModel)
})

abstract fun onCreateViewModel(): ViewModel
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.github.iamwee.kotlinmvpstructure.base.presenter


/**
* Created by zeon on 8/24/2017 AD.
*/

interface IBaseView
interface IBaseView<in ViewModel: Any> {

fun onViewModelChanged(viewModel: ViewModel)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class HttpProvider {
val instance by lazy { HttpProvider() }
}

val githubService: GithubService by lazy {
retrofit().create(GithubService::class.java)
}
val githubService: GithubService by lazy { retrofit().create(GithubService::class.java) }

private fun retrofit(): Retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.BASE_SERVICE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,44 @@ import com.github.iamwee.kotlinmvpstructure.base.presenter.MvpFragment
import com.github.iamwee.kotlinmvpstructure.extensions.showToast
import com.github.iamwee.kotlinmvpstructure.http.entity.RepositoryEntity
import com.github.iamwee.kotlinmvpstructure.view.main.adapter.MainAdapter
import com.github.iamwee.kotlinmvpstructure.view.main.presenter.IMainPresenter
import com.github.iamwee.kotlinmvpstructure.view.main.presenter.IMainView
import com.github.iamwee.kotlinmvpstructure.view.main.presenter.MainPresenter
import com.github.iamwee.kotlinmvpstructure.view.main.presenter.MainRepository
import com.github.iamwee.kotlinmvpstructure.view.main.presenter.*
import kotlinx.android.synthetic.main.fragment_main.*

class MainFragment : MvpFragment<IMainPresenter>(), IMainView {
class MainFragment : MvpFragment<IMainPresenter>(), IMainView<MainViewModel> {

companion object {
fun newInstance(): MainFragment = MainFragment()
}

private lateinit var mainAdapter: MainAdapter
private val mainAdapter = MainAdapter()
override var layoutId: Int = R.layout.fragment_main

override fun onCreatePresenter(): IMainPresenter = MainPresenter(MainRepository(), this)
override fun onCreatePresenter(): IMainPresenter = MainPresenter(this)

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

mainAdapter = MainAdapter().apply {
mainAdapter.apply {
listener = { position -> showToast("Action from position $position") }
}

swipeRefreshLayout.setOnRefreshListener { presenter.getRepositoryFromGithub() }

with(recyclerView) {
layoutManager = LinearLayoutManager(activity)
addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
adapter = mainAdapter
}
}

override fun onRepoSuccess(entities: List<RepositoryEntity>) = mainAdapter.map(entities)
override fun onViewModelChanged(viewModel: MainViewModel) {
with(viewModel) {
swipeRefreshLayout.isRefreshing = loading
mainAdapter.map(repositoryEntities)

error?.let {
showToast(viewModel.error!!)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,26 @@ class MainAdapter : BaseAdapter() {

lateinit var listener: (Int) -> Unit

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder = when (viewType) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
: BaseViewHolder = when (viewType) {
TYPE_REPO -> ItemViewHolder(parent)
else -> throw ViewTypeNotFoundException(viewType)
}

override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
if (holder is ItemViewHolder) {
val item = items[position] as RepoItem
val item = items[position] as RepositoryItem
with(holder.itemView) {
tvTitle.text = item.content
setOnClickListener {
listener(position)
}
setOnClickListener { listener(position) }
}
}
}

fun map(response: List<RepositoryEntity>) {
val baseItems = ArrayList<BaseItem>()
for ((_, name, fullName, _, url) in response) {
baseItems.add(RepoItem("""
baseItems.add(RepositoryItem("""
|$name
|$fullName
|$url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import com.github.iamwee.kotlinmvpstructure.base.BaseItem
* Created by zeon on 9/15/2017 AD.
*/

class RepoItem(val content: String) : BaseItem(MainAdapter.TYPE_REPO)
class RepositoryItem(val content: String) : BaseItem(MainAdapter.TYPE_REPO)
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.github.iamwee.kotlinmvpstructure.view.main.presenter

import com.github.iamwee.kotlinmvpstructure.base.presenter.IBaseView
import com.github.iamwee.kotlinmvpstructure.http.entity.RepositoryEntity

/**
* Created by zeon on 8/24/2017 AD.
*/

interface IMainView : IBaseView {

fun onRepoSuccess(entities: List<RepositoryEntity>)

}
interface IMainView<in ViewModel: Any> : IBaseView<ViewModel>
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ import com.github.iamwee.kotlinmvpstructure.base.presenter.BasePresenter
* Created by zeon on 8/24/2017 AD.
*/

class MainPresenter(repository: MainRepository, view: IMainView)
: BasePresenter<MainRepository, IMainView>(repository, view), IMainPresenter {
class MainPresenter(view: IMainView<MainViewModel>) :
BasePresenter<IMainView<MainViewModel>, MainViewModel>(view), IMainPresenter {

private val repository = MainRepository()

override fun onCreateViewModel(): MainViewModel = MainViewModel()

init {
getRepositoryFromGithub()
}

override fun getRepositoryFromGithub() {
repository.getRepositories({ data -> view.onRepoSuccess(data) }, {})
repository.getRepositories({ data ->
viewModel = viewModel.copy(loading = false, repositoryEntities = data, error = null)
}, { error ->
viewModel = viewModel.copy(loading = false, repositoryEntities = listOf(), error = error)
})
viewModel = viewModel.copy(loading = true, repositoryEntities = listOf(), error = null)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.iamwee.kotlinmvpstructure.view.main.presenter


import com.github.iamwee.kotlinmvpstructure.http.entity.RepositoryEntity

/**
* Created by zeon on 9/11/2017 AD.
*/

data class MainViewModel(var loading: Boolean = false,
var repositoryEntities: List<RepositoryEntity> = listOf(),
var error: String? = null
)
13 changes: 10 additions & 3 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="@+id/recyclerView"
android:layout_height="match_parent"/>
android:id="@+id/swipeRefreshLayout"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recyclerView"
android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

0 comments on commit 19c4145

Please sign in to comment.