Merge pull request #148 from TouchInstinct/loading-view

added loading view
This commit is contained in:
Aksenov Vladimir 2020-08-21 20:00:18 +05:00 committed by GitHub
commit 2728db1940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 188 additions and 0 deletions

View File

@ -1,10 +1,19 @@
apply from: "../android-configs/lib-config.gradle"
apply plugin: 'kotlin-android'
android {
buildFeatures {
viewBinding true
}
}
dependencies {
implementation project(":utils")
implementation project(":kotlin-extensions")
implementation project(":logging")
implementation "com.google.android.material:material"
implementation "androidx.core:core-ktx"
constraints {
implementation("com.google.android.material:material") {
@ -12,5 +21,19 @@ dependencies {
require '1.0.0'
}
}
implementation("androidx.core:core-ktx") {
version {
require '1.3.1'
}
}
implementation("org.jetbrains.kotlin:kotlin-stdlib") {
version {
require '1.3.0'
}
}
}
}
repositories {
mavenCentral()
}

View File

@ -0,0 +1,69 @@
package ru.touchin.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.core.content.withStyledAttributes
import ru.touchin.extensions.observable
import ru.touchin.extensions.setOnRippleClickListener
import ru.touchin.roboswag.components.views.R
import ru.touchin.roboswag.components.views.databinding.ProgressViewBinding
import kotlin.properties.Delegates
//TODO make customizable views list and views style
class LoadingContentView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : Switcher(context, attrs) {
private val binding = ProgressViewBinding.inflate(LayoutInflater.from(context), this)
var state by Delegates.observable<State>(State.Loading, this::updateView)
init {
if (attrs != null) {
context.withStyledAttributes(attrs, R.styleable.LoadingContentView, defStyleAttr, 0) {
if (hasValue(R.styleable.LoadingContentView_stubText)) {
setStubText(getString(R.styleable.LoadingContentView_stubText))
}
}
}
}
private fun setStubText(text: String?) {
binding.textStub.text = text
}
private fun updateView(state: State) {
if (state == State.ShowContent) {
getChildAt(childCount - 1)?.let { showChild(it.id) }
} else {
when (state) {
is State.Stub -> {
setStubText(state.stubText)
showChild(R.id.text_stub)
}
is State.Loading -> {
showChild(R.id.progress_bar)
}
is State.Error -> {
binding.apply {
errorText.text = state.errorText
errorRepeatButton.setOnRippleClickListener { state.action.invoke() }
errorRepeatButton.text = state.repeatButtonText
showChild(R.id.error_with_repeat)
}
}
}
}
}
sealed class State {
object ShowContent : State()
data class Stub(val stubText: String) : State()
object Loading : State()
data class Error(val action: () -> Unit, val errorText: String, val repeatButtonText: String) : State()
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="220dp"
android:height="50dp"
android:viewportWidth="220"
android:viewportHeight="50">
<path
android:pathData="M8 0h204a8 8 0 0 1 8 8v34a8 8 0 0 1-8 8H8a8 8 0 0 1-8-8V8a8 8 0 0 1 8-8z"
android:fillColor="#F2F2F7" />
</vector>

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,64 @@
<merge 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:background="@android:color/white">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminateTint="@color/loadingContentViewProgressBar" />
<LinearLayout
android:id="@+id/error_with_repeat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/error_text"
style="@style/Text.Regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:gravity="center"
android:textColor="@color/loadingContentViewTextColor"
android:textSize="15sp"
tools:text="Error text" />
<TextView
android:id="@+id/error_repeat_button"
style="@style/Text.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/light_button_background"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:textColor="@color/loadingContentViewButtonTextColor"
android:textSize="18sp"
tools:text="Повторить" />
</LinearLayout>
<TextView
android:id="@+id/text_stub"
style="@style/Text.Regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:gravity="center"
android:textColor="@color/loadingContentViewTextColor"
android:textSize="15sp"
tools:text="Stub text" />
</merge>

View File

@ -30,4 +30,8 @@
<attr name="defaultChild" format="reference"/>
</declare-styleable>
<declare-styleable name="LoadingContentView">
<attr name="stubText" format="string"/>
</declare-styleable>
</resources>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="loadingContentViewProgressBar">#8D8EA6</color>
<color name="loadingContentViewTextColor">#999BBF</color>
<color name="loadingContentViewButtonTextColor">#141233</color>
</resources>

View File

@ -4,4 +4,17 @@
<item name="android:inAnimation">@android:anim/fade_in</item>
<item name="android:outAnimation">@android:anim/fade_out</item>
</style>
<style name="Text">
<item name="android:textColor">@android:color/white</item>
</style>
<style name="Text.Medium">
<item name="android:fontFamily">@font/roboto_medium</item>
</style>
<style name="Text.Regular">
<item name="android:fontFamily">@font/roboto</item>
</style>
</resources>