Skip to content

Instantly share code, notes, and snippets.

View iambaljeet's full-sized avatar

Baljeet Singh iambaljeet

View GitHub Profile
@iambaljeet
iambaljeet / EnumJsonAdapter.kt
Created January 4, 2022 12:07
Custom Adapter for setting default value for enums insude API response.
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.annotations.SerializedName
import java.lang.reflect.Type
class EnumJsonAdapter<T : Enum<T>>(private val defaultValue: Enum<T>) : JsonDeserializer<Enum<T>> {
override fun deserialize(
json: JsonElement?,
WorkManager.getInstance(this)
.beginUniqueWork("ForegroundWorker", ExistingWorkPolicy.APPEND_OR_REPLACE,
OneTimeWorkRequest.from(ForegroundWorker::class.java)).enqueue()
class ForegroundWorker(context: Context, parameters: WorkerParameters) :
CoroutineWorker(context, parameters) {
private val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
override suspend fun doWork(): ListenableWorker.Result = withContext(Dispatchers.IO) {
setForeground(createForegroundInfo())
return@withContext runCatching {
runTask()
//Fake long running task for 60 seconds
private suspend fun runTask() {
delay(60000)
}
//Creates notifications for service
private fun createForegroundInfo(): ForegroundInfo {
val id = "1225"
val channelName = "Downloads Notification"
val title = "Downloading"
val cancel = "Cancel"
val body = "Long running task is running"
val intent = WorkManager.getInstance(applicationContext)
class ForegroundWorker(context: Context, parameters: WorkerParameters) :
CoroutineWorker(context, parameters) {
override suspend fun doWork(): Result {
TODO("Not yet implemented")
}
}
/**
* Touch listener to detect when a user touches the ArScene plane to place a model
*/
arFragment.setOnTapArPlaneListener { hitResult, plane, motionEvent ->
setModelOnUi(hitResult)
}
/**
* Used to load model and set it on ArScene where a user Taps
*/
private fun setModelOnUi(hitResult: HitResult) {
loadModel(R.raw.model) { modelRenderable ->
//Used to get anchor point on scene where user tapped
val anchor = hitResult.createAnchor()
//Created an anchor node to attach the anchor with its parent
val anchorNode = AnchorNode(anchor)
//Added arSceneView as parent to the anchorNode. So our anchors will bind to arSceneView.
arFragment = fragment as ArFragment
/**
* Touch listener to detect when a user touches the ArScene plane to place a model
*/
arFragment.setOnTapArPlaneListener { hitResult, plane, motionEvent ->
}
/**
* Used to laod models from 'raw' with a callback when loading is complete
*/
fun loadModel(@RawRes model: Int, callback: (ModelRenderable) -> Unit) {
ModelRenderable
.builder()
.setSource(this, model)
.build()
.thenAccept { modelRenderable ->
callback(modelRenderable)