Skip to content

Commit

Permalink
Merge pull request #75 from ininmm/refactor_to_koin
Browse files Browse the repository at this point in the history
Refactor HotBoardsFragment.kt to Kotlin and Koin
  • Loading branch information
ininmm authored Jan 24, 2021
2 parents fca0d91 + 60ad6ce commit 8a0d0d4
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 277 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ dependencies {
// Jsoup
implementation deps.jsoup

implementation deps.koin.android
implementation deps.koin.scope
implementation deps.koin.viewModel
implementation deps.koin.fragment

// Kotlin
implementation deps.kotlin.stdlib.jdk8
implementation deps.kotlin.coroutines.core
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/PttApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFact
import com.facebook.imagepipeline.core.ImagePipelineConfig
import com.facebook.imagepipeline.core.ImagePipelineFactory
import okhttp3.OkHttpClient
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import tw.y_studio.ptt.di.apiModules
import tw.y_studio.ptt.di.appModules
import tw.y_studio.ptt.di.dataSourceModules
import tw.y_studio.ptt.di.viewModelModules
import tw.y_studio.ptt.fresco.BitmapMemoryCacheSupplier
import tw.y_studio.ptt.fresco.OkHttpNetworkFetcher
import tw.y_studio.ptt.utils.OkHttpUtils
Expand All @@ -20,6 +26,17 @@ class PttApplication : MultiDexApplication() {

override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@PttApplication)
modules(
listOf(
appModules,
viewModelModules,
apiModules,
dataSourceModules
)
)
}
if (mOkHttpClient == null) {
try {
mOkHttpClient = OkHttpUtils().getCacheClient(this)
Expand Down
48 changes: 13 additions & 35 deletions app/src/main/java/tw/y_studio/ptt/adapter/HotBoardsListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageButton
import androidx.recyclerview.widget.RecyclerView
import tw.y_studio.ptt.R
import tw.y_studio.ptt.model.HotBoardsItem
import tw.y_studio.ptt.ptt.PttColor
import tw.y_studio.ptt.utils.StringUtils.notNullString

class HotBoardsListAdapter(
private val data: List<Map<String, Any>>
private val data: List<HotBoardsItem>,
private val onItemClickListener: OnItemClickListener,
) : RecyclerView.Adapter<RecyclerView.ViewHolder?>() {

private var mOnItemClickListener: OnItemClickListener? = null
private var mOnItemLongClickListener: OnItemLongClickListener? = null
private val editMode = false

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand All @@ -38,31 +36,23 @@ class HotBoardsListAdapter(
when (getItemViewType(position)) {
TYPE_NORMAL -> {
(holder as? ViewHolder)?.apply {
textViewTitle.text = notNullString(data[position]["title"])
textViewSubtitle.text = notNullString(data[position]["subtitle"])
textViewOnlinePeople.text = notNullString(data[position]["online"])
textViewTitle.text = data[position].title
textViewSubtitle.text = data[position].subtitle
textViewOnlinePeople.text = data[position].online
person.setColorFilter(
PttColor.ColorTrans(
notNullString(data[position]["onlineColor"])
data[position].onlineColor
)
)
itemView.setOnClickListener { mOnItemClickListener?.onItemClick(it, adapterPosition) }
itemView.setOnLongClickListener {
mOnItemLongClickListener?.onItemClick(it, adapterPosition)
true
}
itemView.setOnClickListener { onItemClickListener.onItemClick(data[position]) }
}
}
TYPE_EDIT -> {
(holder as? ViewHolderEdit)?.apply {
textViewTitle.text = notNullString(data[position]["title"])
textViewSubtitle.text = notNullString(data[position]["subtitle"])
textViewOnlinePeople.text = notNullString(data[position]["online"])
itemView.setOnClickListener { mOnItemClickListener?.onItemClick(it, adapterPosition) }
itemView.setOnLongClickListener {
mOnItemLongClickListener?.onItemClick(it, adapterPosition)
true
}
textViewTitle.text = data[position].title
textViewSubtitle.text = data[position].subtitle
textViewOnlinePeople.text = data[position].online
itemView.setOnClickListener { onItemClickListener.onItemClick(data[position]) }
}
}
}
Expand All @@ -72,14 +62,6 @@ class HotBoardsListAdapter(
return data.size
}

fun setOnItemClickListener(listener: OnItemClickListener?) {
mOnItemClickListener = listener
}

fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
mOnItemLongClickListener = listener
}

private inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val textViewTitle: TextView = v.findViewById(R.id.textView_hot_boards_title)
val textViewSubtitle: TextView = v.findViewById(R.id.textView_hot_boards_subtitle)
Expand All @@ -95,11 +77,7 @@ class HotBoardsListAdapter(

// define interface
interface OnItemClickListener {
fun onItemClick(view: View?, position: Int)
}

interface OnItemLongClickListener {
fun onItemClick(view: View?, position: Int)
fun onItemClick(item: HotBoardsItem)
}

companion object {
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/java/tw/y_studio/ptt/api/PopularBoardListAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package tw.y_studio.ptt.api

import okhttp3.Request
import org.json.JSONArray
import tw.y_studio.ptt.model.HotBoard
import java.util.*

class PopularBoardListAPI : BaseAPIHelper(), IBaseAPI {
private val _data: MutableList<Map<String, Any>> = mutableListOf()
private val _data: MutableList<HotBoard> = mutableListOf()

@Throws(Exception::class)
fun refresh(page: Int, count: Int): MutableList<Map<String, Any>> {
fun refresh(page: Int, count: Int): MutableList<HotBoard> {
_data.clear()
val request = Request.Builder()
.url("$hostUrl/api/Board/Popular?page=$page&count=$count")
Expand All @@ -28,16 +29,15 @@ class PopularBoardListAPI : BaseAPIHelper(), IBaseAPI {
while (!list.isNull(i)) {
val m3 = list.getJSONObject(i)
i++
val item: MutableMap<String, Any> = HashMap()
item["number"] = m3.getInt("sn")
item["title"] = m3.getString("name")
item["subtitle"] = m3.getString("title")
item["boardType"] = m3.getInt("boardType")
item["moderators"] = ""
item["class"] = ""
item["online"] = m3.getInt("onlineCount")
item["onlineColor"] = 7
_data.add(item)
val hotboard = HotBoard(
number = m3.getInt("sn"),
title = m3.getString("name"),
subtitle = m3.getString("title"),
boardType = m3.getInt("boardType"),
online = m3.getInt("onlineCount"),
onlineColor = "7"
)
_data.add(hotboard)
}
}
return _data
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/di/ApiModules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tw.y_studio.ptt.di

import org.koin.dsl.module
import tw.y_studio.ptt.api.PopularBoardListAPI
import tw.y_studio.ptt.api.PostAPI
import tw.y_studio.ptt.api.SearchBoardAPI

val apiModules = module {
factory { PopularBoardListAPI() }
factory { SearchBoardAPI() }
factory { PostAPI() }
}
10 changes: 10 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/di/AppModules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tw.y_studio.ptt.di

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import org.koin.core.qualifier.named
import org.koin.dsl.module

val appModules = module {
single<CoroutineDispatcher>(named("IO")) { Dispatchers.IO }
}
15 changes: 15 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/di/DataSourceModules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tw.y_studio.ptt.di

import org.koin.dsl.module
import tw.y_studio.ptt.source.remote.popular.IPopularRemoteDataSource
import tw.y_studio.ptt.source.remote.popular.PopularRemoteDataSourceImpl
import tw.y_studio.ptt.source.remote.post.IPostRemoteDataSource
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

val dataSourceModules = module {
factory<IPopularRemoteDataSource> { PopularRemoteDataSourceImpl(get()) }
factory<ISearchBoardRemoteDataSource> { SearchBoardRemoteDataSourceImpl(get()) }
factory<IPostRemoteDataSource> { PostRemoteDataSourceImpl(get()) }
}
9 changes: 0 additions & 9 deletions app/src/main/java/tw/y_studio/ptt/di/Injection.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package tw.y_studio.ptt.di

import tw.y_studio.ptt.api.PopularBoardListAPI
import tw.y_studio.ptt.api.PostAPI
import tw.y_studio.ptt.api.SearchBoardAPI
import tw.y_studio.ptt.source.remote.popular.IPopularRemoteDataSource
import tw.y_studio.ptt.source.remote.popular.PopularRemoteDataSourceImpl
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 popularBoardListAPI by lazy {
PopularBoardListAPI()
}
val searchBoardAPI by lazy { SearchBoardAPI() }
val postAPI by lazy { PostAPI() }
}

object RemoteDataSource {
val popularRemoteDataSource: IPopularRemoteDataSource by lazy {
PopularRemoteDataSourceImpl(API.popularBoardListAPI)
}
val searchBoardRemoteDataSource: ISearchBoardRemoteDataSource by lazy {
SearchBoardRemoteDataSourceImpl(API.searchBoardAPI)
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/di/ViewModelModules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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.hot_board.HotBoardsViewModel

val viewModelModules = module {
viewModel { HotBoardsViewModel(get(), get(named("IO"))) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import tw.y_studio.ptt.R;
import tw.y_studio.ptt.ui.BaseFragment;
import tw.y_studio.ptt.ui.hot_board.HotBoardsFragment;
import tw.y_studio.ptt.ui.setting.SettingFragment;

import java.util.HashMap;
Expand Down
Loading

0 comments on commit 8a0d0d4

Please sign in to comment.