Skip to content

Commit

Permalink
Include new components, extensions and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Aug 24, 2021
1 parent 193a774 commit 24a556b
Show file tree
Hide file tree
Showing 36 changed files with 1,326 additions and 180 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

android {
compileSdk 30
compileSdk 31

defaultConfig {
applicationId "com.jeluchu.compposecomponents"
Expand Down Expand Up @@ -58,7 +58,7 @@ dependencies {
implementation 'androidx.compose.runtime:runtime:1.0.1'
implementation 'androidx.compose.runtime:runtime-livedata:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02'
implementation 'androidx.navigation:navigation-compose:2.4.0-alpha06'
implementation 'androidx.navigation:navigation-compose:2.4.0-alpha07'
implementation 'androidx.activity:activity-compose:1.3.1'

}
23 changes: 23 additions & 0 deletions app/src/main/java/com/jeluchu/compposecomponents/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.runtime.Composable
Expand All @@ -15,7 +17,11 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.jeluchu.jchucomponentscompose.core.extensions.context.shortToast
import com.jeluchu.jchucomponentscompose.ui.animations.animateItem
import com.jeluchu.jchucomponentscompose.ui.cards.StoryCard
import com.jeluchu.jchucomponentscompose.ui.images.DoubleTapAnimation
import com.jeluchu.jchucomponentscompose.ui.images.NetworkImage

Expand All @@ -37,9 +43,26 @@ class MainActivity : ComponentActivity() {

@Composable
fun PhotoSelector() {

val context = LocalContext.current

val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {

LazyRow {
items(numbers) { item ->
StoryCard(
modifier = Modifier.animateItem(),
title = item.toString(),
iconMainUrl = "https://i.picsum.photos/id/1003/1181/1772.jpg?hmac=oN9fHMXiqe9Zq2RM6XT-RVZkojgPnECWwyEF1RvvTZk",
circleImage = R.drawable.ic_btnfavourite,
navigateToScreen = { context.shortToast("Clicked!") }
)
}
}

if (imageUriState.value != null) {

NetworkImage(
Expand Down
17 changes: 10 additions & 7 deletions jchucomponentscompose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ plugins {

android {

compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 31
consumerProguardFiles "consumer-rules.pro"
}

Expand Down Expand Up @@ -56,11 +56,14 @@ dependencies {
implementation 'androidx.compose.runtime:runtime:1.0.1'
implementation 'androidx.compose.runtime:runtime-livedata:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02'
implementation 'androidx.navigation:navigation-compose:2.4.0-alpha06'
implementation 'androidx.navigation:navigation-compose:2.4.0-alpha07'

// FIREBASE LIBRARY ----------------------------------------------------------------------------
implementation("com.google.firebase:firebase-analytics-ktx:19.0.1")

// ACCOMPANIST GOOGLE LIBRARY ------------------------------------------------------------------
implementation 'com.google.accompanist:accompanist-systemuicontroller:0.16.1'
implementation 'com.google.accompanist:accompanist-navigation-animation:0.16.1'
implementation 'com.google.accompanist:accompanist-systemuicontroller:0.17.0'
implementation 'com.google.accompanist:accompanist-navigation-animation:0.17.0'

// ANDROIDX LIBRARY ----------------------------------------------------------------------------
implementation 'androidx.core:core-ktx:1.6.0'
Expand All @@ -69,7 +72,7 @@ dependencies {

// GOOGLE LIBRARY ------------------------------------------------------------------------------
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.google.code.gson:gson:2.8.8'

// SQUAREUP LIBRARY ----------------------------------------------------------------------------
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
Expand All @@ -88,7 +91,7 @@ afterEvaluate {
from components.release
groupId = "com.jeluchu"
artifactId = "jchucomponentscompose"
version = "0.0.6"
version = "0.1.0"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions jchucomponentscompose/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jeluchu.jchucomponentscompose">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.jeluchu.jchucomponentscompose.core.extensions.context

import android.annotation.SuppressLint
import android.app.Activity
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ClipData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.text.DecimalFormat

fun Int.Companion.empty() = 0
fun Int?.orEmpty() = this ?: Int.empty()
fun Int.isNotEmpty() = this != Int.empty()
fun Long.bytesToMeg(): String = (this / (1024L * 1024L)).toString()

fun Int.fixedDecimalsTime(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.jeluchu.jchucomponentscompose.core.extensions.notifications

import android.annotation.SuppressLint
import android.app.*
import android.content.Context
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat

/**
* NotificationExtensions.kt
* is a class that contain all extension functions
* to help us easy to create and update notification
* from anywhere that has context object.
*/

/**
* Create notification group
*/
fun Context.createNotificationChannelGroup(@StringRes groupId: Int, @StringRes groupName: Int) {
val id = this.getString(groupId)
val name = this.getString(groupName)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = this.getSystemService(NotificationManager::class.java)
notificationManager?.createNotificationChannelGroup(NotificationChannelGroup(id, name))
}
}

/**
* Get notification channel
* @param channelId
* @return [NotificationChannel]
*/
fun Context.getNotificationChannel(@StringRes channelId: Int): NotificationChannel? {
return NotificationManagerCompat.from(this).getNotificationChannel(this.getString(channelId))
}

/**
* Get notification channel group
* @param groupId
* @return [NotificationChannelGroup]
*/
fun Context.getNotificationChannelGroup(@StringRes groupId: Int): NotificationChannelGroup? {
return NotificationManagerCompat.from(this).getNotificationChannelGroup(this.getString(groupId))
}

/**
* Create notification channel
*/
fun Context.createNotificationChannel(
@StringRes channelId: Int,
@StringRes channelName: Int,
@StringRes channelDescription: Int,
@SuppressLint("InlinedApi") importance: Int = NotificationManager.IMPORTANCE_DEFAULT
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val id = this.getString(channelId)
val name = this.getString(channelName)
val descriptionText = this.getString(channelDescription)
val channel = NotificationChannel(id, name, importance).also {
it.description = descriptionText
if (importance >= NotificationManager.IMPORTANCE_HIGH) {
it.enableVibration(true)
it.enableLights(true)
} else if (importance < NotificationManager.IMPORTANCE_DEFAULT) {
it.enableVibration(false)
it.enableLights(false)
it.setSound(null, null)
}
}
val notificationManager = this.getSystemService(NotificationManager::class.java)
notificationManager?.createNotificationChannel(channel)
}
}

/**
* Build a notification
*/
fun Context.getNotificationBuilder(
@StringRes channelId: Int,
title: Int,
text: Int,
@DrawableRes icon: Int,
@StringRes groupId: Int,
importance: Int = NotificationCompat.PRIORITY_DEFAULT,
pendingIntent: PendingIntent? = null
): NotificationCompat.Builder =
NotificationCompat.Builder(this, this.getString(channelId))
.setSmallIcon(icon)
.setContentTitle(this.getString(title))
.setContentText(this.getString(text))
.setPriority(importance)
.setContentIntent(pendingIntent)
.setGroup(this.getString(groupId))

/**
* Update notification
*/
fun Context.notifyNotification(notificationId: Int, notification: Notification) =
NotificationManagerCompat.from(this).notify(notificationId, notification)

/**
* Cancel notification base on notification id
*
* @param notificationId id to cancel
*
* Note:
* - if [notificationId] is empty or default, this method
* will cancel all notifications
*/
fun Context.cancelNotification(vararg notificationId: Int = intArrayOf()) {
if (notificationId.isEmpty()) {
NotificationManagerCompat.from(this).cancelAll()
return
}
notificationId.forEach {
NotificationManagerCompat.from(this).cancel(it)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jeluchu.jchucomponentscompose.core.extensions.retrofit

import com.jeluchu.jchucomponentscompose.core.functional.Either
import com.jeluchu.jchucomponentscompose.core.exception.Failure
import com.jeluchu.jchucomponentscompose.core.functional.Either
import retrofit2.Call

fun <T, R> request(
Expand All @@ -14,13 +14,12 @@ fun <T, R> request(
when (response.isSuccessful) {
true -> Either.Right(transform((response.body() ?: default)))
false -> {

when (response.code()) {
StatusCode.InternalServerError.code -> Either.Left(Failure.ServerError)
StatusCode.BadGateway.code -> Either.Left(Failure.ServerError)
StatusCode.NotFound.code -> Either.Left(Failure.ServerError)
else -> Either.Left(Failure.ServerError)
}

}
}
} catch (exception: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.util.Base64
import androidx.compose.ui.graphics.Color
import org.intellij.lang.annotations.RegExp
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
Expand All @@ -18,6 +19,28 @@ import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

fun String.getLastBitFromUrl(): String = replaceFirst(".*/([^/?]+).*".toRegex(), "$1")

fun String.saveImage(destinationFile: File) {
try {
Thread {
val url = URL(this)
val inputStream = url.openStream()
val os = FileOutputStream(destinationFile)
val b = ByteArray(2048)
var length: Int
while (inputStream.read(b).also { length = it } != -1) {
os.write(b, 0, length)
}
inputStream?.close()
os.close()
}.start()

} catch (e: Exception) {
e.printStackTrace()
}
}

/** ---- COMPOSE FUNCTIONS --------------------------------------------------------------------- **/

fun String.getColor() = Color(android.graphics.Color.parseColor(this))
Expand Down Expand Up @@ -71,12 +94,6 @@ fun String?.compareDate(): Boolean {

/** ---- CHECKER ------------------------------------------------------------------------------- **/

val String.isPhone: Boolean get() = matches("^1([34578])\\d{9}\$".toRegex())
val String.isEmail: Boolean get() = matches("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)\$".toRegex())
val String.isNumeric: Boolean get() = matches("^[0-9]+$".toRegex())
val String.isAlphanumeric get() = matches("^[a-zA-Z0-9]*$".toRegex())
val String.isAlphabetic get() = matches("^[a-zA-Z]*$".toRegex())
fun String.isLocal() = !isEmptyString() && (startsWith("http://") || startsWith("https://"))

fun CharSequence.isEmptyString(): Boolean = this.isEmpty() || this.toString().equals("null", true)
fun CharSequence.isDigitOnly(): Boolean = (0 until length).any { Character.isDigit(this[it]) }
Expand All @@ -100,11 +117,13 @@ fun String.atLeastOneNumber(): Boolean = !matches(Regex(".*\\d.*"))
fun String.startsWithNonNumber(): Boolean = Character.isDigit(this[0])
fun String.noSpecialCharacter(): Boolean = !matches(Regex("[A-Za-z0-9]+"))
fun String.atLeastOneSpecialCharacter(): Boolean = matches(Regex("[A-Za-z0-9]+"))
fun Any.readResourceText(resource: String): String? = this.javaClass.classLoader?.getResource(resource)?.readText()
fun Any.readResourceText(resource: String): String? =
this.javaClass.classLoader?.getResource(resource)?.readText()

inline val String.isIp: Boolean
get() {
val p = Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")
val p =
Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")
val m = p.matcher(this)
return m.find()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jeluchu.jchucomponentscompose.core.extensions.uri

import android.content.ContentResolver
import android.database.Cursor
import android.net.Uri
import android.provider.OpenableColumns
import java.io.IOException

fun Uri.getFileName(contentResolver: ContentResolver): String {
var result: String? = null
if (scheme == "content") {
val cursor: Cursor? = contentResolver.query(this, null, null, null, null)
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
}
} catch (e: IOException) {
e.printStackTrace()
cursor?.close()
}
}
if (result == null) {
result = path
val cut = result!!.lastIndexOf('/')
if (cut != -1) {
result = result.substring(cut + 1)
}
}
return result
}
Loading

0 comments on commit 24a556b

Please sign in to comment.