Skip to content

Commit

Permalink
feat: add itemAsync function
Browse files Browse the repository at this point in the history
  • Loading branch information
maxinghai committed Jul 28, 2022
1 parent ef5115c commit 9ad1bff
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 27 deletions.
12 changes: 5 additions & 7 deletions app/src/main/java/com/worktile/lib/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.worktile.common.default
import com.worktile.json.JsonDsl
import com.worktile.json.Parser
import com.worktile.json.ParserData
import com.worktile.ui.recyclerview.*
import com.worktile.ui.recyclerview.data.EdgeState
import com.worktile.ui.recyclerview.data.LoadingState
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import org.json.JSONObject
import java.util.*

class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -152,6 +145,11 @@ interface TestItemViewModel : ItemDefinition {

override fun bind(itemView: View) {
(itemView as? TextView)?.text = title.value
itemAsync {
main(itemView) {

}
}
}

override fun type(): Any = TestItemViewModel::class
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def configPublish(Project project, group, _artifact, ver) {
}

ext {
common_version = "0.0.9.220050821-SNAPSHOT"
widgets_version = "0.0.1.22040815-SNAPSHOT"
common_version = "0.0.9.22070923-SNAPSHOT"
widgets_version = "0.0.1.22071000-SNAPSHOT"
recyclerview_version = "0.9.7.21101500-SNAPSHOT"
recyclerview2_version = "1.0.0.2207050101-SNAPSHOT"
recyclerview2_version = "1.0.0.22072502-SNAPSHOT"
json_version = "0.2.1.22031623-SNAPSHOT"
kanban_version = "0.0.1.22012702-SNAPSHOT"
wt_common = "com.worktile:common:$common_version" as GStringImpl
Expand All @@ -121,4 +121,5 @@ ext {
recyclerview = 'androidx.recyclerview:recyclerview:1.2.1'
constraintlayout = "androidx.constraintlayout:constraintlayout:2.0.1"
viewpager2 = "androidx.viewpager2:viewpager2:1.0.0"
startup_runtime = "androidx.startup:startup-runtime:1.0.0"
}
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
implementation core_ktx
implementation acticity_ktx
implementation lifecycle_runtime_ktx
implementation startup_runtime
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
21 changes: 20 additions & 1 deletion common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.worktile.common"/>
xmlns:tools="http://schemas.android.com/tools"
package="com.worktile.common">

<application>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">

<meta-data
android:name="com.worktile.common.WorktileInitializer"
android:value="androidx.startup"/>

</provider>

</application>

</manifest>
6 changes: 5 additions & 1 deletion common/src/main/java/com/worktile/common/Worktile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log

@SuppressLint("StaticFieldLeak")
object Worktile {
Expand All @@ -14,7 +15,10 @@ object Worktile {
private var application: Application? = null

fun install(application: Application) {
if (this.application != null) return
if (this.application != null) {
Log.e("Worktile", "不要重复初始化Worktile")
return
}
this.application = application
val contextRecorder = object : ActivityLifecycleCallbacks() {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/com/worktile/common/WorktileInitializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.worktile.common

import android.app.Application
import android.content.Context
import androidx.startup.Initializer

class WorktileInitializer : Initializer<WorktileInitializer> {
override fun create(context: Context): WorktileInitializer {
Worktile.install(context as Application)
return this
}

override fun dependencies(): MutableList<Class<out Initializer<*>>> {
return mutableListOf()
}

}
6 changes: 2 additions & 4 deletions recyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

debugApi project(path: ':common')
debugImplementation project(path: ':widgets')
releaseApi wt_common
releaseImplementation wt_widgets
api project(path: ':common')
implementation project(path: ':widgets')
}

configPublish.call(project, 'com.worktile.ui', 'recyclerview', recyclerview_version)
6 changes: 2 additions & 4 deletions recyclerview2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ dependencies {
implementation lifecycle_runtime_ktx
api recyclerview

debugApi project(path: ':common')
debugImplementation project(path: ':widgets')
releaseApi wt_common
releaseImplementation wt_widgets
api project(path: ':common')
implementation project(path: ':widgets')

testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
31 changes: 26 additions & 5 deletions recyclerview2/src/main/java/com/worktile/ui/recyclerview/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.worktile.ui.recyclerview

import android.view.View
import android.view.ViewGroup
import androidx.core.view.forEachIndexed
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import com.worktile.common.Default
import java.lang.reflect.Field
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*
import java.util.concurrent.ConcurrentHashMap

typealias ViewCreator = (parent: ViewGroup) -> View

Expand Down Expand Up @@ -39,3 +39,24 @@ inline fun <reified T> contentArrayOf(vararg elements: T): Array<ContentItem<T>>
return arrayOf(*elements).map { ContentItem(it) }.toTypedArray()
}

internal val postponeAsyncMainBlocks = WeakHashMap<ItemDefinition, () -> Unit>()

fun ItemDefinition.itemAsync(async: suspend ItemAsyncScope.() -> Unit) {
CoroutineScope(Dispatchers.Default).launch {
async.invoke(ItemAsyncScope())
}
}

class ItemAsyncScope {
suspend fun ItemDefinition.main(itemView: View, block: () -> Unit) {
withContext(Dispatchers.Main) {
val keyInTag = itemView.getTag(R.id.item_definition_key)
if (keyInTag == key() || keyInTag == null) {
block()
} else {
postponeAsyncMainBlocks[this@main] = block
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class SimpleAdapter(
data[position].apply {
contentSparseArray.put(position, content())
holder.itemData = this
holder.itemView.setTag(R.id.item_definition_key, key())
postponeAsyncMainBlocks[this]?.invoke()
postponeAsyncMainBlocks.remove(this)
bind(holder.itemView)
}
}
Expand Down
1 change: 1 addition & 0 deletions recyclerview2/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<item name="wt_recycler_view_config" type="id" />
<item name="wt_recycler_view_adapter_data" type="id" />
<item name="wt_recycler_view_extensions_package" type="id" />
<item name="item_definition_key" type="id" />
</resources>
3 changes: 1 addition & 2 deletions widgets/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

debugApi project(path: ':common')
releaseApi wt_common
api project(path: ':common')
}

configPublish.call(project, 'com.worktile.ui', 'widgets', widgets_version)

0 comments on commit 9ad1bff

Please sign in to comment.