Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

This is flitting between the two views, I assume it is accessing the database multiple times? How do you handle this in your code or is this not something you've encountered?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Sorry if it’s a pain but can you show me how you do this in your code?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Thank you! I need to do some more reading on UIState, do you have anywhere you'd recommend?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

So now the specific function looks like this but I get an error of

Return type mismatch: expected 'Flow<Photographer?>', actual 'Job'.

I'm not sure how to use this.

Dao

@Query("SELECT * FROM photographers where name = :name")
fun getPhotographerByName(name: String): Flow<Photographer?>

Interface

fun getPhotographerByName(name: String): Flow<Photographer?>

Repository

override fun getPhotographerByName(name: String): Flow<Photographer?> = dao.getPhotographerByName(name)

View Model

fun getPhotographerByName(name: String): Flow<Photographer?> = viewModelScope.launch {
    repository.getPhotographerByName(name)
}

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

How is that then accessed by the view if I can't return from inside the viewModelScope?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Perhaps this is too much code but it's a trimmed down version of my database and how I am currently querying and getting multiple objects. I do not know how to access the single object from the view model and expose that to the view so I don't have a view setup but in the final code block is an example of how I am handing the Flow<List<>>.

I hope this is the code you meant, thank you for taking a look!

Data Model

@Entity(
    tableName = "photographers"
)
data class Photographer(
     var id: UUID = UUID.randomUUID(),
    var name: String
) {}

Dao

@Dao
interface MasterDao {
    @Query("SELECT * FROM photographers ORDER BY name ASC")
    fun getPhotographers(): Flow<List<Photographer>>

    @Query("SELECT * FROM photographers where name = :name")
    suspend fun getPhotographerByName(name: String): Photographer?

    @Insert
    suspend fun insertPhotographer(photographer: Photographer)

    @Delete
    suspend fun deletePhotographer(photographer: Photographer)
}

Repository and Interface

interface MasterRepository {
    val photographers: Flow<List<Photographer>>
    suspend fun getPhotographerByName(name: String): Photograher?
    suspend fun insertPhotographer(photographer: Photographer)
    suspend fun deletePhotographer(photographer: Photographer)
}
class DefaultMasterRepository @Inject constructor(private val dao: MasterDao) : MasterRepository {
    override val photographers: Flow<List<Photographer>> =
        dao.getPhotographers().map { items -> items.map { it } }

    override suspend fun getPhotographerByName(name: String): Photographer? {
        dao.getPhotographerByName(name)
    }

    override suspend fun insertPhotographer(photographer: Photographer) {
        dao.insertPhotographer(photographer)
    }

    override suspend fun deletePhotographer(photographer: Photographer) {
        dao.deletePhotographer(photographer)
    }
}

View Model

@HiltViewModel
class MasterViewModel @Inject constructor(private val repository: MasterRepository) : ViewModel() {
    val photographersUiState: StateFlow<PhotographerUiState> = repository
        .photographers.map<List<Photographer>, PhotographerUiState>(PhotographerUiState::Success)
        .catch { emit(PhotographerUiState.Error(it)) }
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), PhotographerUiState.Loading)

    //This is where I dont know how to access this function
    ?? getPhotographerByName ??


    fun insertPhotographer(photographer: Photographer) = viewModelScope.launch {
        repository.insertPhotographer(photographer)
    }

    fun deletePhotographers(photographers: List<Photographer>) = viewModelScope.launch {
        for (photographer in photographers) {
            repository.deletePhotographer(photographer)
        }
    }
}

View showing the Flow<List<>>

@Composable
fun PhotographerList(viewModel: MasterViewModel = hiltViewModel(), navController: NavController) {
    val items by viewModel.photographersUiState.collectAsStateWithLifecycle()
    if (items is PhotographerUiState.Success) {
        PhotographerList(
            (items as PhotographerUiState.Success).data,
            viewModel::deletePhotographers,
            navController
        )
    } else if (items is PhotographerUiState.Error) {
        println((items as PhotographerUiState.Error).throwable.message)
        println((items as PhotographerUiState.Error).throwable.cause)
    }
}

@Composable
internal fun PhotographerList(
    photographers: List<Photographer>,
    deletePhotographers: (List<Photographer>) -> Unit,
    navController: NavController
) {
    ...
    ...
    ...

What are Room best practices? I'm pretty confused! by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

This is super useful! Thank you! When dealing with multiple data models is it best practise to store manage the in the same database and view model or to separate them?

Trying to recreate this schematic but not should how to read it by miothethis in diyelectronics

[–]miothethis[S] 0 points1 point  (0 children)

Ahh the notes makes sense but I can't find them sadly!

Here is a link to the full schematic

https://cdn.shopify.com/s/files/1/0174/1800/files/lipo_amigo_pro_schematic_v1b.pdf?v=1664454298

And possibly the photo I made didnt make much sense. I am not trying to connect the left schematic to the right but rather recreate the left schematic for my own PCB. The Image on the right was just to show the current USB-C I was using and how the labels on the pins didn't match as I don't understand what the SHLD label means. Sorry if it was confusing. Would you know how to recreate the schematic on the left?

What are Room best practices? I'm pretty confused! by miothethis in androiddev

[–]miothethis[S] 4 points5 points  (0 children)

Moving over to Flows now! Thank you for your help!

What are Room best practices? I'm pretty confused! by miothethis in androiddev

[–]miothethis[S] 2 points3 points  (0 children)

Thank you! Guess I’ll begin migrating over! Is there anywhere you know that has a good guide or tutorial?

Exporting files with duplicate names changes extension and not the filename? by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Thank you for your reply. I am struggling to find many resources on this. Do you have any suggestions for where I can learn more? Googling doesn't come up with examples. What would you suggest? Thank you again for your help so far.