Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install chaquopy #26

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ android {
putBuildConfig(localProperties, "LAST_FM_API_KEY")
putBuildConfig(localProperties, "LAST_FM_API_SECRET")
putBuildConfig(localProperties, "MUSIXMATCH_API_KEY")
putBuildConfig(localProperties, "YTMUSIC_CLIENT_ID")
putBuildConfig(localProperties, "YTMUSIC_CLIENT_SECRET")
}

if (it.buildType == "release") {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:localeConfig="@xml/locales_config"
android:enableOnBackInvokedCallback="true"
android:enableOnBackInvokedCallback="false"
android:requestLegacyExternalStorage="true"
android:extractNativeLibs="true"
android:icon="@mipmap/ic_launcher"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/caios/android/kanade/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ object AppModule {
lastFmApiKey = BuildConfig.LAST_FM_API_KEY,
lastFmApiSecret = BuildConfig.LAST_FM_API_SECRET,
musixmatchApiKey = BuildConfig.MUSIXMATCH_API_KEY,
ytMusicApiKey = BuildConfig.YTMUSIC_CLIENT_ID,
ytMusicApiSecret = BuildConfig.YTMUSIC_CLIENT_SECRET,
)
}

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/caios/android/kanade/navigation/KanadeNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import caios.android.kanade.feature.setting.oss.settingLicenseScreen
import caios.android.kanade.feature.setting.theme.navigateToSettingTheme
import caios.android.kanade.feature.setting.theme.settingThemeScreen
import caios.android.kanade.feature.setting.top.settingTopScreen
import caios.android.kanade.feature.setting.ytmusic.navigateToYTMusicLogin
import caios.android.kanade.feature.setting.ytmusic.ytmusicLoginDialog
import caios.android.kanade.feature.share.ShareUtil
import caios.android.kanade.feature.song.detail.navigateToSongDetail
import caios.android.kanade.feature.song.detail.songDetailScreen
Expand Down Expand Up @@ -417,6 +419,9 @@ fun KanadeNavHost(
)

