Skip to content

Commit

Permalink
Merge pull request #13 from Aftermoon-dev/develop-SimpleView
Browse files Browse the repository at this point in the history
Manage constructor and change pattern to Singleton
  • Loading branch information
jungh0 authored Oct 22, 2019
2 parents 00594a8 + 13718c5 commit 460cb1d
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 24 deletions.
23 changes: 21 additions & 2 deletions app/src/main/java/com/bluecat/view404Demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,47 @@

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

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()
Expand Down
58 changes: 36 additions & 22 deletions view404/src/main/java/com/bluecat/view404/View404.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
54 changes: 54 additions & 0 deletions view404/src/main/java/com/bluecat/view404/View404CustomLayout.kt
Original file line number Diff line number Diff line change
@@ -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) }
}
}
9 changes: 9 additions & 0 deletions view404/src/main/res/drawable/ic_error_outline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M11,15h2v2h-2zM11,7h2v6h-2zM11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector>
43 changes: 43 additions & 0 deletions view404/src/main/res/layout/view404_customlayout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 Team Mulro in BlueCat-Community
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<ImageView
android:id="@+id/errorImage"
android:layout_width="64dp"
android:layout_height="64dp"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:src="@drawable/ic_error_outline" />

<TextView
android:id="@+id/errorText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold"
android:layout_below="@+id/errorImage"
android:text="Error!" />
</RelativeLayout>
5 changes: 5 additions & 0 deletions view404/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="custom_background_default">#ffffff</color>

</resources>

0 comments on commit 460cb1d

Please sign in to comment.