Skip to content

Commit

Permalink
1.1.0 - New Extensions Added
Browse files Browse the repository at this point in the history
  • Loading branch information
thesarangal committed Sep 13, 2021
1 parent 14ddc8c commit b3086f0
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 16 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@

- Publish to jitpack
- Initial version

### Version 1.1.0
#### Date: September 13, 2021

- New Activity Extensions Added
setStatusBarColor
setNavigationBarColor
setSystemBarColor

- New Misc Extension Added
isColorDark

- New Graphics Extension Added
addGradient

- New String Extensions Added
haveAnyAlphabetNumber
isContainNumber

- New Validation Extensions Added
isPasswordValid
isValidPackageName
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ Done! The first time you request a project JitPack checks out the code, builds i
##### * shareTextMessage
##### * openDirectionInGoogleMap
##### * openLocationInGoogleMap
##### * openAddressInGoogleMap
##### * setStatusBarColor
##### * setNavigationBarColor
##### * setSystemBarColor

#### II. Context Extensions
##### * copyTextToClipboard
Expand All @@ -82,6 +84,7 @@ Done! The first time you request a project JitPack checks out the code, builds i
##### * modifyOrientation
##### * flip
##### * rotateImage
##### * addGradient

#### V. LOG Extensions
##### * logger
Expand Down Expand Up @@ -109,16 +112,21 @@ Done! The first time you request a project JitPack checks out the code, builds i
##### * getCurrencySymbol
##### * isContainEnglish
##### * isContainHindi
##### * haveAnyAlphabetNumber
##### * isContainNumber

#### VIII. Validation Extensions
##### * isValidIFSC
##### * isValidUPI
##### * isValidPANCard
##### * isStrongPassword
##### * isPhoneValid
##### * isPasswordValid
##### * isValidPackageName

#### IX. Misc Extensions
##### * mixTwoColors
##### * isColorDark



Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
buildscript {
ext.kotlin_version = "1.5.10"
ext.kotlin_version = "1.5.30"

repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
12 changes: 6 additions & 6 deletions kotlinsupplement/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ plugins {
}

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0.0"
targetSdkVersion 31
versionCode 2
versionName "1.1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down Expand Up @@ -42,10 +42,10 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.core:core-ktx:1.6.0'

/** ExifInterface: For Image Orientation */
implementation "androidx.exifinterface:exifinterface:1.3.2"
implementation "androidx.exifinterface:exifinterface:1.3.3"
}

allprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.view.View
import android.view.WindowInsetsController
import android.view.inputmethod.InputMethodManager
import androidx.annotation.DrawableRes
import androidx.annotation.RequiresApi
import kotlin.system.exitProcess


Expand Down Expand Up @@ -215,4 +218,140 @@ fun Activity.openAddressInGoogleMap(address: String) {
} else {
openURLInBrowser( "https://www.google.com/maps/place/$address/")
}
}

