Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rahbadev committed Jun 18, 2023
0 parents commit 63a2f39
Show file tree
Hide file tree
Showing 46 changed files with 1,267 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions BtechUtils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
65 changes: 65 additions & 0 deletions BtechUtils/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apply plugin: 'com.android.library'

apply plugin: 'org.jetbrains.kotlin.android'

apply plugin: 'maven-publish'

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
}
}
}
}

android {
namespace 'com.rhdev.btechutils'
compileSdk 33

defaultConfig {
minSdk 22
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// Toasty
implementation 'com.github.GrenderG:Toasty:1.5.2'

// in app review
implementation 'com.google.android.play:review:2.0.1'

// in app updates
implementation 'com.google.android.play:app-update:2.1.0'

implementation 'androidx.preference:preference-ktx:1.2.0'


}
Empty file added BtechUtils/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions BtechUtils/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.rhdev.btechutils

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.rhdev.btechutils.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions BtechUtils/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
118 changes: 118 additions & 0 deletions BtechUtils/src/main/java/com/rhdev/btechutils/DarkMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.rhdev.btechutils

import android.content.Context
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder

object DarkMode {

private val TAG = "P_TAG : " + DarkMode::class.simpleName
private const val APP_DARK_MODE_SP_KEY = "app_dark_mode_sp_key"

private val uiModeMap = mapOf(
"نهاري" to AppCompatDelegate.MODE_NIGHT_NO,
"ليلي" to AppCompatDelegate.MODE_NIGHT_YES,
"تلقائي" to AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM,
)


private fun setAppDarkModeValue(context: Context, mode: Int) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
sharedPreferences.edit().putInt(APP_DARK_MODE_SP_KEY, mode).apply()
}

private fun getAppDarkModeValue(context: Context): Int {
val sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
return sharedPreferences.getInt(
APP_DARK_MODE_SP_KEY,
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
)
}

fun applyAppModeToUI(context: Context) {
AppCompatDelegate.setDefaultNightMode(getAppDarkModeValue(context))
}

private fun applyAppModeToUI(mode: Int) {
AppCompatDelegate.setDefaultNightMode(mode)
}

// Function to check if the current theme is dark
fun isDarkTheme(context: Context): Boolean {
return when (getAppDarkModeValue(context)) {
AppCompatDelegate.MODE_NIGHT_YES -> true
AppCompatDelegate.MODE_NIGHT_NO -> false
else -> {
val currentNightMode =
context.applicationContext.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
currentNightMode == Configuration.UI_MODE_NIGHT_YES
}
}
}

fun showUiModeDialog(context: Context) {

val currentSelectedMode = getPositionByValue(getAppDarkModeValue(context))
var checkedItem = currentSelectedMode

MaterialAlertDialogBuilder(context)
.setTitle("اختيار المظهر")
.setSingleChoiceItems(uiModeMap.keys.toTypedArray(), checkedItem) { _, which ->
checkedItem = which
}
.setNeutralButton("موافق") { _, _ ->
if (currentSelectedMode != checkedItem) {
val selectedMode = getValueByPosition(checkedItem)
setAppDarkModeValue(context, selectedMode)
applyAppModeToUI(context)
}
}
.setPositiveButton("الغاء") { _, _ ->

}

.show()
}

private fun getValueByPosition(position: Int): Int {
uiModeMap.values.toIntArray().forEachIndexed { index, i ->
if (index == position) {
return i
}
}
return -1
}

private fun getKeyByPosition(position: Int): String {
uiModeMap.keys.toTypedArray().forEachIndexed { index, i ->
if (index == position) {
return i
}
}
return ""
}

private fun getPositionByValue(value: Int): Int {
uiModeMap.values.forEachIndexed { index, i ->
if (value == i) {
return index
}
}
return -1
}

private fun getPositionByKey(map: Map<String, Int>, key: String): Int {
map.keys.forEachIndexed { index, i ->
if (key == i) {
return index
}
}
return -1
}

}


116 changes: 116 additions & 0 deletions BtechUtils/src/main/java/com/rhdev/btechutils/StoreUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.rhdev.btechutils

import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.GET_META_DATA
import android.net.Uri
import android.os.Build
import android.util.Log
import com.google.android.gms.tasks.Task
import com.google.android.play.core.appupdate.AppUpdateInfo
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory

object StoreUtils {

private val TAG = "BtechUtils : " + StoreUtils::class.simpleName

fun openDevGooglePlayAccount(context: Context) {
val applicationContext = context.applicationContext
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("https://play.google.com/store/apps/dev?id=6736668226615202750")
setPackage("com.android.vending")
flags = Intent.FLAG_ACTIVITY_NEW_TASK

}
applicationContext.startActivity(intent)
}

fun checkAppUpdate(context: Context, callback: (Boolean) -> Unit) {
val applicationContext = context.applicationContext
val appUpdateManager: AppUpdateManager = AppUpdateManagerFactory.create(applicationContext)
val appUpdateInfoTask: Task<AppUpdateInfo> = appUpdateManager.appUpdateInfo

val currentVersionCode = getAppVersionCode(applicationContext)

Log.v(TAG, "checkAppUpdate: currentVersionCode = $currentVersionCode")

appUpdateInfoTask
.addOnSuccessListener { appUpdateInfo ->
Log.v(
TAG,
"onSuccess() called with: availableVersionCode = [" + appUpdateInfo?.availableVersionCode() + "]"
)
appUpdateInfo?.let {
val availableVersionCode = appUpdateInfo.availableVersionCode()

callback((currentVersionCode > -1 && availableVersionCode > currentVersionCode))

} ?: kotlin.run {
callback(false)
}

}
.addOnFailureListener { exception ->
Log.w(TAG, "checkAppUpdate() addOnFailureListener called", exception)
}
.addOnCompleteListener { task ->
Log.d(TAG, "checkAppUpdate() addOnCompleteListener called with: task = $task")
}
.addOnCanceledListener {
Log.d(TAG, "checkAppUpdate() addOnCanceledListener called")
}
}

private fun getAppVersionCode(applicationContext: Context): Int {
var versionCode = -1
val packageName = applicationContext.packageName ?: return versionCode
val packageManager = applicationContext.packageManager ?: return versionCode
val packageInfo: PackageInfo = try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getPackageInfo(
packageName,
PackageManager.PackageInfoFlags.of(GET_META_DATA.toLong() or PackageManager.GET_ACTIVITIES.toLong())
)
} else {
@Suppress("DEPRECATION")
packageManager.getPackageInfo(
packageName,
PackageManager.GET_ACTIVITIES
)
}

} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, "getAppVersionCode: ", e)
null
} ?: return versionCode

versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.longVersionCode.toInt()
} else {
@Suppress("DEPRECATION") packageInfo.versionCode
}

return versionCode
}


fun openAppPageOnGooglePlay(context: Context) {
val applicationContext = context.applicationContext
val appPackageName: String = context.packageName
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(
"https://play.google.com/store/apps/details?id=$appPackageName"
)
setPackage("com.android.vending")
flags = Intent.FLAG_ACTIVITY_NEW_TASK

}

Log.d(TAG, "openAppPageOnGooglePlay() called with: url = ${intent.data}")

applicationContext.startActivity(intent)
}
}
Loading

0 comments on commit 63a2f39

Please sign in to comment.