Skip to content

Commit

Permalink
implement view extensions
Browse files Browse the repository at this point in the history
move all resource id of views to ids.xml
convert view to appcompat views
update gradle and kotlin version
  • Loading branch information
erkutaras committed Feb 2, 2019
1 parent 8c84034 commit a30cd44
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 76 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.10'
ext.kotlin_version = '1.3.20'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Oct 14 21:18:39 MSK 2018
#Sat Feb 02 14:14:10 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
99 changes: 39 additions & 60 deletions library/src/main/java/com/erkutaras/statelayout/StateLayout.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.erkutaras.statelayout

import android.content.Context
import androidx.annotation.LayoutRes
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import com.erkutaras.statelayout.StateLayout.State.*

/**
Expand All @@ -34,25 +33,21 @@ class StateLayout @JvmOverloads constructor(context: Context,
private var loadingWithContentLayoutRes: Int = R.layout.layout_state_loading_with_content

init {

if (isInEditMode) {
state = CONTENT
}

context.theme.obtainStyledAttributes(
attrs,
R.styleable.StateLayout,
0, 0).apply {

try {
state = State.values()[getInteger(R.styleable.StateLayout_state, NONE.ordinal)]
loadingLayoutRes = getResourceId(R.styleable.StateLayout_loadingLayout, R.layout.layout_state_loading)
infoLayoutRes = getResourceId(R.styleable.StateLayout_infoLayout, R.layout.layout_state_info)
loadingWithContentLayoutRes = getResourceId(R.styleable.StateLayout_loadingWithContentLayout, R.layout.layout_state_loading_with_content)
} finally {
recycle()
context.theme.obtainStyledAttributes(attrs, R.styleable.StateLayout, 0, 0)
.apply {
try {
state = State.values()[getInteger(R.styleable.StateLayout_state, NONE.ordinal)]
loadingLayoutRes = getResourceId(R.styleable.StateLayout_loadingLayout, R.layout.layout_state_loading)
infoLayoutRes = getResourceId(R.styleable.StateLayout_infoLayout, R.layout.layout_state_info)
loadingWithContentLayoutRes = getResourceId(R.styleable.StateLayout_loadingWithContentLayout, R.layout.layout_state_loading_with_content)
} finally {
recycle()
}
}
}
}

override fun onFinishInflate() {
Expand Down Expand Up @@ -115,18 +110,14 @@ class StateLayout @JvmOverloads constructor(context: Context,
private fun throwChildCountException(): Nothing =
throw IllegalStateException("StateLayout can host only one direct child")

private fun inflate(@LayoutRes layoutId: Int): View? {
return LayoutInflater.from(context).inflate(layoutId, null)
}

fun initialState(state: State) {
this.state = state
}

fun loadingMessage(message: String): StateLayout {
loadingLayout?.findViewById<TextView>(R.id.textView_state_layout_loading_message)?.let {
it.text = message
it.visibility = View.VISIBLE
loadingLayout.findView<TextView>(R.id.textView_state_layout_loading_message) {
text = message
visibility = View.VISIBLE
}
return loading()
}
Expand Down Expand Up @@ -157,56 +148,56 @@ class StateLayout @JvmOverloads constructor(context: Context,
}

fun infoImage(imageRes: Int): StateLayout {
infoLayout?.findViewById<ImageView>(R.id.imageView_state_layout_info)?.let {
it.setImageResource(imageRes)
it.visibility = View.VISIBLE
infoLayout.findView<ImageView>(R.id.imageView_state_layout_info) {
setImageResource(imageRes)
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
infoLayout.findView<TextView>(R.id.textView_state_layout_info_title) {
text = title
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
infoLayout.findView<TextView>(R.id.textView_state_layout_info_message) {
text = message
visibility = View.VISIBLE
}
return info()
}

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

fun infoButtonListener(block: () -> Unit) {
infoLayout?.findViewById<Button>(R.id.button_state_layout_info)?.setOnClickListener {
block.invoke()
infoLayout.findView<Button>(R.id.button_state_layout_info) {
setOnClickListener { block.invoke() }
}
info()
}

fun infoButtonText(buttonText: String): StateLayout {
infoLayout?.findViewById<Button>(R.id.button_state_layout_info)?.let {
it.text = buttonText
it.visibility = View.VISIBLE
infoLayout.findView<Button>(R.id.button_state_layout_info) {
text = buttonText
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
infoLayout.findView<Button>(R.id.button_state_layout_info) {
text = buttonText
setOnClickListener { onStateLayoutListener?.onStateLayoutInfoButtonClick() }
visibility = View.VISIBLE
}
return info()
}
Expand Down Expand Up @@ -243,29 +234,17 @@ class StateLayout @JvmOverloads constructor(context: Context,
showState(provideLoadingWithContentStateInfo())
}

fun showLoading(stateInfo: StateInfo?) {
if (stateInfo?.state == LOADING) showState(stateInfo)
}
fun showLoading(stateInfo: StateInfo?) = showState(stateInfo)

fun showContent(stateInfo: StateInfo?) {
if (stateInfo?.state == CONTENT) showState(stateInfo)
}
fun showContent(stateInfo: StateInfo?) = showState(stateInfo)

fun showInfo(stateInfo: StateInfo?) {
if (stateInfo?.state == INFO) showState(stateInfo)
}
fun showInfo(stateInfo: StateInfo?) = showState(stateInfo)

fun showLoadingWithContent(stateInfo: StateInfo?) {
if (stateInfo?.state == LOADING_WITH_CONTENT) showState(stateInfo)
}
fun showLoadingWithContent(stateInfo: StateInfo?) = showState(stateInfo)

fun showError(stateInfo: StateInfo?) {
if (stateInfo?.state == ERROR) showState(stateInfo)
}
fun showError(stateInfo: StateInfo?) = showState(stateInfo)

fun showEmpty(stateInfo: StateInfo?) {
if (stateInfo?.state == EMPTY) showState(stateInfo)
}
fun showEmpty(stateInfo: StateInfo?) = showState(stateInfo)

fun showState(stateInfo: StateInfo?) {
when (stateInfo?.state) {
Expand Down
18 changes: 18 additions & 0 deletions library/src/main/java/com/erkutaras/statelayout/ViewExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.erkutaras.statelayout

import android.view.LayoutInflater
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes

/**
* Created by erkutaras on 2.02.2019.
*/