/**
* Set System Bar Color (StatusBar)
*
* @param statusBarColor For StatusBar Color
* @param view Parent View of Activity
*
* Makes status bars become opaque with solid dark background and light foreground.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun Activity.setStatusBarColor(statusBarColor: Int, view: View ?= null) {
window.statusBarColor = statusBarColor

val isColorDark = statusBarColor.isColorDark()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.setSystemBarsAppearance(
if (isColorDark) 0
else WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
return
}

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(view ?: window.decorView).systemUiVisibility = if(isColorDark) 0 else View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}

/**
* Set System Bar Color (NavigationBar)
*
* @param navigationBarColor For System NavigationBar Color
* @param view Parent View of Activity
*
* Makes status bars become opaque with solid dark background and light foreground.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun Activity.setNavigationBarColor(navigationBarColor: Int, view: View ?= null) {
window.navigationBarColor = navigationBarColor

val isColorDark = navigationBarColor.isColorDark()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.setSystemBarsAppearance(
if (isColorDark) 0
else WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
)
return
}

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

(view ?: window.decorView).apply {
systemUiVisibility = if (isColorDark) 0
else View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
}

/**
* Set System Bar Color (StatusBar & NavigationBar)
*
* @param statusBarColor For StatusBar Color
* @param navigationBarColor For System NavigationBar Color
* @param view Parent View of Activity
*
* Makes status bars become opaque with solid dark background and light foreground.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun Activity.setSystemBarColor(statusBarColor: Int,
navigationBarColor: Int,
view: View ?= null) {
window.statusBarColor = statusBarColor
window.navigationBarColor = navigationBarColor

val isColorDarkStatusBar = statusBarColor.isColorDark()
val isColorDarkNavigationBar = navigationBarColor.isColorDark()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.setSystemBarsAppearance(

when {
isColorDarkStatusBar && isColorDarkNavigationBar -> {
0
}

isColorDarkStatusBar -> {
WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
}

isColorDarkNavigationBar -> {
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
}

else -> {
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
}
},
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
)
return
}

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(view ?: window.decorView).systemUiVisibility = when {
isColorDarkStatusBar && isColorDarkNavigationBar -> {
0
}

isColorDarkStatusBar -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} else {
0
}
}

isColorDarkNavigationBar -> {
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}

else -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} else {
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package `in`.sarangal.kotlinsupplement

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.*
import android.graphics.drawable.Drawable
import android.net.Uri
import android.provider.MediaStore
Expand Down Expand Up @@ -131,3 +129,33 @@ fun rotateImage(img: Bitmap, degree: Int): Bitmap? {
}
}

/**
* @return Gradient Bitmap Image
*
* @param startColor Start Color
* @param endColor End Color
* */
fun Bitmap.addGradient(startColor: Int, endColor: Int): Bitmap {
val width = this.width
val height = this.height
val updatedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(updatedBitmap)
canvas.drawBitmap(this, 0F, 0F, null)
val paint = Paint()
if (startColor == endColor) {
paint.colorFilter = PorterDuffColorFilter(startColor, PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(this, 0F, 0F, paint)
} else {
val shader =
LinearGradient(
0F, 0F, 0F, height.toFloat(),
startColor, endColor, Shader.TileMode.CLAMP
)
paint.shader = shader
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawRect(0F, 0F, width.toFloat(), height.toFloat(), paint)
}

return updatedBitmap ?: this
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package `in`.sarangal.kotlinsupplement

import android.graphics.Color

/**
* @return Mixed Color from Two Colors
*
Expand All @@ -25,4 +27,10 @@ fun mixTwoColors(color1: Int, color2: Int, amount: Float = 0.5f): Int {
(r shl RED_CHANNEL.toInt()) or
(g shl GREEN_CHANNEL.toInt()) or
(b shl BLUE_CHANNEL.toInt())
}
}

/** Check COLOR is DARK or LIGHT */
fun Int.isColorDark(): Boolean =
(Color.red(this) * 0.299
+ Color.green(this) * 0.587
+ Color.blue(this) * 0.114) <= 187 //186
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package `in`.sarangal.kotlinsupplement
import android.graphics.Color
import java.text.DecimalFormat
import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
* String Extensions
Expand All @@ -23,8 +25,8 @@ fun String.lowerCase() = this.lowercase(Locale.getDefault())
*
* @param locale Locale
* */
fun String.capitalize(locale: Locale) = replaceFirstChar {
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
fun String.capitalize(locale: Locale ?= null) = replaceFirstChar {
if (it.isLowerCase()) it.titlecase(locale ?: Locale.getDefault()) else it.toString()
}

/**
Expand Down Expand Up @@ -141,4 +143,21 @@ fun String.isContainEnglish(): Boolean {
}
}
return isEnglish
}

/**
* @return TRUE if String contains any Alphabet or Number else FALSE
* */
fun String.haveAnyAlphabetNumber(): Boolean {
val pattern = Pattern.compile("[a-zA-Z0-9]")
val matcher: Matcher = pattern.matcher(this)
return matcher.find()
}

/**
* @return TRUE if STRING is contain any number
* */
fun String.isContainNumber(): Boolean {
val CONTAIN_NUMBER = ".*\\d.*"
return Pattern.compile(CONTAIN_NUMBER).matcher(this.trim()).matches()
}
Loading

0 comments on commit b3086f0

Please sign in to comment.