-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6e94d8
commit 5459cd2
Showing
6 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...es/model/src/main/java/net/pantasystem/milktea/model/note/timeline/SyncTimelineUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.pantasystem.milktea.model.note.timeline | ||
|
||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.coroutineScope | ||
import net.pantasystem.milktea.common.runCancellableCatching | ||
import net.pantasystem.milktea.model.UseCase | ||
import net.pantasystem.milktea.model.account.AccountRepository | ||
import javax.inject.Inject | ||
|
||
class SyncTimelineUseCase @Inject constructor( | ||
private val accountRepository: AccountRepository, | ||
private val timelineRepository: TimelineRepository, | ||
) : UseCase { | ||
|
||
suspend operator fun invoke(): Result<Unit> = runCancellableCatching { | ||
val accounts = accountRepository.findAll().getOrThrow() | ||
coroutineScope { | ||
accounts.flatMap { it.pages }.map { | ||
async { | ||
val type = TimelineType( | ||
accountId = it.accountId, | ||
pageable = it.pageable(), | ||
pageId = null, | ||
) | ||
if (type.canCache() && it.isSavePagePosition) { | ||
sync( | ||
type, | ||
nextId = timelineRepository.findFirstLaterId(type).getOrNull() | ||
) | ||
} | ||
} | ||
}.awaitAll() | ||
} | ||
} | ||
|
||
// ページがなくなるまで最新の投稿を取得し続ける | ||
suspend fun sync(type: TimelineType, nextId: String?): Result<Unit> = runCancellableCatching { | ||
if (type.canCache()) { | ||
return@runCancellableCatching | ||
} | ||
val response = timelineRepository.findLaterTimeline(type, sinceId = nextId).getOrThrow() | ||
if (response.timelineItems.isEmpty()) { | ||
return@runCancellableCatching | ||
} | ||
|
||
sync(type, response.untilId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
modules/worker/src/main/java/net/pantasystem/milktea/worker/note/SyncTimelineWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package net.pantasystem.milktea.worker.note | ||
|
||
import android.content.Context | ||
import androidx.hilt.work.HiltWorker | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.OneTimeWorkRequest | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.PeriodicWorkRequest | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkerParameters | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedInject | ||
import net.pantasystem.milktea.model.note.timeline.SyncTimelineUseCase | ||
import java.util.concurrent.TimeUnit | ||
|
||
@HiltWorker | ||
|
||
class SyncTimelineWorker @AssistedInject constructor( | ||
@Assisted context: Context, | ||
@Assisted val params: WorkerParameters, | ||
private val syncTimelineUseCase: SyncTimelineUseCase, | ||
): CoroutineWorker(context, params) { | ||
|
||
companion object { | ||
const val WORKER_NAME = "SyncTimelineWorker" | ||
fun createPeriodicWorkRequest(): PeriodicWorkRequest { | ||
return PeriodicWorkRequestBuilder<SyncTimelineWorker>(1, TimeUnit.MINUTES) | ||
.build() | ||
} | ||
|
||
fun createOneTimeWorkRequest(): OneTimeWorkRequest { | ||
return OneTimeWorkRequestBuilder<SyncTimelineWorker>() | ||
.build() | ||
} | ||
} | ||
|
||
override suspend fun doWork(): Result { | ||
syncTimelineUseCase().fold( | ||
onSuccess = { | ||
return Result.success() | ||
}, | ||
onFailure = { | ||
return Result.failure() | ||
} | ||
) | ||
} | ||
} |