().find().asFlow()
```
```
// ViewModel
val itemListLiveData = locationRepository.listFlow().map { it.list.toList() }.asLiveData()
```
---
# Guardar login
- Un cop fet el login l'usuari queda guardat a no haver de fer el login la seguent vegada que s'obri l'aplicació.
- Pots mirar si està loggejat amb:
```
fun loggedIn() = realmApp.currentUser?.loggedIn ?: false
```
---
# Splash screen
Pots usar una splash screen per a que a l'app et dirigeixi al login/register o al mapa
graph LR
Spash[Spash] --> Login[Login]
Spash[Spash] --> Map[Map]
---
# Arch
```
/**
* Contains all the operations with locations
*/
class LocationRepository(realm: Realm){
fun listFlow() : Flow> = TODO()
// More location functions
}
```
---
# Arch
```
/**
* Manages the realm app
*/
class RealmManager{
val realmApp = App.create(AppConfiguration.Builder("MY_APPLICATION_ID").log(LogLevel.ALL).build())
var realm : Realm? = null
fun loggedIn() = realmApp.currentUser?.loggedIn ?: false
suspend fun register(username: String, password: String): Boolean = TODO()
suspend fun login(username: String, password: String) : Boolean = TODO()
}
```
---
#Arch
```
/**
* Singleton with services
*/
object ServiceLocator {
val realmManager = RealmManager()
lateinit var locationRepository: LocationRepository
/**
* Call when user logged in and realm created
*/
fun configureRealm(){
requireNotNull(realmManager.realm)
val realm = realmManager.realm!!
locationRepository = LocationRepository(realm)
}
}
```
---
# Realm imatge
```
open class RealmModelWithPicture(
var picture: ByteArray? = null,
var owner_id: String = "",
@PrimaryKey
var _id: ObjectId = ObjectId.create(),
) : RealmObject {
// Declaring empty contructor for Realm
constructor() : this(owner_id = "") {}
}
```
---
# Android display Image
```
val bmp = BitmapFactory.decodeByteArray(location.picture, 0, location.picture!!.size)
val image = binding.image
image.setImageBitmap(bmp)
```
---
# Thumbnail
```
val thumbImage = ThumbnailUtils.extractThumbnail(
BitmapFactory.decodeFile(args.uri),
200,
200
)
val stream = ByteArrayOutputStream()
thumbImage.compress(Bitmap.CompressFormat.PNG, 100, stream)
val thumbByteArray: ByteArray = stream.toByteArray()
thumbImage.recycle()
```
---
# Exemple
https://gitlab.com/mateuyabar_at_itb/realmAndroidSample?authuser=0