diff --git a/.idea/misc.xml b/.idea/misc.xml index 3378229..271a9f2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,6 +5,18 @@ + + + diff --git a/app/build.gradle b/app/build.gradle index 3234cdb..c9b4893 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,6 +30,9 @@ android { kotlinOptions { jvmTarget = '1.8' } + viewBinding { + enabled = true + } } dependencies { @@ -41,4 +44,10 @@ dependencies { testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + + implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0" + + implementation project(":source") + implementation project(":observer") + implementation project(":paging") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 99d27fd..ea11580 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,19 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/Theme.RetrofitSource" /> + android:theme="@style/Theme.RetrofitSource"> + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/me/twc/retrofitsource/MainActivity.kt b/app/src/main/java/me/twc/retrofitsource/MainActivity.kt new file mode 100644 index 0000000..9f40567 --- /dev/null +++ b/app/src/main/java/me/twc/retrofitsource/MainActivity.kt @@ -0,0 +1,22 @@ +package me.twc.retrofitsource + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import me.twc.retrofitsource.databinding.ActMainBinding + +/** + * @author 唐万超 + * @date 2022/09/23 + */ +class MainActivity : AppCompatActivity() { + + private val mBinding by lazy { ActMainBinding.inflate(layoutInflater) } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(mBinding.root) + mBinding.tvObserverView2.setOnClickListener { + SourceObserverView2Activity.show(this) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/me/twc/retrofitsource/SourceObserverView2Activity.kt b/app/src/main/java/me/twc/retrofitsource/SourceObserverView2Activity.kt new file mode 100644 index 0000000..8c05fa4 --- /dev/null +++ b/app/src/main/java/me/twc/retrofitsource/SourceObserverView2Activity.kt @@ -0,0 +1,67 @@ +package me.twc.retrofitsource + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import me.twc.retrofitsource.databinding.ActSourceObserverView2Binding +import me.twc.source.ErrorSource +import me.twc.source.LoadingSource +import me.twc.source.Source +import me.twc.source.observer.utils.loadingObserver + +/** + * @author 唐万超 + * @date 2022/09/23 + */ +class SourceObserverView2Activity : AppCompatActivity() { + + companion object{ + fun show(context: Context){ + val intent = Intent(context,SourceObserverView2Activity::class.java) + context.startActivity(intent) + } + } + + private val mBinding by lazy { ActSourceObserverView2Binding.inflate(layoutInflater) } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(mBinding.root) + mBinding.root.setOnClickListener { + Toast.makeText(this, "content click", Toast.LENGTH_SHORT).show() + } + mBinding.obserview.mReloadFun = fun(){ + test() + } + test.loadingObserver(this,mBinding.obserview){ + Toast.makeText(this, it, Toast.LENGTH_SHORT).show() + } + lifecycleScope.launchWhenResumed { test() } + } + + + private val _test: MutableLiveData> by lazy { + return@lazy MutableLiveData>() + } + + val test: LiveData> = _test + + private var testJob: Job? = null + + fun test() { + testJob?.cancel() + testJob = lifecycleScope.launch { + _test.value = LoadingSource + delay(1000) + _test.value = ErrorSource("测试异常") + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/act_main.xml b/app/src/main/res/layout/act_main.xml new file mode 100644 index 0000000..e2a956a --- /dev/null +++ b/app/src/main/res/layout/act_main.xml @@ -0,0 +1,14 @@ + + + +