Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,203 changes: 1,175 additions & 28 deletions app/src/main/AndroidManifest.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ class MangaLinkResolver @Inject constructor(
companion object {

fun isValidLink(str: String): Boolean {
return str.isHttpUrl() || str.startsWith("doki://", ignoreCase = true)
return str.isHttpUrl() ||
str.startsWith("doki://", ignoreCase = true) ||
str.startsWith("kotatsu://", ignoreCase = true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.roundToInt

private const val REDIRECT_URI = "doki://anilist-auth"
private const val REDIRECT_URI = "kotatsu://anilist-auth"
private const val BASE_URL = "https://anilist.co/api/v2/"
private const val ENDPOINT = "https://graphql.anilist.co"
private const val MANGA_PAGE_SIZE = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class KitsuAuthActivity : BaseActivity<ActivityKitsuAuthBinding>(),
private fun continueAuth() {
val email = viewBinding.editEmail.text?.toString()?.trim().orEmpty()
val password = viewBinding.editPassword.text?.toString()?.trim().orEmpty()
val url = "doki://kitsu-auth?code=" + "$email;$password".urlEncoded()
val url = "kotatsu://kitsu-auth?code=" + "$email;$password".urlEncoded()
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
startActivity(intent)
finishAfterTransition()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.security.SecureRandom
import javax.inject.Inject
import javax.inject.Singleton

private const val REDIRECT_URI = "doki://mal-auth"
private const val REDIRECT_URI = "kotatsu://mal-auth"
private const val BASE_WEB_URL = "https://myanimelist.net"
private const val BASE_API_URL = "https://api.myanimelist.net/v2"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import javax.inject.Inject
import javax.inject.Singleton

private const val DOMAIN = "shikimori.one"
private const val REDIRECT_URI = "doki://shikimori-auth"
private const val REDIRECT_URI = "kotatsu://shikimori-auth"
private const val BASE_URL = "https://$DOMAIN/"
private const val MANGA_PAGE_SIZE = 10

Expand Down
163 changes: 95 additions & 68 deletions app/src/main/kotlin/org/dokiteam/doki/tracker/work/TrackWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
Expand Down Expand Up @@ -95,7 +94,7 @@ class TrackWorker @AssistedInject constructor(
doWorkImpl(isFullRun = isForeground && TAG_ONESHOT in tags)
} catch (e: CancellationException) {
throw e
} catch (e: Throwable) {
} catch (e: Throwable) {
e.printStackTraceDebug()
Result.failure()
} finally {
Expand All @@ -105,74 +104,102 @@ class TrackWorker @AssistedInject constructor(
}
}

private suspend fun doWorkImpl(isFullRun: Boolean): Result {
if (!settings.isTrackerEnabled) {
return Result.success()
}
val tracks = getTracksUseCase(if (isFullRun) Int.MAX_VALUE else BATCH_SIZE)
if (tracks.isEmpty()) {
return Result.success()
}
private suspend fun doWorkImpl(isFullRun: Boolean): Result {
if (!settings.isTrackerEnabled) {
return Result.success()
}
val tracks = getTracksUseCase(if (isFullRun) Int.MAX_VALUE else BATCH_SIZE)
if (tracks.isEmpty()) {
return Result.success()
}

val notifications = checkUpdatesAsync(tracks)
if (notifications.isNotEmpty() && applicationContext.checkNotificationPermission(null)) {
val groupNotification = notificationHelper.createGroupNotification(notifications)
notifications.forEach { notificationManager.notify(it.tag, it.id, it.notification) }
if (groupNotification != null) {
notificationManager.notify(TAG, TrackerNotificationHelper.GROUP_NOTIFICATION_ID, groupNotification)
}
}
return Result.success()
}
checkUpdatesAsync(tracks)
return Result.success()
}

@CheckResult
private suspend fun checkUpdatesAsync(tracks: List<MangaTracking>): List<NotificationInfo> {
val semaphore = Semaphore(MAX_PARALLELISM)
return channelFlow {
for (track in tracks) {
launch {
semaphore.withPermit {
send(
runCatchingCancellable {
checkNewChaptersUseCase.invoke(track)
}.getOrElse { error ->
MangaUpdates.Failure(
manga = track.manga,
error = error,
)
},
)
}
}
}
}.onEachIndexed { index, it ->
if (applicationContext.checkNotificationPermission(WORKER_CHANNEL_ID)) {
notificationManager.notify(WORKER_NOTIFICATION_ID, createWorkerNotification(tracks.size, index + 1))
}
when (it) {
is MangaUpdates.Failure -> {
val e = it.error
if (e is CloudFlareException) {
captchaHandler.handle(e)
}
}
@CheckResult
private suspend fun checkUpdatesAsync(tracks: List<MangaTracking>) {
val semaphore = Semaphore(MAX_PARALLELISM)
val groupNotifications = mutableListOf<NotificationInfo>()

is MangaUpdates.Success -> processDownload(it)
}
}.mapNotNull {
when (it) {
is MangaUpdates.Failure -> null
is MangaUpdates.Success -> if (it.isValid && it.isNotEmpty()) {
notificationHelper.createNotification(
manga = it.manga,
newChapters = it.newChapters,
)
} else {
null
}
}
}.toList()
}
try {
channelFlow {
for (track in tracks) {
launch {
semaphore.withPermit {
send(
runCatchingCancellable {
checkNewChaptersUseCase.invoke(track)
}.getOrElse { error ->
MangaUpdates.Failure(
manga = track.manga,
error = error,
)
},
)
}
}
}
}.onEachIndexed { index, it ->
if (applicationContext.checkNotificationPermission(WORKER_CHANNEL_ID)) {
notificationManager.notify(
WORKER_NOTIFICATION_ID,
createWorkerNotification(tracks.size, index + 1)
)
}

when (it) {
is MangaUpdates.Failure -> {
val e = it.error
if (e is CloudFlareException) {
captchaHandler.handle(e)
}
}

is MangaUpdates.Success -> {
processDownload(it)

if (it.isValid && it.isNotEmpty()) {
val notificationInfo = notificationHelper.createNotification(
manga = it.manga,
newChapters = it.newChapters,
)

if (notificationInfo != null &&
applicationContext.checkNotificationPermission(TrackerNotificationHelper.CHANNEL_ID)) {
notificationManager.notify(
notificationInfo.tag,
notificationInfo.id,
notificationInfo.notification
)

synchronized(groupNotifications) {
groupNotifications.add(notificationInfo)
}
}
}
}
}
}.collect()

} catch (e: CancellationException) {
e.printStackTraceDebug()
} finally {
withContext(NonCancellable) {
if (groupNotifications.size > 1 &&
applicationContext.checkNotificationPermission(TrackerNotificationHelper.CHANNEL_ID)) {
val groupNotification = notificationHelper.createGroupNotification(groupNotifications)
if (groupNotification != null) {
notificationManager.notify(
TAG,
TrackerNotificationHelper.GROUP_NOTIFICATION_ID,
groupNotification
)
}
}
}
}
}

override suspend fun getForegroundInfo(): ForegroundInfo {
val channel = NotificationChannelCompat.Builder(
Expand Down