internal fun <T : View> View?.findView(@IdRes id: Int, block: T.() -> Unit) {
this?.findViewById<T>(id)?.let { block(it) }
}

internal fun View.inflate(@LayoutRes layoutId: Int): View? {
return LayoutInflater.from(this.context).inflate(layoutId, null)
}
16 changes: 8 additions & 8 deletions library/src/main/res/layout/layout_state_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
android:orientation="vertical"
android:padding="64dp">

<ImageView
android:id="@+id/imageView_state_layout_info"
<androidx.appcompat.widget.AppCompatImageView
android:id="@id/imageView_state_layout_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:visibility="gone"
tools:srcCompat="@drawable/ic_info_outline_black_64dp"
tools:visibility="visible"/>

<TextView
android:id="@+id/textView_state_layout_info_title"
<androidx.appcompat.widget.AppCompatTextView
android:id="@id/textView_state_layout_info_title"
style="@android:style/TextAppearance.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -30,8 +30,8 @@
tools:text="Oops..."
tools:visibility="visible"/>

<TextView
android:id="@+id/textView_state_layout_info_message"
<androidx.appcompat.widget.AppCompatTextView
android:id="@id/textView_state_layout_info_message"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -42,8 +42,8 @@
tools:text="Sorry!... Something goes wrong :("
tools:visibility="visible"/>

<Button
android:id="@+id/button_state_layout_info"
<androidx.appcompat.widget.AppCompatButton
android:id="@id/button_state_layout_info"
style="@android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
6 changes: 3 additions & 3 deletions library/src/main/res/layout/layout_state_loading.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/textView_state_layout_loading_message"
<androidx.appcompat.widget.AppCompatTextView
android:id="@id/textView_state_layout_loading_message"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -22,7 +22,7 @@
tools:visibility="visible"/>

<ProgressBar
android:id="@+id/progressBar_state_layout"
android:id="@id/progressBar_state_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tint="?colorAccent"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent">

<ProgressBar
android:id="@+id/progressBar_state_layout_with_content"
android:id="@id/progressBar_state_layout_with_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- layout_state_loading.xml -->
<item name="textView_state_layout_loading_message" type="id" />
<item name="progressBar_state_layout" type="id" />
<item name="view_state_layout_loading" type="id" />

<!-- layout_state_info.xml -->
<item name="imageView_state_layout_info" type="id" />
<item name="textView_state_layout_info_title" type="id" />
<item name="textView_state_layout_info_message" type="id" />
<item name="button_state_layout_info" type="id" />

<!-- layout_state_loading_with_content.xml -->
<item name="progressBar_state_layout_with_content" type="id" />
</resources>

0 comments on commit a30cd44

Please sign in to comment.