Skip to content

Commit

Permalink
添加 SourceObserverView2.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
tangwanchao committed Sep 23, 2022
1 parent ff14325 commit 4533e72
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
viewBinding {
enabled = true
}
}

dependencies {
Expand All @@ -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")
}
15 changes: 14 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SourceObserverView2Activity"
android:screenOrientation="portrait" />
</application>

</manifest>
22 changes: 22 additions & 0 deletions app/src/main/java/me/twc/retrofitsource/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Source<String>> by lazy {
return@lazy MutableLiveData<Source<String>>()
}

val test: LiveData<Source<String>> = _test

private var testJob: Job? = null

fun test() {
testJob?.cancel()
testJob = lifecycleScope.launch {
_test.value = LoadingSource
delay(1000)
_test.value = ErrorSource("测试异常")
}
}
}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/act_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/tv_observer_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="observer_view"
tools:ignore="HardcodedText" />
</LinearLayout>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/act_source_observer_view2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="@android:color/black"/>
<me.twc.source.observer.widget.SourceObserverView2
android:id="@+id/obserview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package me.twc.source.observer.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.annotation.MainThread
import androidx.core.view.isVisible
import me.twc.source.observer.R
import me.twc.source.observer.engine.ISourceObserverView

/**
* @author 唐万超
* @date 2022/09/23
*/
open class SourceObserverView2 @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attributeSet, defStyleAttr), ISourceObserverView {
companion object {
/**
* 展示实际 Loading 的延迟时间
*/
val GLOBAL_LOADING_DELAY_TIME = 450L
val GLOBAL_ERROR_BACKGROUND = Color.WHITE
val GLOBAL_EMPTY_BACKGROUND = Color.WHITE
}

var mLoadingDelayTime = GLOBAL_LOADING_DELAY_TIME
var mErrorBackground = GLOBAL_ERROR_BACKGROUND
var mEmptyBackground = GLOBAL_EMPTY_BACKGROUND
var mReloadFun: (() -> Unit)? = null

private val mLayoutInflater = LayoutInflater.from(context)
private var mLoadingLayout: View? = null
private var mErrorLayout: View? = null
private var mEmptyLayout: View? = null

private val mDelayLoadingRunnable = Runnable {
mLoadingLayout?.isVisible = true
}

init {
// 可点击,防止点击穿透
isClickable = true
isVisible = false
}

@LayoutRes
open fun getLoadingLayoutRes(): Int = R.layout.source_observer_layout_loading

@LayoutRes
open fun getErrorLayoutRes(): Int = R.layout.source_observer_layout_error

@LayoutRes
open fun getEmptyLayoutRes(): Int = R.layout.source_observer_layout_empty

@MainThread
override fun showSourceLoadingView() {
if (mLoadingLayout == null) {
mLoadingLayout = mLayoutInflater.inflate(getLoadingLayoutRes(), this, false)
addView(mLoadingLayout)
}
showView()
postDelayed(mDelayLoadingRunnable, mLoadingDelayTime)
}

@MainThread
override fun showSourceErrorView(message: String) {
if (mErrorLayout == null) {
mErrorLayout = mLayoutInflater.inflate(getErrorLayoutRes(), this, false)
mErrorLayout!!.setBackgroundColor(mErrorBackground)
mErrorLayout!!.setOnClickListener {
mReloadFun?.invoke()
}
addView(mErrorLayout)
}
mErrorLayout!!.findViewById<TextView>(R.id.tv_network_error).text = message
showView(mErrorLayout)
}

@MainThread
override fun showSourceEmptyView(message: String) {
if (mEmptyLayout == null) {
mEmptyLayout = mLayoutInflater.inflate(getEmptyLayoutRes(), this, false)
mEmptyLayout!!.setBackgroundColor(mEmptyBackground)
addView(mEmptyLayout)
}
mEmptyLayout!!.findViewById<TextView>(R.id.tv_empty).text = message
showView(mEmptyLayout)
}

@MainThread
override fun showSourceSuccessView() {
removeCallbacks(mDelayLoadingRunnable)
isVisible = false
}

private fun showView(show: View? = null) {
removeCallbacks(mDelayLoadingRunnable)
mLoadingLayout?.isVisible = show === mLoadingLayout
mErrorLayout?.isVisible = show === mErrorLayout
mEmptyLayout?.isVisible = show === mEmptyLayout
isVisible = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="146pt"
android:height="107pt"
android:viewportWidth="146"
android:viewportHeight="107">
<group>
<clip-path
android:pathData="M4.577,84.306l140.499,0l0,22.5l-140.499,0z"/>
<path
android:pathData="M145.077,95.556C145.077,101.769 113.626,106.806 74.827,106.806C36.029,106.806 4.577,101.769 4.577,95.556C4.577,89.342 36.029,84.306 74.827,84.306C113.626,84.306 145.077,89.342 145.077,95.556"
android:strokeWidth="1"
android:fillColor="#F4F4F4"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
</group>
<path
android:pathData="M115.566,49.056C115.566,71.423 97.433,89.556 75.066,89.556C52.699,89.556 34.566,71.423 34.566,49.056C34.566,26.688 52.699,8.556 75.066,8.556C97.433,8.556 115.566,26.688 115.566,49.056"
android:strokeWidth="1"
android:fillColor="#E9E9E9"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:pathData="M115.566,49.056C115.566,71.423 97.433,89.556 75.066,89.556C52.699,89.556 34.566,71.423 34.566,49.056C34.566,26.688 52.699,8.556 75.066,8.556C97.433,8.556 115.566,26.688 115.566,49.056Z"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="#C8C8C8"
android:fillType="evenOdd"/>
<path
android:pathData="M79.808,16.461C80.635,17.715 80.289,19.4 79.037,20.228C77.783,21.055 76.097,20.71 75.27,19.456C74.443,18.203 74.787,16.517 76.041,15.69C77.294,14.863 78.981,15.208 79.808,16.461"
android:strokeWidth="1"
android:fillColor="#FFFFFF"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:pathData="M98.748,33.168C98.268,31.629 96.877,27.893 93.183,24.752C89.454,21.584 85.502,20.825 83.907,20.606C82.851,20.202 82.208,19.229 82.301,18.348C82.423,17.189 83.823,16.152 85.389,16.56C87.424,16.975 91.716,18.135 95.834,21.674C100.109,25.348 101.897,29.607 102.584,31.57C102.884,33.17 101.822,34.476 100.736,34.544C99.973,34.593 99.171,34.033 98.748,33.168"
android:strokeWidth="1"
android:fillColor="#FFFFFF"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:pathData="M112.265,33.037C121.145,32.92 126.942,34.347 127.758,37.394C129.528,43.996 107.261,55.701 78.024,63.538C53.584,70.089 23.651,71.993 21.881,65.776C21.328,63.834 23.309,60.345 34.816,53.557"
android:strokeLineJoin="round"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="#C8C8C8"
android:fillType="evenOdd"/>
<path
android:pathData="M126.331,19.813C126.331,21.861 124.671,23.522 122.623,23.522C120.575,23.522 118.915,21.861 118.915,19.813C118.915,17.765 120.575,16.105 122.623,16.105C124.671,16.105 126.331,17.765 126.331,19.813"
android:strokeWidth="1"
android:fillColor="#DFDFDF"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:pathData="M120.978,9.585C127.559,9.585 132.894,14.92 132.894,21.501"
android:strokeLineJoin="bevel"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="#DFDFDF"
android:fillType="evenOdd"
android:strokeLineCap="round"/>
<path
android:pathData="M120.978,2C131.898,2 140.749,10.853 140.749,21.772"
android:strokeLineJoin="round"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="#DFDFDF"
android:fillType="evenOdd"
android:strokeLineCap="round"/>
<path
android:pathData="M9.155,55.913C9.155,58.441 7.106,60.491 4.578,60.491C2.05,60.491 0.001,58.441 0.001,55.913C0.001,53.386 2.05,51.336 4.578,51.336C7.106,51.336 9.155,53.386 9.155,55.913"
android:strokeWidth="1"
android:fillColor="#E1E1E1"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
<path
android:pathData="M129.523,72.639C129.523,74.738 127.821,76.44 125.721,76.44C123.622,76.44 121.919,74.738 121.919,72.639C121.919,70.539 123.622,68.837 125.721,68.837C127.821,68.837 129.523,70.539 129.523,72.639"
android:strokeWidth="1"
android:fillColor="#F5F5F5"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
</vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12pt" />
<stroke
android:width="0.5pt"
android:color="#DADADA" />
<solid android:color="@android:color/white" />
</shape>
Loading

0 comments on commit 4533e72

Please sign in to comment.