diff --git a/app/src/main/java/com/bluecat/view404Demo/MainActivity.kt b/app/src/main/java/com/bluecat/view404Demo/MainActivity.kt index e872a46..6419fee 100644 --- a/app/src/main/java/com/bluecat/view404Demo/MainActivity.kt +++ b/app/src/main/java/com/bluecat/view404Demo/MainActivity.kt @@ -16,10 +16,12 @@ package com.bluecat.view404Demo +import android.graphics.Color import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.bluecat.view404.View404 +import com.bluecat.view404.View404CustomLayout import com.bluecat.view404.show404 import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.dismissError @@ -27,17 +29,34 @@ import kotlinx.android.synthetic.main.activity_main.dismissError class MainActivity : AppCompatActivity() { private var view404: View404? = null + private var view404CustomLayout: View404CustomLayout? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + view404CustomLayout = View404CustomLayout.getInstance( + context = applicationContext, + bgColor = Color.parseColor("#ffffff"), + errMsg = "에러발생!" + ) + + /** It's same as + + view404CustomLayout = View404CustomLayout.getInstance(applicationContext).apply { + bgColor = Color.parseColor("#ffffff") + errMsg = "에러발생!" + //errImg = null + } + + **/ + + showError.setOnClickListener { if (view404 == null) { - view404 = View404(this, R.layout.layout_404) + view404 = View404(this, view404CustomLayout?.inflate()!!) Toast.makeText(this, "shown", Toast.LENGTH_SHORT).show() - //parentLayout.show404(view404!!) errorLayout.show404(view404!!, R.anim.view404_fade_in_default) } else { Toast.makeText(this, "already shown", Toast.LENGTH_SHORT).show() diff --git a/view404/src/main/java/com/bluecat/view404/View404.kt b/view404/src/main/java/com/bluecat/view404/View404.kt index 3931381..a2537f8 100644 --- a/view404/src/main/java/com/bluecat/view404/View404.kt +++ b/view404/src/main/java/com/bluecat/view404/View404.kt @@ -21,27 +21,21 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.animation.AnimationUtils +import androidx.annotation.LayoutRes import androidx.core.view.ViewCompat -class View404(private val context: Context, layout: Int) { +class View404 { + private val context: Context /** NoneNull type */ - private var layoutInflater: LayoutInflater = - context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + private var layoutInflater: LayoutInflater - /** initialized in show method. */ - private var parentView: ViewGroup? = null - - /** public getter, private setter */ - var view404: View - private set + /** initialization(Layout Resource ID)*/ + constructor(context: Context, @LayoutRes layout: Int) { + this.context = context + layoutInflater = + context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater - /** public getter, private setter */ - var isShowing = false - private set - - /** initialization */ - init { /** 'apply' refer to the context object as a lambda receiver by keyword 'this', it returns context object. */ view404 = layoutInflater.inflate(layout, null).apply { @@ -52,12 +46,32 @@ class View404(private val context: Context, layout: Int) { } } + /** initialization(Custom View) */ + constructor(context: Context, layout: View) { + this.context = context + layoutInflater = + context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + view404 = layout + } + + + /** initialized in show method. */ + private var parentView: ViewGroup? = null + + /** public getter, private setter */ + var view404: View? = null + private set + + /** public getter, private setter */ + var isShowing = false + private set + /** shows the view404 on the parent view. - if you want to put another fadeIn animation, you can do that. */ - fun show(parentView: ViewGroup?, fadeInAnimation:Int = R.anim.not_move_activity) { + if you want to put another fadeIn animation, you can do that. */ + fun show(parentView: ViewGroup?, fadeInAnimation: Int = R.anim.not_move_activity) { if (!isShowing && parentView != null) { this.parentView = parentView - view404.startAnimation(AnimationUtils.loadAnimation(context, fadeInAnimation)) + view404!!.startAnimation(AnimationUtils.loadAnimation(context, fadeInAnimation)) /** 'run' refer to the context object as a lambda receiver by keyword 'this', it returns the lambda result. */ this.parentView?.run { @@ -67,16 +81,16 @@ class View404(private val context: Context, layout: Int) { invalidate() } - ViewCompat.setTranslationZ(view404, 99f) + ViewCompat.setTranslationZ(view404!!, 99f) isShowing = true } } /** dismiss the view404 on the parent view. - if you want to put another fadeOut animation, you can do that. */ - fun dismiss(fadeOutAnimation:Int = R.anim.not_move_activity) { + if you want to put another fadeOut animation, you can do that. */ + fun dismiss(fadeOutAnimation: Int = R.anim.not_move_activity) { if (isShowing) { - view404.startAnimation(AnimationUtils.loadAnimation(context, fadeOutAnimation)) + view404!!.startAnimation(AnimationUtils.loadAnimation(context, fadeOutAnimation)) parentView?.removeView(view404) isShowing = false } diff --git a/view404/src/main/java/com/bluecat/view404/View404CustomLayout.kt b/view404/src/main/java/com/bluecat/view404/View404CustomLayout.kt new file mode 100644 index 0000000..a3117ac --- /dev/null +++ b/view404/src/main/java/com/bluecat/view404/View404CustomLayout.kt @@ -0,0 +1,54 @@ +package com.bluecat.view404 + +import android.annotation.SuppressLint +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import kotlinx.android.synthetic.main.view404_customlayout.view.* + +class View404CustomLayout private constructor() { + + private lateinit var layoutInflater: LayoutInflater + private lateinit var customLayout: View + + /** option area */ + var bgColor: Int = R.color.custom_background_default + var errMsg: String = "" + var errImg: Int? = null + + /** Static area */ + companion object { + @SuppressLint("InflateParams") + @JvmStatic + fun getInstance(context: Context) = View404CustomLayout().apply { + layoutInflater = + context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + customLayout = layoutInflater.inflate(R.layout.view404_customlayout, null).apply { + layoutParams = ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + ) + } + } + + @JvmStatic + fun getInstance( + context: Context, + bgColor: Int = R.color.custom_background_default, + errMsg: String = "", + errImg: Int? = null + ) = getInstance(context).apply { + this.bgColor = bgColor + this.errMsg = errMsg + this.errImg = errImg + } + } + + + fun inflate(): View = customLayout.apply { + setBackgroundColor(bgColor) + errorText.text = errMsg + errImg?.let { errorImage.setImageResource(it) } + } +} \ No newline at end of file diff --git a/view404/src/main/res/drawable/ic_error_outline.xml b/view404/src/main/res/drawable/ic_error_outline.xml new file mode 100644 index 0000000..4c5029e --- /dev/null +++ b/view404/src/main/res/drawable/ic_error_outline.xml @@ -0,0 +1,9 @@ + + + diff --git a/view404/src/main/res/layout/view404_customlayout.xml b/view404/src/main/res/layout/view404_customlayout.xml new file mode 100644 index 0000000..b01213a --- /dev/null +++ b/view404/src/main/res/layout/view404_customlayout.xml @@ -0,0 +1,43 @@ + + + + + + + + + \ No newline at end of file diff --git a/view404/src/main/res/values/colors.xml b/view404/src/main/res/values/colors.xml new file mode 100644 index 0000000..3321ef5 --- /dev/null +++ b/view404/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #ffffff + + \ No newline at end of file