Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ captures/

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
.idea/caches
.idea/*

# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ stateLayout.content()
stateLayout.loadingWithContent()

// error/info
stateLayout.infoImage(R.drawable.ic_android_black_64dp)
.infoTitle("Ooops.... :(")
.infoMessage("Unexpected error occurred. Please refresh the page!")
.infoButton("Refresh", onStateLayoutListener)
stateLayout.InfoLayoutBuilder()
.infoImage(R.drawable.ic_android_black_64dp)
.infoTitle("Ooops.... :(")
.infoMessage("Unexpected error occurred. Please refresh the page!")
.infoButton("Refresh", onStateLayoutListener)
// error/info
stateLayout.info()
```
Expand Down
126 changes: 61 additions & 65 deletions library/src/main/java/com/erkutaras/statelayout/StateLayout.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.erkutaras.statelayout

import android.content.Context
import android.support.annotation.LayoutRes
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewStub
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageView
Expand All @@ -15,13 +14,12 @@ import android.widget.TextView
*/
class StateLayout @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0)
: FrameLayout(context, attrs, defStyleAttr) {
defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {

private var contentLayout: View? = null
private var loadingLayout: View? = null
private var infoLayout: View? = null
private var loadingWithContentLayout: View? = null
private var loadingLayout: ViewStub? = null
private var infoLayout: ViewStub? = null
private var loadingWithContentLayout: ViewStub? = null

private var state: State = State.CONTENT

Expand All @@ -41,20 +39,17 @@ class StateLayout @JvmOverloads constructor(context: Context,
}

private fun setupLoadingState() {
loadingLayout = inflate(R.layout.layout_state_loading)
loadingLayout?.visibility = View.GONE
loadingLayout = ViewStub(context, R.layout.layout_state_loading)
addView(loadingLayout)
}

private fun setupInfoState() {
infoLayout = inflate(R.layout.layout_state_info)
infoLayout?.visibility = View.GONE
infoLayout = ViewStub(context, R.layout.layout_state_info)
addView(infoLayout)
}

private fun setupLoadingWithContentState() {
loadingWithContentLayout = inflate(R.layout.layout_state_loading_with_content)
loadingWithContentLayout?.visibility = View.GONE
loadingWithContentLayout = ViewStub(context, R.layout.layout_state_loading_with_content)
addView(loadingWithContentLayout)
}

Expand All @@ -76,54 +71,6 @@ class StateLayout @JvmOverloads constructor(context: Context,
return this
}

fun infoImage(imageRes: Int): StateLayout {
infoLayout?.findViewById<ImageView>(R.id.imageView_state_layout_info)?.let {
it.setImageResource(imageRes)
it.visibility = View.VISIBLE
}
return info()
}

fun infoTitle(title: String): StateLayout {
infoLayout?.findViewById<TextView>(R.id.textView_state_layout_info_title)?.let {
it.text = title
it.visibility = View.VISIBLE
}
return info()
}

fun infoMessage(message: String): StateLayout {
infoLayout?.findViewById<TextView>(R.id.textView_state_layout_info_message)?.let {
it.text = message
it.visibility = View.VISIBLE
}
return info()
}

fun infoButtonListener(onStateLayoutListener: OnStateLayoutListener?): StateLayout {
infoLayout?.findViewById<Button>(R.id.button_state_layout_info)?.setOnClickListener {
onStateLayoutListener?.onStateLayoutInfoButtonClick()
}
return info()
}

fun infoButtonText(buttonText: String): StateLayout {
infoLayout?.findViewById<Button>(R.id.button_state_layout_info)?.let {
it.text = buttonText
it.visibility = View.VISIBLE
}
return info()
}

fun infoButton(buttonText: String, onStateLayoutListener: OnStateLayoutListener?): StateLayout {
infoLayout?.findViewById<Button>(R.id.button_state_layout_info)?.let { it ->
it.text = buttonText
it.setOnClickListener { onStateLayoutListener?.onStateLayoutInfoButtonClick() }
it.visibility = View.VISIBLE
}
return info()
}

fun info(): StateLayout {
state = State.INFO
loadingLayout?.visibility = View.GONE
Expand All @@ -148,11 +95,60 @@ class StateLayout @JvmOverloads constructor(context: Context,
}
}

private fun throwChildCountException(): Nothing =
throw IllegalStateException("StateLayout can host only one direct child")
private fun throwChildCountException(): Nothing = throw IllegalStateException("StateLayout can host only one direct child")

inner class InfoLayoutBuilder {
init {
info()
}

fun infoImage(imageRes: Int): InfoLayoutBuilder {
findViewById<ImageView>(R.id.imageView_state_layout_info)?.let {
it.setImageResource(imageRes)
it.visibility = View.VISIBLE
}
return this
}

fun infoTitle(title: String): InfoLayoutBuilder {
infoLayout?.findViewById<TextView>(R.id.textView_state_layout_info_title)?.let {
it.text = title
it.visibility = View.VISIBLE
}
return this
}

fun infoMessage(message: String): InfoLayoutBuilder {
findViewById<TextView>(R.id.textView_state_layout_info_message)?.let {
it.text = message
it.visibility = View.VISIBLE
}
return this
}

fun infoButtonListener(onStateLayoutListener: OnStateLayoutListener?): InfoLayoutBuilder {
findViewById<Button>(R.id.button_state_layout_info)?.setOnClickListener {
onStateLayoutListener?.onStateLayoutInfoButtonClick()
}
return this
}

fun infoButtonText(buttonText: String): InfoLayoutBuilder {
findViewById<Button>(R.id.button_state_layout_info)?.let {
it.text = buttonText
it.visibility = View.VISIBLE
}
return this
}

private fun inflate(@LayoutRes layoutId: Int): View? {
return LayoutInflater.from(context).inflate(layoutId, null)
fun infoButton(buttonText: String, onStateLayoutListener: OnStateLayoutListener?): InfoLayoutBuilder {
findViewById<Button>(R.id.button_state_layout_info)?.let { it ->
it.text = buttonText
it.setOnClickListener { onStateLayoutListener?.onStateLayoutInfoButtonClick() }
it.visibility = View.VISIBLE
}
return this
}
}

interface OnStateLayoutListener {
Expand Down
11 changes: 5 additions & 6 deletions library/src/main/res/layout/layout_state_info.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<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"
Expand All @@ -16,7 +15,7 @@
android:layout_marginBottom="16dp"
android:visibility="gone"
tools:srcCompat="@drawable/ic_info_outline_black_64dp"
tools:visibility="visible"/>
tools:visibility="visible" />

<TextView
android:id="@+id/textView_state_layout_info_title"
Expand All @@ -28,7 +27,7 @@
android:textStyle="normal"
android:visibility="gone"
tools:text="Oops..."
tools:visibility="visible"/>
tools:visibility="visible" />

<TextView
android:id="@+id/textView_state_layout_info_message"
Expand All @@ -40,7 +39,7 @@
android:textStyle="normal"
android:visibility="gone"
tools:text="Sorry!... Something goes wrong :("
tools:visibility="visible"/>
tools:visibility="visible" />

<Button
android:id="@+id/button_state_layout_info"
Expand All @@ -52,5 +51,5 @@
android:textColor="@android:color/black"
android:visibility="gone"
tools:text="Try Again"
tools:visibility="visible"/>
tools:visibility="visible" />
</LinearLayout>
11 changes: 5 additions & 6 deletions library/src/main/res/layout/layout_state_loading.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground">
android:background="?android:colorBackground"
android:gravity="center">

<ProgressBar
android:id="@+id/progressBar_state_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:tint="?colorAccent"/>
app:tint="?colorAccent" />

</RelativeLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand All @@ -10,8 +9,7 @@
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:indeterminate="true"
app:tint="?colorAccent"/>
app:tint="?colorAccent" />

</RelativeLayout>
</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import com.erkutaras.statelayout.StateLayout
import kotlinx.android.synthetic.main.activity_state_layout_sample.*
import kotlinx.android.synthetic.main.activity_state_layout_sample.stateLayout
import kotlinx.android.synthetic.main.activity_state_layout_sample.webView

const val WEB_URL = "http://www.erkutaras.com/"

Expand Down Expand Up @@ -54,7 +55,8 @@ class StateLayoutSampleActivity : AppCompatActivity(), StateLayout.OnStateLayout
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
super.onReceivedError(view, request, error)
hasError = true
stateLayout.infoImage(R.drawable.ic_android_black_64dp)
stateLayout.InfoLayoutBuilder()
.infoImage(R.drawable.ic_android_black_64dp)
.infoTitle("Ooops.... :(")
.infoMessage("Unexpected error occurred. Please refresh the page!")
.infoButton("Refresh", onStateLayoutListener)
Expand Down
22 changes: 6 additions & 16 deletions sample/src/main/res/layout/activity_state_layout_sample.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.erkutaras.statelayout.StateLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/stateLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StateLayoutSampleActivity">

<com.erkutaras.statelayout.StateLayout
android:id="@+id/stateLayout"
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
android:layout_height="match_parent" />

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</com.erkutaras.statelayout.StateLayout>

</android.support.constraint.ConstraintLayout>
</com.erkutaras.statelayout.StateLayout>