Skip to content
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
4 changes: 4 additions & 0 deletions AnkiDroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ apply plugin: 'app.brant.amazonappstorepublisher'
apply plugin: 'idea'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'


repositories {
Expand Down Expand Up @@ -315,6 +317,8 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
implementation 'com.github.ByteHamster:SearchPreference:v2.3.0'
implementation 'com.github.AppIntro:AppIntro:6.2.0'
implementation "com.google.dagger:hilt-android:2.43.2"
kapt "com.google.dagger:hilt-android-compiler:2.43.2"

// Cannot use debugImplementation since classes need to be imported in AnkiDroidApp
// and there's no no-op version for release build. Usage has been disabled for release
Expand Down
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.ichi2.anki.services.NotificationService
import com.ichi2.compat.CompatHelper
import com.ichi2.themes.Themes
import com.ichi2.utils.*
import dagger.hilt.android.HiltAndroidApp
import net.ankiweb.rsdroid.BackendFactory
import timber.log.Timber
import timber.log.Timber.DebugTree
Expand All @@ -55,6 +56,7 @@ import java.util.regex.Pattern
/**
* Application class.
*/
@HiltAndroidApp
@KotlinCleanup("lots to do")
@KotlinCleanup("IDE Lint")
open class AnkiDroidApp : Application() {
Expand Down
55 changes: 55 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/di/DispatcherModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/****************************************************************************************
* *
* Copyright (c) 2022 Shridhar Goel <shridhar.goel@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/

package com.ichi2.anki.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import javax.inject.Qualifier

@Module
@InstallIn(SingletonComponent::class)
object DispatchersModule {

@DefaultDispatcher
@Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default

@IoDispatcher
@Provides
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO

@MainDispatcher
@Provides
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
}

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class DefaultDispatcher

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class IoDispatcher

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class MainDispatcher
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.lifecycleScope
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.R
import com.ichi2.anki.UIUtils.showThemedToast
import com.ichi2.anki.di.IoDispatcher
import com.ichi2.anki.di.MainDispatcher
import com.ichi2.anki.multimediacard.beolingus.parsing.BeolingusParser
import com.ichi2.anki.multimediacard.language.LanguageListerBeolingus
import com.ichi2.anki.web.HttpFetcher.downloadFileToSdCard
import com.ichi2.anki.web.HttpFetcher.fetchThroughHttp
import com.ichi2.themes.Themes.disableXiaomiForceDarkMode
import com.ichi2.utils.AdaptionUtil.isUserATestClient
import com.ichi2.utils.NetworkUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import javax.inject.Inject

/**
* Activity to load pronunciation files from Beolingus.
Expand All @@ -58,7 +60,15 @@ import java.net.URLEncoder
* <p>
* When activity finished, it passes the filepath as another extra to the caller.
*/
@AndroidEntryPoint
open class LoadPronunciationActivity : AnkiActivity(), DialogInterface.OnCancelListener {

@Inject
@MainDispatcher lateinit var mainDispatcher: CoroutineDispatcher

@Inject
@IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher

private var mStopped = false
private lateinit var source: String
private lateinit var mTranslationAddress: String
Expand Down Expand Up @@ -345,14 +355,5 @@ open class LoadPronunciationActivity : AnkiActivity(), DialogInterface.OnCancelL

// Passed out as a result
const val EXTRA_PRONUNCIATION_FILE_PATH = "com.ichi2.anki.LoadPronounciationActivity.extra.pronun.file.path"

private var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
private var mainDispatcher: CoroutineDispatcher = Dispatchers.Main

@VisibleForTesting
fun setTestDispatchers(dispatcher: CoroutineDispatcher) {
ioDispatcher = dispatcher
mainDispatcher = dispatcher
}
}
}
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ buildscript {

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:11.0.0"

classpath "com.google.dagger:hilt-android-gradle-plugin:2.43.2"
}
}

Expand Down