Created
November 14, 2019 06:53
-
-
Save shmehdi01/d46a66ab9a1700420638e148ec3cfa84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gist.shmehdi | |
import android.animation.Animator | |
import android.animation.AnimatorListenerAdapter | |
import android.content.Context | |
import android.net.Uri | |
import android.os.Build | |
import android.view.View | |
import android.view.inputmethod.InputMethodManager | |
import android.widget.ImageView | |
import android.widget.Toast | |
import androidx.annotation.IdRes | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.content.FileProvider | |
import androidx.fragment.app.Fragment | |
import com.google.android.material.snackbar.Snackbar | |
import java.io.File | |
open class BaseUtilActivity : AppCompatActivity() { | |
fun loadFragment( | |
fragment: Fragment, | |
addToStack: Boolean = false, | |
add: Boolean = false, @IdRes rootId: Int = R.id.container | |
) { | |
supportFragmentManager.beginTransaction().apply { | |
if (addToStack) addToBackStack(null) | |
if (add) add(rootId, fragment) else replace(rootId, fragment) | |
}.commit() | |
} | |
fun showToast(text: String, length: Int = Toast.LENGTH_SHORT) { | |
Toast.makeText(this, text, length).show() | |
} | |
fun File.getUri(): Uri { | |
return if (Build.VERSION.SDK_INT >= 24) { | |
FileProvider.getUriForFile(MyApp.instance!!, "$packageName.provider", this) | |
} else { | |
Uri.fromFile(this) | |
} | |
} | |
fun showSnackbar(view: View, text: String) { | |
Snackbar.make(view, text, Snackbar.LENGTH_SHORT).show() | |
} | |
fun showSnackbarWithAction( | |
view: View, | |
text: String, | |
actionText: String, | |
actionListener: View.OnClickListener | |
) { | |
Snackbar.make(view, text, Snackbar.LENGTH_LONG).setAction(actionText, actionListener).show() | |
} | |
fun ImageView.tint(colorID: Int) { | |
this.setColorFilter(resources.getColor(colorID)) | |
} | |
fun View.goneWithFade(duration: Long = 300) { | |
val view = this | |
view.animate() | |
.alpha(0f) | |
.setDuration(duration) | |
.setListener(object : AnimatorListenerAdapter() { | |
override fun onAnimationEnd(animation: Animator?) { | |
view.visibility = View.GONE | |
} | |
}) | |
} | |
fun View.visbleWithFade(duration: Long = 300) { | |
val view = this | |
view.alpha = 0f | |
view.show() | |
view.animate() | |
.alpha(1f) | |
.setDuration(duration) | |
.setListener(null) | |
} | |
fun View.hide() { | |
this.visibility = View.GONE | |
} | |
fun View.show() { | |
this.visibility = View.VISIBLE | |
} | |
fun View.invisible() { | |
this.visibility = View.INVISIBLE | |
} | |
fun View.hideKeyboard() { | |
this.let { v -> | |
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager | |
imm?.hideSoftInputFromWindow(v.windowToken, 0) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment