Skip to content

Commit

Permalink
Merge pull request #21 from Wanted-Pre-Onboarding-Android-team3-2/fea…
Browse files Browse the repository at this point in the history
…t/paging_list

[Feat]paging list
  • Loading branch information
yforyuri authored Sep 30, 2022
2 parents 725fa89 + fb02214 commit 1681a44
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ interface SensorHistoryDao {
@Delete
suspend fun deleteSensorHistory(sensorHistory: SensorHistoryEntity)

@Query("SELECT * FROM sensor_history_table ORDER BY publishedAt DESC LIMIT :loadSize OFFSET (:page-1) * :loadSize")
fun getSensorDataList(page: Int, loadSize: Int): List<SensorHistoryEntity>
@Query("SELECT * from sensor_history_table ORDER BY publishedAt DESC LIMIT :loadSize OFFSET (:page - 1) * :loadSize")
suspend fun getSensorDataList(page: Int, loadSize: Int): List<SensorHistoryEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ interface SensorHistoryLocalDataSource {
fun getSensorHistoryList(): Flow<List<SensorHistoryEntity>>
suspend fun saveSensorHistory(sensorHistory: SensorHistoryEntity)
suspend fun deleteSensorHistory(sensorHistory: SensorHistoryEntity)
fun getSensorDataList(): List<SensorHistoryEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ class SensorHistoryLocalDataSourceImpl @Inject constructor(
override suspend fun deleteSensorHistory(sensorHistory: SensorHistoryEntity) {
sensorHistoryDao.deleteSensorHistory(sensorHistory)
}

override fun getSensorDataList(): List<SensorHistoryEntity> =
sensorHistoryDao.getSensorDataList(page = 1, loadSize = 5)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ class HistoryPagingSource(
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, SensorHistoryEntity> {
val page = params.key ?: 1
return try {
val items = dao.getSensorDataList(
page,
params.loadSize
)
val items = dao.getSensorDataList(page, params.loadSize)
LoadResult.Page(
data = items,
prevKey = if (page == 1) null else page - 1,
nextKey = if (items.isEmpty()) null else page + (params.loadSize / 5)
nextKey = if (items.isEmpty()) null else page + 1
)
} catch (e: Exception) {
return LoadResult.Error(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class SensorHistoryRepositoryImpl @Inject constructor(
sensorHistoryLocalDataSource.deleteSensorHistory(sensorHistory.toEntity())
}

override fun getSensorDataList(): Flow<PagingData<SensorHistory>> {
override suspend fun getSensorDataList(): Flow<PagingData<SensorHistory>> {
return (Pager(
config = PagingConfig(
pageSize = 5,
pageSize = 10,
enablePlaceholders = false
),
pagingSourceFactory = { HistoryPagingSource(dao) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface SensorHistoryRepository {
fun getSensorHistoryList(): Flow<List<SensorHistory>>
suspend fun saveSensorHistory(sensorHistory: SensorHistory)
suspend fun deleteSensorHistory(sensorHistory: SensorHistory)
fun getSensorDataList(): Flow<PagingData<SensorHistory>>
suspend fun getSensorDataList(): Flow<PagingData<SensorHistory>>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.preonboarding.sensordashboard.domain.usecase

import androidx.paging.PagingData
import com.preonboarding.sensordashboard.domain.model.SensorHistory
import com.preonboarding.sensordashboard.domain.repository.SensorHistoryRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class GetSensorHistoryListUseCase @Inject constructor(
private val sensorHistoryRepository: SensorHistoryRepository,
) {
operator fun invoke(): Flow<PagingData<SensorHistory>> =
suspend operator fun invoke() =
sensorHistoryRepository.getSensorDataList().flowOn(Dispatchers.Default)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package com.preonboarding.sensordashboard.presentation.view.sensor_history_list
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.lifecycle.repeatOnLifecycle
import com.preonboarding.sensordashboard.R
import com.preonboarding.sensordashboard.common.base.BaseFragment
import com.preonboarding.sensordashboard.databinding.FragmentSensorHistoryListBinding
import com.preonboarding.sensordashboard.presentation.viewmodel.SensorHistoryViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

@AndroidEntryPoint
Expand All @@ -30,23 +29,20 @@ class SensorHistoryListFragment :
viewmodel = sensorHistoryViewModel
}
initAdapter()
initViewmodel()
sensorHistoryViewModel.sensorHistoryList()
}

private fun initAdapter() {
binding.historyListRv.adapter = adapter
initViewmodel()
}

private fun initViewmodel() {
lifecycleScope.launch {
adapter.loadStateFlow
.distinctUntilChangedBy { it.refresh }
.filter { it.refresh is LoadState.NotLoading }
.collect { binding.historyListRv.scrollToPosition(0) }
}
lifecycleScope.launch {
sensorHistoryViewModel.sensorHistoryList.collectLatest {
adapter.submitData(it)
repeatOnLifecycle(Lifecycle.State.STARTED) {
sensorHistoryViewModel.historyList.collectLatest {
adapter.submitData(it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@ import com.preonboarding.sensordashboard.domain.model.SensorHistory
import com.preonboarding.sensordashboard.domain.usecase.DeleteSensorHistoryUseCase
import com.preonboarding.sensordashboard.domain.usecase.GetSensorHistoryListUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SensorHistoryViewModel @Inject constructor(
getSensorHistoryListUseCase: GetSensorHistoryListUseCase,
private val getSensorHistoryListUseCase: GetSensorHistoryListUseCase,
private val deleteSensorHistoryUseCase: DeleteSensorHistoryUseCase,
) : BaseViewModel() {
val sensorHistoryList: Flow<PagingData<SensorHistory>> = getSensorHistoryListUseCase().cachedIn(viewModelScope)

fun deleteSensorHistory(){
// deleteSensorHistoryUseCase()
private val _historyList: MutableStateFlow<PagingData<SensorHistory>> =
MutableStateFlow<PagingData<SensorHistory>>(PagingData.empty())
val historyList: StateFlow<PagingData<SensorHistory>> = _historyList.asStateFlow()

fun sensorHistoryList() {
viewModelScope.launch {
getSensorHistoryListUseCase.invoke().cachedIn(viewModelScope).collectLatest {
_historyList.emit(it)
}
}
}

fun deleteSensorHistory(sensorHistory: SensorHistory){
viewModelScope.launch {
deleteSensorHistoryUseCase(sensorHistory)
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_sensor_history_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
android:id="@+id/history_list_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/cl_main"
tools:listitem="@layout/item_history_rv" />
Expand Down
37 changes: 26 additions & 11 deletions app/src/main/res/layout/item_history_rv.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
<layout>

<data>
<variable
name="viewModel"
type="com.preonboarding.sensordashboard.presentation.viewmodel.SensorHistoryViewModel" />
<variable
name="data"
type="com.preonboarding.sensordashboard.domain.model.SensorHistory" />
<variable
name="viewName"
type="com.preonboarding.sensordashboard.common.constant.ViewName" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
Expand All @@ -13,15 +19,16 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp">
android:layout_marginVertical="20dp"
android:id="@+id/rv_item">

<TextView
android:id="@+id/rv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.publishedAt}"
android:layout_marginStart="30dp"
android:textSize="15sp"
android:layout_marginStart="20dp"
android:textSize="10sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="2022/09/22 11:11:11"/>
Expand All @@ -31,8 +38,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.type}"
android:layout_marginStart="30dp"
android:textSize="30sp"
android:layout_marginStart="20dp"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/rv_date"
app:layout_constraintStart_toStartOf="parent"
tools:text="Gyro"/>
Expand All @@ -42,29 +49,37 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.period)}"
android:textSize="40sp"
android:layout_marginStart="10dp"
android:textSize="25sp"
android:layout_marginEnd="15dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/rv_date"
app:layout_constraintEnd_toStartOf="@id/rv_btn_play"
tools:text="20.0"/>

<Button
android:id="@+id/rv_btn_play"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/play"
android:textSize="10sp"
android:layout_marginStart="10dp"
android:backgroundTint="@color/purple_200"
android:layout_marginEnd="10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/rv_period"/>
app:layout_constraintEnd_toStartOf="@id/rv_btn_del"
app:navigateTo="@{viewName.PLAY}"/>

<Button
android:id="@+id/rv_btn_del"
android:layout_width="70dp"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/delete"
android:textSize="10sp"
android:onClick="@{()->viewModel.deleteSensorHistory(data)}"
android:backgroundTint="@color/purple_200"
android:layout_marginEnd="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/rv_btn_play"/>
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<string name="list_Acc">Accelerometer</string>
<string name="list_gyro">Gyro</string>
<string name="play">Play</string>
<string name="delete">Delete</string>
<string name="delete">Del</string>
<string name="view">View</string>
<string name="play_show_title">다시보기</string>

Expand Down

0 comments on commit 1681a44

Please sign in to comment.