Skip to content

Instantly share code, notes, and snippets.

View oussama-dz's full-sized avatar

CHERFAOUI Oussama oussama-dz

View GitHub Profile
@oussama-dz
oussama-dz / ImagePicker.kt
Last active June 22, 2023 07:15
Integrating local storage with the image picker.
@Composable
fun ImagePicker() {
var imageUri: Uri? by remember { mutableStateOf(null) }
val context = LocalContext.current
val dataStore = remember { StoreData() }
val scope = rememberCoroutineScope()
val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.OpenDocument()) {
it?.let { uri ->
context.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION )
@oussama-dz
oussama-dz / StoreData.kt
Last active June 23, 2023 08:23
A class for handling the storage of the image URI using datastore.
class StoreData {
private val Context.storeData: DataStore<Preferences> by preferencesDataStore(name = "data")
suspend fun storeImage(context: Context, value: String) {
context.storeData.edit { preferences ->
preferences[stringPreferencesKey("image")] = value
}
}
suspend fun getImage(context: Context): Flow<String?> {
return context.storeData.data.map {
preferences ->
@oussama-dz
oussama-dz / ImagePicker.kt
Created June 20, 2023 08:16
basic image picker using kotlin and jetpack compose
@Composable
fun ImagePicker() {
var imageUri: Uri? by remember {
mutableStateOf(null)
}
val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.OpenDocument()) {
it?.let { uri ->
imageUri = uri
}
@oussama-dz
oussama-dz / MapScreen.kt
Last active June 18, 2023 15:47
Handle the user interaction with the map.
@Composable
fun MapScreen() {
var point: Point? by remember {
mutableStateOf(null)
}
var relaunch by remember {
mutableStateOf(false)
}
val context = LocalContext.current
val permissionRequest = rememberLauncherForActivityResult(
@oussama-dz
oussama-dz / MapBoxMap.kt
Last active June 18, 2023 16:16
Handle user interaction with the mapbox map.
@Composable
fun MapBoxMap(
modifier: Modifier = Modifier,
onPointChange: (Point) -> Unit,
point: Point?,
) {
val context = LocalContext.current
val marker = remember(context) {
context.getDrawable(R.drawable.marker)!!.toBitmap()
}
@oussama-dz
oussama-dz / MapScreen.kt
Last active June 18, 2023 15:46
Show the current user's location on the map using MapBoxComposable.
@Composable
fun MapScreen() {
var point: Point? by remember {
mutableStateOf(null)
}
var relaunch by remember {
mutableStateOf(false)
}
val context = LocalContext.current
@oussama-dz
oussama-dz / MapScreen.kt
Created June 18, 2023 07:38
The usage of the MapBoxMap composable.
@Composable
fun MapScreen() {
Column(
modifier = Modifier.fillMaxSize(),
) {
MapBoxMap(
point = Point.fromLngLat(-0.6333, 35.6971),
modifier = Modifier
.fillMaxSize()
)
@oussama-dz
oussama-dz / MapBoxMap.kt
Last active June 18, 2023 16:17
Mapbox composable using the AndroidView composable.
@Composable
fun MapBoxMap(
modifier: Modifier = Modifier,
point: Point?,
) {
val context = LocalContext.current
val marker = remember(context) {
context.getDrawable(R.drawable.marker)!!.toBitmap()
}
var pointAnnotationManager: PointAnnotationManager? by remember {
@oussama-dz
oussama-dz / settings.gradle.GRADLE
Created June 18, 2023 06:51
Mapbox downloads API's declaration
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
@oussama-dz
oussama-dz / MainActivity.kt
Created May 31, 2023 08:09
Calling the get current location composable in the main activity.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GetCurrentLocationUI()
}
}
}