Skip to content

Commit

Permalink
refactor: deleted unused methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirelon committed Feb 9, 2024
1 parent 47f2178 commit 82e5954
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ class ImagesRepository(private val context: Context) {

fun loadImages(ids: List<String>) = imagesDao.getImagesByIds(ids)

fun loadPopularPhotos() = imagesDao.loadPopularImages()

fun loadFirstImage() = imagesDao.getOneImage()

fun loadFavoritePagedSource(): Flow<PagingData<MarsImage>> {
return Pager(
config = PagingConfig(10, 2),
Expand All @@ -64,8 +60,6 @@ class ImagesRepository(private val context: Context) {
}
}

fun getFavoriteImages() = imagesDao.getFavoriteImages()

@OptIn(ExperimentalPagingApi::class)
fun loadPopularPagedSource(): Flow<PagingData<MarsImage>> {
return Pager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ import kotlin.random.Random
*/
class PhotosViewModel(app: Application) : AndroidViewModel(app) {

val dataManger = getApplication<RoverApplication>().dataManger
private val dataManger = getApplication<RoverApplication>().dataManger
private val roversRepository = dataManger.roverRepo
private val photosRepository = dataManger.photosRepo
private val imagesRepository = dataManger.imagesRepo

private val queryEmmiter = MutableStateFlow<PhotosQueryRequest?>(null)
private val roverIdEmmiter = MutableStateFlow<Long?>(null)
private val queryEmitter = MutableStateFlow<PhotosQueryRequest?>(null)
private val roverIdEmitter = MutableStateFlow<Long?>(null)
private var dateUtil: RoverDateUtil? = null

private val roverFlow = roverIdEmmiter
private val roverFlow = roverIdEmitter
.filterNotNull()
.mapNotNull { roversRepository.loadRoverById(it) }
.onEach { dateUtil = RoverDateUtil(it) }
.catch { recordException(it) }
.flowOn(Dispatchers.IO)

// null means that we are in loading process.
val photosFlow = queryEmmiter
val photosFlow = queryEmitter
.map {
if (it != null) photosRepository.refreshImages(it)
else null
}
.catch { recordException(it) }
.flowOn(Dispatchers.IO)

val solFlow = queryEmmiter.mapNotNull { it?.sol }
val solFlow = queryEmitter.mapNotNull { it?.sol }

init {
viewModelScope.launch(Dispatchers.IO) {
Expand All @@ -63,16 +63,16 @@ class PhotosViewModel(app: Application) : AndroidViewModel(app) {
}
}

fun setPhotosQuery(query: PhotosQueryRequest) {
private fun setPhotosQuery(query: PhotosQueryRequest) {
query.logD()
queryEmmiter.tryEmit(null)
queryEmitter.tryEmit(null)
viewModelScope.launch {
queryEmmiter.emit(query)
queryEmitter.emit(query)
}
}

fun setRoverId(roverId: Long) {
roverIdEmmiter.tryEmit(roverId)
roverIdEmitter.tryEmit(roverId)
}

fun randomize() {
Expand All @@ -86,7 +86,7 @@ class PhotosViewModel(app: Application) : AndroidViewModel(app) {
viewModelScope.launch(Dispatchers.IO) {
val rover = roverFlow.first()
val maxSol = rover.maxSol - 1
val query = if (queryEmmiter.value?.sol == maxSol) {
val query = if (queryEmitter.value?.sol == maxSol) {
randomPhotosQueryRequest()
} else {
PhotosQueryRequest(rover.id, maxSol, null)
Expand All @@ -96,7 +96,7 @@ class PhotosViewModel(app: Application) : AndroidViewModel(app) {
}
}

fun getSol() = queryEmmiter.value?.sol ?: 0
private fun getSol() = queryEmitter.value?.sol ?: 0

fun earthDateStr(sol: Long): String {
val time = earthTime(sol)
Expand Down Expand Up @@ -125,9 +125,9 @@ class PhotosViewModel(app: Application) : AndroidViewModel(app) {
}

fun loadBySol(sol: Long) {
val queryRequest = queryEmmiter.value ?: return
val queryRequest = queryEmitter.value ?: return
if (sol == queryRequest.sol) return
queryEmmiter.tryEmit(null)
queryEmitter.tryEmit(null)

val queryUpdated = queryRequest.copy(sol = sol)
setPhotosQuery(queryUpdated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -82,7 +83,7 @@ fun RoverPhotosScreen(

var openSolDialog by remember { mutableStateOf(false) }

var maxSol: Long by remember(calculation = { mutableStateOf(Long.MAX_VALUE) })
var maxSol: Long by remember(calculation = { mutableLongStateOf(Long.MAX_VALUE) })
LaunchedEffect(key1 = maxSol, block = {
maxSol = viewModel.maxSol()
})
Expand Down Expand Up @@ -269,7 +270,6 @@ private fun SolChanger(sol: Long?, maxSol: Long, onSolChanged: (sol: Long?) -> U
text = "Sol: ",
modifier = Modifier.weight(1f),
style = MaterialTheme.typography.titleLarge.copy(
// color = MaterialTheme.colorScheme.secondary
)
)
OutlinedTextField(
Expand Down Expand Up @@ -334,22 +334,4 @@ private fun showEarthDateeChooser(activity: FragmentActivity, viewModel: PhotosV
calender.get(Calendar.DAY_OF_MONTH)
)
datePicker.show()

// findViewById<View>(R.id.dateEarthChoose).setOnClickListener {
// // UPDATE TIME
// val timeFromSol = viewModel.dateFromSol()
//
// calender.timeInMillis = timeFromSol
//
// datePicker.updateDate(
// calender.get(Calendar.YEAR),
// calender.get(Calendar.MONTH),
// calender.get(Calendar.DAY_OF_MONTH)
// )
//
// // Hide title. Need to set AFTER all
// datePicker.setTitle("")
// // SHOW DIALOG
// datePicker.show()
// }
}
10 changes: 0 additions & 10 deletions app/src/main/java/com/sirelon/marsroverphotos/storage/ImagesDao.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sirelon.marsroverphotos.storage

import androidx.lifecycle.LiveData
import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.Insert
Expand All @@ -19,12 +18,6 @@ interface ImagesDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertImages(images: List<MarsImage>): List<Long>

@Query("SELECT * FROM images WHERE favorite = 1")
fun getFavoriteImages(): LiveData<List<MarsImage>>

@Query("SELECT * FROM images WHERE favorite = 1 LIMIT 1 ")
fun getOneImage(): LiveData<MarsImage?>

@Query("SELECT * FROM images WHERE id IN (:ids) ORDER BY `order` ASC")
fun getImagesByIds(ids: List<String>): Flow<List<MarsImage>>

Expand All @@ -49,9 +42,6 @@ interface ImagesDao {
@Query("SELECT * FROM images WHERE popular = 1 ORDER BY `order` ASC")
fun loadPopularPagedSource(): PagingSource<Int, MarsImage>

@Query("SELECT * FROM images WHERE popular = 1 ORDER BY `order` ASC")
fun loadPopularImages(): Flow<List<MarsImage>>

@Query("DELETE FROM images WHERE popular = 1")
fun deleteAllPopular()

Expand Down

0 comments on commit 82e5954

Please sign in to comment.