settingTopScreen(
navigateToYTMusicLogin = {
navController.navigateToYTMusicLogin()
},
navigateToEqualizer = {
activity.startActivity(
Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL).apply {
Expand Down Expand Up @@ -460,5 +465,11 @@ fun KanadeNavHost(
navController.popBackStack()
},
)

ytmusicLoginDialog(
terminate = {
navController.popBackStack()
},
)
}
}
15 changes: 14 additions & 1 deletion app/src/main/java/caios/android/kanade/ui/KanadeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.asPaddingValues
Expand Down Expand Up @@ -90,6 +91,12 @@ fun KanadeApp(
val activity = (LocalContext.current as Activity)
var isShowWelcomeScreen by remember { mutableStateOf(!isAgreedTeams || !isAllowedPermission) }

LaunchedEffect(true) {
if (userData.kanadeId.isBlank()) {
musicViewModel.initKanadeId()
}
}

KanadeBackground(modifier) {
AnimatedContent(
targetState = isShowWelcomeScreen && (!isAgreedTeams || !isAllowedPermission),
Expand Down Expand Up @@ -277,6 +284,11 @@ private fun IdleScreen(
)
},
) { rootPadding ->
val padding = PaddingValues(
top = rootPadding.calculateTopPadding(),
bottom = bottomSheetPeekHeight,
)

BottomSheetScaffold(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -347,7 +359,7 @@ private fun IdleScreen(
)
},
) {
Box(Modifier.padding(rootPadding)) {
Box(Modifier.padding(padding)) {
if (musicViewModel.uiState.isAnalyzing) {
LoadingDialog(R.string.common_analyzing)
}
Expand All @@ -360,6 +372,7 @@ private fun IdleScreen(
}
.zIndex(if (appState.currentLibraryDestination == null) 0f else 1f)
.alpha(topBarAlpha),
isEnableYTMusic = userData.isEnableYTMusic,
active = isSearchActive,
yOffset = toolbarOffset,
onChangeActive = { isSearchActive = it },
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/caios/android/kanade/ui/KanadeTopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -47,13 +48,15 @@ import caios.android.kanade.core.model.music.Playlist
import caios.android.kanade.core.model.music.Song
import caios.android.kanade.feature.search.top.SearchRoute
import caios.android.kanade.feature.search.top.SearchViewModel
import kotlinx.coroutines.launch

@Suppress("ViewModelInjection")
@OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationGraphicsApi::class)
@Composable
fun KanadeTopBar(
active: Boolean,
yOffset: Dp,
isEnableYTMusic: Boolean,
onChangeActive: (Boolean) -> Unit,
onClickDrawerMenu: () -> Unit,
navigateToArtistDetail: (Long) -> Unit,
Expand All @@ -66,6 +69,7 @@ fun KanadeTopBar(
modifier: Modifier = Modifier,
) {
val density = LocalDensity.current
val scope = rememberCoroutineScope()
val searchViewModel = hiltViewModel<SearchViewModel>()

val image = AnimatedImageVector.animatedVectorResource(R.drawable.av_drawer_to_arrow)
Expand All @@ -85,7 +89,9 @@ fun KanadeTopBar(
}

LaunchedEffect(query) {
searchViewModel.search(listOf(query))
if (!isEnableYTMusic) {
searchViewModel.search(listOf(query))
}
}

Column(
Expand All @@ -105,7 +111,13 @@ fun KanadeTopBar(
SearchBar(
query = query,
onQueryChange = { query = it },
onSearch = { },
onSearch = {
if (isEnableYTMusic) {
scope.launch {
searchViewModel.search(listOf(query))
}
}
},
active = active,
onActiveChange = onChangeActive,
colors = SearchBarDefaults.colors(containerColor = MaterialTheme.colorScheme.surfaceContainerHigh),
Expand Down Expand Up @@ -151,6 +163,7 @@ private fun Preview() {
KanadeTopBar(
active = false,
yOffset = 0.dp,
isEnableYTMusic = false,
onChangeActive = { },
onClickDrawerMenu = { },
navigateToArtistDetail = { },
Expand Down
4 changes: 4 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ gradlePlugin {
id = "kanade.library.glance"
implementationClass = "AndroidLibraryGlanceConventionPlugin"
}
register("androidLibraryChaquopy") {
id = "kanade.library.chaquopy"
implementationClass = "AndroidLibraryChaquopyConventionPlugin"
}
register("androidHilt") {
id = "kanade.hilt"
implementationClass = "AndroidHiltConventionPlugin"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import caios.android.kanade.configureChaquopy
import com.android.build.gradle.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidLibraryChaquopyConventionPlugin: Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.chaquo.python")
}

extensions.configure<LibraryExtension> {
configureChaquopy(this)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package caios.android.kanade

import com.android.build.api.dsl.CommonExtension

internal fun configureChaquopy(commonExtension: CommonExtension<*, *, *, *, *>) {
commonExtension.apply {
defaultConfig {
ndk {
abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86_64")
}
}
}
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ plugins {
alias(libs.plugins.detekt) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.libraries) apply false
alias(libs.plugins.chaquopy) apply false
alias(libs.plugins.module.graph) apply false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ data class KanadeConfig(
val lastFmApiKey: String,
val lastFmApiSecret: String,
val musixmatchApiKey: String,
val ytMusicApiKey: String,
val ytMusicApiSecret: String,
) {
companion object {
fun dummy(): KanadeConfig {
Expand All @@ -23,6 +25,8 @@ data class KanadeConfig(
lastFmApiKey = "1919191919",
lastFmApiSecret = "1919191919",
musixmatchApiKey = "1919191919",
ytMusicApiKey = "1919191919",
ytMusicApiSecret = "1919191919",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package caios.android.kanade.core.common.network.util

import timber.log.Timber
import kotlin.coroutines.cancellation.CancellationException

suspend fun <T> suspendRunCatching(block: suspend () -> T): Result<T> = try {
Result.success(block())
} catch (cancellationException: CancellationException) {
throw cancellationException
} catch (exception: Exception) {
Timber.i(exception, "Failed to evaluate a suspendRunCatchingBlock. Returning failure Result")
Result.failure(exception)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class KanadePreferencesDataStore @Inject constructor(
val userData = userPreference.data
.map {
UserData(
kanadeId = it.kanadeId,
themeConfig = when (it.themeConfig) {
ThemeConfigProto.THEME_CONFIG_LIGHT -> ThemeConfig.Light
ThemeConfigProto.THEME_CONFIG_DARK -> ThemeConfig.Dark
Expand All @@ -51,6 +52,7 @@ class KanadePreferencesDataStore @Inject constructor(
isIgnoreNotMusic = if (it.hasIsUseIgnoreNotMusic()) it.isUseIgnoreNotMusic else true,
isAgreedPrivacyPolicy = if (it.hasIsAgreedPrivacyPolicy()) it.isAgreedPrivacyPolicy else false,
isAgreedTermsOfService = if (it.hasIsAgreedTermsOfService()) it.isAgreedTermsOfService else false,
isEnableYTMusic = if (it.hasIsEnableYtmusic()) it.isEnableYtmusic else false,
)
}

Expand Down Expand Up @@ -121,6 +123,14 @@ class KanadePreferencesDataStore @Inject constructor(
)
}

suspend fun setKanadeId(id: String) = withContext(ioDispatcher) {
userPreference.updateData {
it.copy {
this.kanadeId = id
}
}
}

suspend fun setThemeConfig(themeConfig: ThemeConfig) = withContext(ioDispatcher) {
userPreference.updateData {
it.copy {
Expand Down Expand Up @@ -237,6 +247,14 @@ class KanadePreferencesDataStore @Inject constructor(
}
}

suspend fun setEnableYTMusic(isEnableYTMusic: Boolean) = withContext(ioDispatcher) {
userPreference.updateData {
it.copy {
this.isEnableYtmusic = isEnableYTMusic
}
}
}

suspend fun setShuffleMode(shuffleMode: ShuffleMode) = withContext(ioDispatcher) {
musicPreference.updateData {
it.copy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.core.net.toUri
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

class DownloadPathPreference @Inject constructor(
class PreferenceDownloadPath @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val preference by lazy { context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

class EqualizerPreference @Inject constructor(
class PreferenceEqualizer @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val preference by lazy { context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlinx.serialization.json.Json
import java.io.File
import javax.inject.Inject

class LyricsPreference @Inject constructor(
class PreferenceLyrics @Inject constructor(
@ApplicationContext private val context: Context,
@Dispatcher(KanadeDispatcher.IO) private val ioDispatcher: CoroutineDispatcher,
@ApplicationScope private val scope: CoroutineScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.core.content.edit
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

class TokenPreference @Inject constructor(
class PreferenceToken @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val preference by lazy { context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.serialization.json.Json
import java.util.Locale
import javax.inject.Inject

class VersionPreference @Inject constructor(
class PreferenceVersion @Inject constructor(
@ApplicationContext private val context: Context,
) {
private var data: List<Version>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlinx.serialization.json.Json
import java.io.File
import javax.inject.Inject

class VolumePreference @Inject constructor(
class PreferenceVolume @Inject constructor(
@ApplicationContext private val context: Context,
@Dispatcher(KanadeDispatcher.IO) private val ioDispatcher: CoroutineDispatcher,
@ApplicationScope private val scope: CoroutineScope,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package caios.android.kanade.core.datastore

import android.content.Context
import caios.android.kanade.core.model.entity.YTMusicOAuthToken
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.serialization.json.Json
import java.io.File
import javax.inject.Inject

class PreferenceYTMusic @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val tokenFile get() = File(context.filesDir, FILE_NAME)

fun saveToken(token: YTMusicOAuthToken) {
Json.encodeToString(YTMusicOAuthToken.serializer(), token).also {
tokenFile.writeText(it)
}
}

fun removeToken() {
tokenFile.delete()
}

fun getToken(): YTMusicOAuthToken? {
if (!tokenFile.exists()) return null

return tokenFile.readText().let {
Json.decodeFromString(YTMusicOAuthToken.serializer(), it)
}
}

fun getTokenFilePath(): String {
return tokenFile.absolutePath
}

companion object {
private const val FILE_NAME = "YTMusicToken.json"
}
}
Loading
Loading