From a30cd441dc8d96be30ff02dae48adb35b6725647 Mon Sep 17 00:00:00 2001 From: erkutaras Date: Sat, 2 Feb 2019 15:40:33 +0300 Subject: [PATCH] implement view extensions move all resource id of views to ids.xml convert view to appcompat views update gradle and kotlin version --- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- .../com/erkutaras/statelayout/StateLayout.kt | 99 ++++++++----------- .../erkutaras/statelayout/ViewExtensions.kt | 18 ++++ .../src/main/res/layout/layout_state_info.xml | 16 +-- .../main/res/layout/layout_state_loading.xml | 6 +- .../layout_state_loading_with_content.xml | 2 +- library/src/main/res/values/ids.xml | 16 +++ 8 files changed, 89 insertions(+), 76 deletions(-) create mode 100644 library/src/main/java/com/erkutaras/statelayout/ViewExtensions.kt create mode 100644 library/src/main/res/values/ids.xml diff --git a/build.gradle b/build.gradle index 344b644..1a586b3 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5bd21a3..fd176b5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt b/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt index 4a713ea..c87494e 100644 --- a/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt +++ b/library/src/main/java/com/erkutaras/statelayout/StateLayout.kt @@ -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.* /** @@ -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() { @@ -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(R.id.textView_state_layout_loading_message)?.let { - it.text = message - it.visibility = View.VISIBLE + loadingLayout.findView(R.id.textView_state_layout_loading_message) { + text = message + visibility = View.VISIBLE } return loading() } @@ -157,56 +148,56 @@ class StateLayout @JvmOverloads constructor(context: Context, } fun infoImage(imageRes: Int): StateLayout { - infoLayout?.findViewById(R.id.imageView_state_layout_info)?.let { - it.setImageResource(imageRes) - it.visibility = View.VISIBLE + infoLayout.findView(R.id.imageView_state_layout_info) { + setImageResource(imageRes) + visibility = View.VISIBLE } return info() } fun infoTitle(title: String): StateLayout { - infoLayout?.findViewById(R.id.textView_state_layout_info_title)?.let { - it.text = title - it.visibility = View.VISIBLE + infoLayout.findView(R.id.textView_state_layout_info_title) { + text = title + visibility = View.VISIBLE } return info() } fun infoMessage(message: String): StateLayout { - infoLayout?.findViewById(R.id.textView_state_layout_info_message)?.let { - it.text = message - it.visibility = View.VISIBLE + infoLayout.findView(R.id.textView_state_layout_info_message) { + text = message + visibility = View.VISIBLE } return info() } fun infoButtonListener(onStateLayoutListener: OnStateLayoutListener?): StateLayout { - infoLayout?.findViewById