Improve extenson & remove redundant utils

This commit is contained in:
Rahul Kumar Patel
2021-02-24 17:41:20 +05:30
parent 1d29d1dca1
commit b877552634
20 changed files with 209 additions and 195 deletions

View File

@@ -0,0 +1,75 @@
/*
* Aurora Store
* Copyright (C) 2021, Rahul Kumar Patel <whyorean@gmail.com>
*
* Aurora Store is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Aurora Store is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.aurora.extensions
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
fun AppCompatActivity.close() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAfterTransition()
} else {
finish()
}
}
fun AppCompatActivity.restart() {
val intent = intent
overridePendingTransition(0, 0)
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
finish()
overridePendingTransition(0, 0)
startActivity(intent)
}
inline fun <reified T : Activity> Activity.startActivity(): Unit =
this.startActivity(newIntent<T>())
inline fun <reified T : Activity> Activity.startActivity(flags: Int): Unit =
this.startActivity(newIntent<T>(flags))
inline fun <reified T : Activity> Activity.startActivity(extras: Bundle): Unit =
this.startActivity(newIntent<T>(extras))
inline fun <reified T : Activity> Activity.startActivity(flags: Int, extras: Bundle): Unit =
this.startActivity(newIntent<T>(flags, extras))
inline fun <reified T : Activity> Activity.startActivityForResult(requestCode: Int): Unit =
this.startActivityForResult(newIntent<T>(), requestCode)
inline fun <reified T : Activity> Activity.startActivityForResult(
requestCode: Int,
flags: Int
): Unit =
this.startActivityForResult(newIntent<T>(flags), requestCode)
inline fun <reified T : Activity> Activity.startActivityForResult(
extras: Bundle, requestCode: Int
): Unit =
this.startActivityForResult(newIntent<T>(extras), requestCode)
inline fun <reified T : Activity> Activity.startActivityForResult(
extras: Bundle, requestCode: Int, flags: Int
): Unit =
this.startActivityForResult(newIntent<T>(flags, extras), requestCode)

View File

@@ -25,18 +25,27 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.core.app.ShareCompat
import com.aurora.Constants
import com.aurora.gplayapi.data.models.App
import com.aurora.store.MainActivity
import com.aurora.store.R
import com.aurora.store.util.Log
import com.aurora.store.util.ViewUtil
import kotlin.system.exitProcess
val Context.inflater: LayoutInflater
get() = LayoutInflater.from(this)
val Context.displayMetrics: DisplayMetrics
get() = resources.displayMetrics
fun Context.browse(url: String) {
try {
@@ -82,18 +91,10 @@ fun <T> Context.open(className: Class<T>, newTask: Boolean = false) {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(
intent,
ViewUtil.getEmptyActivityBundle(this)
getEmptyActivityBundle()
)
}
fun AppCompatActivity.close() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAfterTransition()
} else {
finish()
}
}
fun Context.restartApp() {
val pendingIntent = PendingIntent.getActivity(
this,
@@ -107,8 +108,23 @@ fun Context.restartApp() {
exitProcess(0)
}
fun Context.getEmptyActivityBundle(): Bundle? {
return ActivityOptionsCompat.makeCustomAnimation(
this,
android.R.anim.fade_in,
android.R.anim.fade_out
).toBundle()
}
fun Context.copyToClipBoard(data: String?) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Download Url", data)
clipboard.setPrimaryClip(clip)
}
fun Context.getStyledAttributeColor(id: Int): Int {
val arr = obtainStyledAttributes(TypedValue().data, intArrayOf(id))
val styledAttr = arr.getColor(0, Color.WHITE)
arr.recycle()
return styledAttr
}

View File

@@ -0,0 +1,42 @@
/*
* Aurora Store
* Copyright (C) 2021, Rahul Kumar Patel <whyorean@gmail.com>
*
* Aurora Store is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Aurora Store is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.aurora.extensions
import android.content.Context
import android.content.Intent
import android.os.Bundle
inline fun <reified T : Context> Context.newIntent(): Intent =
Intent(this, T::class.java)
inline fun <reified T : Context> Context.newIntent(flags: Int): Intent {
val intent = newIntent<T>()
intent.flags = flags
return intent
}
inline fun <reified T : Context> Context.newIntent(extras: Bundle): Intent =
newIntent<T>(0, extras)
inline fun <reified T : Context> Context.newIntent(flags: Int, extras: Bundle): Intent {
val intent = newIntent<T>(flags)
intent.putExtras(extras)
return intent
}

View File

@@ -33,8 +33,6 @@ import androidx.fragment.app.Fragment
import com.aurora.Constants
import com.aurora.store.R
import com.aurora.store.util.CommonUtil
import com.aurora.store.util.ViewUtil
fun Fragment.applyTheme(
themeId: Int,
@@ -140,8 +138,7 @@ private fun AppCompatActivity.setLightStatusBar() {
private fun AppCompatActivity.setLightNavigationBar() {
var flags = window.decorView.systemUiVisibility
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
window.navigationBarColor =
ViewUtil.getStyledAttribute(this, android.R.attr.colorBackground)
window.navigationBarColor = getStyledAttributeColor(android.R.attr.colorBackground)
flags = flags or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
window.decorView.systemUiVisibility = flags

View File

@@ -25,17 +25,10 @@ import android.os.Looper
fun runAsync(action: () -> Unit) = Thread(Runnable(action)).start()
fun runOnUiThread(action: () -> Unit) {
if (isMainLooperAlive()) {
action()
} else {
Handler(Looper.getMainLooper()).post(Runnable(action))
when {
isMainThread() -> action.invoke()
else -> Handler(Looper.getMainLooper()).post(Runnable(action))
}
}
fun runDelayed(delayMillis: Long, action: () -> Unit) =
Handler().postDelayed(Runnable(action), delayMillis)
fun runDelayedOnUiThread(delayMillis: Long, action: () -> Unit) =
Handler(Looper.getMainLooper()).postDelayed(Runnable(action), delayMillis)
private fun isMainLooperAlive() = Looper.myLooper() == Looper.getMainLooper()
private fun isMainThread() = Looper.myLooper() == Looper.getMainLooper()

View File

@@ -21,16 +21,28 @@ package com.aurora.extensions
import android.content.Context
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
fun Context.toast(text: CharSequence): Toast =
Toast.makeText(this, text, Toast.LENGTH_SHORT).apply { show() }
fun Context.longToast(text: CharSequence): Toast =
Toast.makeText(this, text, Toast.LENGTH_LONG).apply { show() }
fun Context.toast(@StringRes resId: Int): Toast =
fun Context.toast(resId: Int) = runOnUiThread {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).apply { show() }
}
fun Context.longToast(@StringRes resId: Int): Toast =
fun Context.toast(text: CharSequence) = runOnUiThread {
Toast.makeText(this, text, Toast.LENGTH_SHORT).apply { show() }
}
fun Context.toastLong(resId: Int) = runOnUiThread {
Toast.makeText(this, resId, Toast.LENGTH_LONG).apply { show() }
}
fun Context.toastLong(text: CharSequence) = runOnUiThread {
Toast.makeText(this, text, Toast.LENGTH_LONG).apply { show() }
}
fun Fragment.toast(resId: Int) = requireContext().toast(resId)
fun Fragment.toast(text: CharSequence) = requireContext().toast(text)
fun Fragment.toastLong(resId: Int) = requireContext().toastLong(resId)
fun Fragment.toastLong(text: CharSequence) = requireContext().toastLong(text)