Skip to content

Instantly share code, notes, and snippets.

View ahmedMubarak2024's full-sized avatar

Ahmed Mostafa Mubarak ahmedMubarak2024

  • Vodafone V.O.I.S
  • Egypt
View GitHub Profile
jar {
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
you may need this if you get errors
tasks.withType(Jar) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
@ahmedMubarak2024
ahmedMubarak2024 / CoroutineRuleToSwapMainDispatcherWithTestDispatcher.kt
Created July 28, 2022 11:03
CoroutineRule To swap Dispatcher.Main with TestCoroutineDispatcher used in Unit Testing
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
/**
@ahmedMubarak2024
ahmedMubarak2024 / ViewModelFactory.kt
Created July 28, 2022 08:47
simple view model factory
@Suppress("UNCHECKED_CAST")
class TasksViewModelFactory (/*pass the pram you need*/ ) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>) =
(ViewModel(/*pass the pram to view model*/) as T)
}
@ahmedMubarak2024
ahmedMubarak2024 / checkLocationTurnOn.kt
Last active July 27, 2022 10:20
check on location services is turn on if not show native dialog to user that can enable the location service
class LocationActivity:AppCompatActivity() {
private val REQUEST_TURN_DEVICE_LOCATION_ON = 29
private val TAG = this::class.java.simpleName
private fun checkDeviceLocationSettings(resolve:Boolean = true, context:AppCompatActivity) {
val locationRequest = LocationRequest.create().apply {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
val settingsClient = LocationServices.getSettingsClient(context)
AsyncTask.execute(()->{
try {
AdvertisingIdClient.Info idInfo = null;
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
System.out.println(idInfo.getId());
} catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
}
);