Skip to content

Commit

Permalink
Merge pull request #76 from ininmm/refactor_article_read_to_koin
Browse files Browse the repository at this point in the history
Refactor ArticleListFragment.kt and ArticleReadFragment.kt to dependency injection with Koin
  • Loading branch information
ininmm authored Jan 31, 2021
2 parents 8a0d0d4 + 7793d54 commit 55da0e6
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 273 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/di/AppModules.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package tw.y_studio.ptt.di

import android.content.Context
import android.content.SharedPreferences
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import org.koin.android.ext.koin.androidContext
import org.koin.core.qualifier.named
import org.koin.dsl.module

val appModules = module {
single<CoroutineDispatcher>(named("IO")) { Dispatchers.IO }

single<SharedPreferences> {
androidContext().getSharedPreferences("MainSetting", Context.MODE_PRIVATE)
}
}
6 changes: 0 additions & 6 deletions app/src/main/java/tw/y_studio/ptt/di/Injection.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package tw.y_studio.ptt.di

import tw.y_studio.ptt.api.PostAPI
import tw.y_studio.ptt.api.SearchBoardAPI
import tw.y_studio.ptt.source.remote.post.PostRemoteDataSourceImpl
import tw.y_studio.ptt.source.remote.search.ISearchBoardRemoteDataSource
import tw.y_studio.ptt.source.remote.search.SearchBoardRemoteDataSourceImpl

object Injection {
object API {
val searchBoardAPI by lazy { SearchBoardAPI() }
val postAPI by lazy { PostAPI() }
}

object RemoteDataSource {
val searchBoardRemoteDataSource: ISearchBoardRemoteDataSource by lazy {
SearchBoardRemoteDataSourceImpl(API.searchBoardAPI)
}
val postRemoteDataSourceImpl by lazy {
PostRemoteDataSourceImpl(API.postAPI)
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/di/ViewModelModules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package tw.y_studio.ptt.di
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.qualifier.named
import org.koin.dsl.module
import tw.y_studio.ptt.ui.article.list.ArticleListViewModel
import tw.y_studio.ptt.ui.article.read.ArticleReadViewModel
import tw.y_studio.ptt.ui.hot_board.HotBoardsViewModel

val viewModelModules = module {
viewModel { HotBoardsViewModel(get(), get(named("IO"))) }
viewModel { ArticleListViewModel(get(), get(named("IO"))) }
viewModel { ArticleReadViewModel(get(), get(), get(named("IO"))) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.koin.androidx.viewmodel.ext.android.viewModel
import tw.y_studio.ptt.R
import tw.y_studio.ptt.databinding.ArticleListFragmentLayoutBinding
import tw.y_studio.ptt.di.Injection
import tw.y_studio.ptt.fragment.ArticleListSearchFragment
import tw.y_studio.ptt.fragment.PostArticleFragment
import tw.y_studio.ptt.model.PartialPost
Expand All @@ -31,13 +29,14 @@ class ArticleListFragment : BaseFragment() {
private var boardSubName = ""
private val mClickFix = ClickFix()

private lateinit var articleListViewModel: ArticleListViewModel
private val articleListViewModel: ArticleListViewModel by viewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bundle = arguments // 取得Bundle
boardName = bundle?.getString("title", getString(R.string.board_list_title_empty)) ?: ""
boardSubName = bundle?.getString("subtitle", getString(R.string.board_list_subtitle_empty)) ?: ""
boardSubName = bundle?.getString("subtitle", getString(R.string.board_list_subtitle_empty))
?: ""
}

override fun onCreateView(
Expand All @@ -51,20 +50,6 @@ class ArticleListFragment : BaseFragment() {
}

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

articleListViewModel = ViewModelProvider(
this,
object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ArticleListViewModel(
Injection.RemoteDataSource.postRemoteDataSourceImpl,
boardName
) as T
}
}
).get(ArticleListViewModel::class.java)

binding.apply {
articleListFragmentTextViewTitle.text = boardName
articleListFragmentRecyclerView.apply {
Expand Down Expand Up @@ -100,7 +85,7 @@ class ArticleListFragment : BaseFragment() {
val lastVisibleItem = layoutManager.findLastVisibleItemPosition()
val totalItemCount = layoutManager.itemCount
if (lastVisibleItem >= totalItemCount - 30) {
articleListViewModel.loadNextData()
articleListViewModel.loadNextData(boardName)
}
}
})
Expand All @@ -112,13 +97,13 @@ class ArticleListFragment : BaseFragment() {
android.R.color.holo_green_light,
android.R.color.holo_orange_light
)
setOnRefreshListener { articleListViewModel.loadData() }
setOnRefreshListener { articleListViewModel.loadData(boardName) }
}
articleListFragmentBottomNavigation.setOnNavigationItemSelectedListener(
BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.article_list_navigation_item_refresh -> {
articleListViewModel.loadData()
articleListViewModel.loadData(boardName)
return@OnNavigationItemSelectedListener false
}
R.id.article_list_navigation_item_post -> {
Expand Down Expand Up @@ -158,7 +143,7 @@ class ArticleListFragment : BaseFragment() {
}

override fun onAnimOver() {
articleListViewModel.loadData()
articleListViewModel.loadData(boardName)
binding.articleListFragmentRecyclerView.adapter?.notifyDataSetChanged()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import tw.y_studio.ptt.model.PartialPost
Expand All @@ -15,7 +15,7 @@ import java.util.concurrent.atomic.AtomicInteger

class ArticleListViewModel(
private val postRemoteDataSource: IPostRemoteDataSource,
private val boardName: String // TODO: 2020/11/21 need refactor
private val ioDispatcher: CoroutineDispatcher
) : ViewModel() {
val data: MutableList<PartialPost> = ArrayList()
private val page = AtomicInteger(1)
Expand All @@ -27,31 +27,31 @@ class ArticleListViewModel(

fun getErrorLiveData(): LiveData<Throwable> = errorLiveData

fun loadData() {
fun loadData(boardName: String) {
viewModelScope.launch {
if (loadingState.value == true) {
return@launch
}
data.clear()
page.set(1)
loadingState.value = true
getDataFromApi()
getDataFromApi(boardName)
loadingState.value = false
}
}

fun loadNextData() {
fun loadNextData(boardName: String) {
viewModelScope.launch {
if (loadingState.value == true) {
return@launch
}
loadingState.value = true
getDataFromApi()
getDataFromApi(boardName)
loadingState.value = false
}
}

private suspend fun getDataFromApi() = withContext(Dispatchers.Default) {
private suspend fun getDataFromApi(boardName: String) = withContext(ioDispatcher) {
try {
val temp = mutableListOf<PartialPost>()
for (i in 0..2) {
Expand Down
Loading

0 comments on commit 55da0e6

Please sign in to comment.