Skip to content

Instantly share code, notes, and snippets.

@yusufceylan
yusufceylan / get_user_data_changes.kt
Last active October 8, 2020 13:20
Cloud DB live data changes
@ExperimentalCoroutinesApi
suspend fun getUserDataChanges(id : String?) : Flow<Resource<User>> = withContext(ioDispatcher) {
callbackFlow {
if (id == null) {
offer(Resource.Error(Exception("Id must not be null")))
return@callbackFlow
}
// 1- Create query
@yusufceylan
yusufceylan / cloud_db_subscription.kt
Last active October 8, 2020 12:40
Cloud Db Subscription
val subscription = cloudDBZone.subscribeSnapshot(query, CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_PRIOR,
object : OnSnapshotListener<User> {
override fun onSnapshot(snapShot: CloudDBZoneSnapshot<User>?, error: AGConnectCloudDBException?) {
// do something
}
})
@yusufceylan
yusufceylan / get_user_data.kt
Created October 8, 2020 11:08
One shot cloud db request with callback flow
@ExperimentalCoroutinesApi
suspend fun getUserData(id : String?) : Flow<Resource<User>> = withContext(ioDispatcher) {
callbackFlow {
if (id == null) {
offer(Resource.Error(Exception("Id must not be null")))
return@callbackFlow
}
// 1- Create query
@yusufceylan
yusufceylan / CustomInfoViewAdapter.kt
Created September 23, 2020 12:50
Custom Info View Adapter
class CustomInfoViewAdapter constructor(context : Context) : HuaweiMap.InfoWindowAdapter {
private var mWindow: View? = null
init {
mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window_layout, null)
}
override fun getInfoWindow(marker: Marker?): View? {
@yusufceylan
yusufceylan / info_view_layout.xml
Created September 23, 2020 12:44
Custom Info View Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:background="@android:color/white">
@yusufceylan
yusufceylan / AddMarker.kt
Last active September 23, 2020 13:15
Add Simple Marker
/**
* Initialize dummy markers
*/
private fun initMarkers() {
val markerDataList = createMarkerList()
markerDataList.forEachIndexed { index, markerOptions ->
val marker = hMap?.addMarker(markerOptions)
marker?.setMarkerAnchor(0.5f, 1f) // Set marker anchor point
marker?.tag = "$index Extra Info" // Set extra data with tag. This data can be a custom class
}
@yusufceylan
yusufceylan / BasicHuaweiMap.kt
Created September 23, 2020 09:05
Basic Huawei Map
class MainActivity : AppCompatActivity(), OnMapReadyCallback {
private var hMap: HuaweiMap? = null
companion object {
private const val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@yusufceylan
yusufceylan / OpenCameraAndSavePhoto.kt
Last active February 6, 2023 03:18
Take photo from camera and save it to gallery
// Take photo from camera and save it to public gallery
// Before Q and After Q implementations
// Answer riginally taken from this SO answer
// https://stackoverflow.com/a/59482148/5695091
private val REQUEST_TAKE_PHOTO = 101
private val REQUEST_PICK_PHOTO = 102
private var photoURI : Uri? = null
@yusufceylan
yusufceylan / ActivityResult.kt
Created September 8, 2020 10:34
ActivityResult
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CHECK_SETTINGS) {
when (resultCode) {
Activity.RESULT_OK -> {
LogUtils.d("User confirm to access location")
}
Activity.RESULT_CANCELED -> {
LogUtils.d("User denied to access location")
@yusufceylan
yusufceylan / LocationPermissions.kt
Created September 8, 2020 10:32
Location Permissions
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q");
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions(this, strings, 1);
}