Skip to content

Commit

Permalink
Use Dagger factories instead of builders
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre committed Oct 15, 2023
1 parent 8fc9019 commit 72dd7ee
Show file tree
Hide file tree
Showing 41 changed files with 172 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.work.WorkManager
import com.quran.labs.androidquran.core.worker.QuranWorkerFactory
import com.quran.labs.androidquran.di.component.application.ApplicationComponent
import com.quran.labs.androidquran.di.component.application.DaggerApplicationComponent
import com.quran.labs.androidquran.di.module.application.ApplicationModule
import com.quran.labs.androidquran.util.QuranSettings
import com.quran.labs.androidquran.util.RecordingLogTree
import com.quran.labs.androidquran.widget.BookmarksWidgetSubscriber
Expand Down Expand Up @@ -42,9 +41,8 @@ open class QuranApplication : Application(), QuranApplicationComponentProvider {
}

open fun initializeInjector(): ApplicationComponent {
return DaggerApplicationComponent.builder()
.applicationModule(ApplicationModule(this))
.build()
return DaggerApplicationComponent.factory()
.generate(this)
}

open fun initializeWorkManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.quran.labs.androidquran.data
import android.content.Context
import com.quran.data.di.ActivityScope
import com.quran.labs.androidquran.util.QuranFileUtils
import com.quran.mobile.di.qualifier.ApplicationContext
import javax.inject.Inject

@ActivityScope
class AyahInfoDatabaseProvider @Inject constructor(
private val context: Context,
@ApplicationContext private val context: Context,
private val widthParameter: String,
private val quranFileUtils: QuranFileUtils
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.util.SparseArray

import androidx.annotation.WorkerThread
import com.quran.labs.androidquran.common.LocalTranslation
import com.quran.labs.androidquran.dao.translation.TranslationItem
import com.quran.labs.androidquran.database.TranslationsDBHelper.TranslationsTable
import com.quran.labs.androidquran.util.QuranFileUtils

import java.util.ArrayList
import java.util.Collections
import com.quran.mobile.di.qualifier.ApplicationContext
import timber.log.Timber

import androidx.annotation.WorkerThread
import java.util.Collections
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TranslationsDBAdapter @Inject constructor(
private val context: Context,
@ApplicationContext private val context: Context,
adapter: TranslationsDBHelper,
private val quranFileUtils: QuranFileUtils
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.quran.labs.androidquran.database
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.quran.mobile.di.qualifier.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TranslationsDBHelper @Inject constructor(context: Context) :
class TranslationsDBHelper @Inject constructor(@ApplicationContext context: Context) :
SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.quran.labs.androidquran.di.component.activity

import com.quran.data.di.QuranReadingScope
import android.content.Context
import com.quran.data.di.ActivityScope
import com.quran.data.di.QuranReadingScope
import com.quran.labs.androidquran.di.component.fragment.QuranPageComponent
import com.quran.labs.androidquran.di.module.activity.PagerActivityModule
import com.quran.labs.androidquran.ui.PagerActivity
import com.quran.labs.androidquran.ui.fragment.AyahPlaybackFragment
import com.quran.labs.androidquran.ui.fragment.AyahTranslationFragment
import com.quran.labs.androidquran.ui.fragment.TagBookmarkFragment
import com.quran.page.common.toolbar.AyahToolBar
import com.quran.labs.androidquran.ui.helpers.AyahSelectedListener
import com.quran.mobile.di.QuranReadingActivityComponent
import com.quran.mobile.di.qualifier.ActivityContext
import com.quran.mobile.feature.qarilist.QariListWrapper
import com.quran.page.common.toolbar.AyahToolBar
import com.squareup.anvil.annotations.MergeSubcomponent
import dagger.BindsInstance
import dagger.Subcomponent

@ActivityScope
@MergeSubcomponent(QuranReadingScope::class, modules = [PagerActivityModule::class])
interface PagerActivityComponent : QuranReadingActivityComponent {
// subcomponents
fun quranPageComponentBuilder(): QuranPageComponent.Builder
fun quranPageComponentFactory(): QuranPageComponent.Factory

fun inject(pagerActivity: PagerActivity)
fun inject(ayahToolBar: AyahToolBar)
Expand All @@ -29,9 +33,11 @@ interface PagerActivityComponent : QuranReadingActivityComponent {

fun inject(qariListWrapper: QariListWrapper)

@Subcomponent.Builder
interface Builder {
fun withPagerActivityModule(pagerModule: PagerActivityModule): Builder
fun build(): PagerActivityComponent
@Subcomponent.Factory
interface Factory {
fun generate(
@BindsInstance @ActivityContext context: Context,
@BindsInstance ayahSelectedListener: AyahSelectedListener
): PagerActivityComponent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import dagger.Subcomponent
interface QuranActivityComponent {
fun inject(quranActivity: QuranActivity)

@Subcomponent.Builder
interface Builder {
fun build(): QuranActivityComponent
@Subcomponent.Factory
interface Factory {
fun generate(): QuranActivityComponent
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quran.labs.androidquran.di.component.application

import android.content.Context
import com.quran.analytics.provider.AnalyticsModule
import com.quran.common.networking.NetworkModule
import com.quran.data.di.AppScope
Expand Down Expand Up @@ -33,7 +34,10 @@ import com.quran.labs.androidquran.widget.BookmarksWidget
import com.quran.labs.androidquran.widget.BookmarksWidgetListProvider
import com.quran.labs.androidquran.widget.ShowJumpFragmentActivity
import com.quran.mobile.di.QuranApplicationComponent
import com.quran.mobile.di.qualifier.ApplicationContext
import com.squareup.anvil.annotations.MergeComponent
import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@Singleton
Expand All @@ -52,8 +56,8 @@ import javax.inject.Singleton
)
interface ApplicationComponent: QuranApplicationComponent {
// subcomponents
fun pagerActivityComponentBuilder(): PagerActivityComponent.Builder
fun quranActivityComponentBuilder(): QuranActivityComponent.Builder
fun pagerActivityComponentFactory(): PagerActivityComponent.Factory
fun quranActivityComponentFactory(): QuranActivityComponent.Factory

// application
fun inject(quranApplication: QuranApplication)
Expand Down Expand Up @@ -89,4 +93,9 @@ interface ApplicationComponent: QuranApplicationComponent {
// widgets
fun inject(bookmarksWidgetListProvider: BookmarksWidgetListProvider)
fun inject(bookmarksWidget: BookmarksWidget)

@Component.Factory
interface Factory {
fun generate(@BindsInstance @ApplicationContext appContext: Context): ApplicationComponent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package com.quran.labs.androidquran.di.component.fragment

import com.quran.data.di.QuranPageScope
import com.quran.data.di.QuranReadingPageScope
import com.quran.labs.androidquran.di.module.fragment.QuranPageModule
import com.quran.labs.androidquran.ui.fragment.QuranPageFragment
import com.quran.labs.androidquran.ui.fragment.TabletFragment
import com.quran.labs.androidquran.ui.fragment.TranslationFragment
import com.quran.mobile.di.QuranReadingPageComponent
import com.squareup.anvil.annotations.MergeSubcomponent
import dagger.BindsInstance
import dagger.Subcomponent

@QuranPageScope
@MergeSubcomponent(QuranReadingPageScope::class, modules = [QuranPageModule::class])
@MergeSubcomponent(QuranReadingPageScope::class)
interface QuranPageComponent: QuranReadingPageComponent {
fun inject(quranPageFragment: QuranPageFragment)
fun inject(tabletFragment: TabletFragment)
fun inject(translationFragment: TranslationFragment)

@Subcomponent.Builder
interface Builder {
fun withQuranPageModule(quranPageModule: QuranPageModule): Builder
fun build(): QuranPageComponent
@Subcomponent.Factory
interface Factory {
fun generate(@BindsInstance pages: IntArray): QuranPageComponent
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package com.quran.labs.androidquran.di.module.activity

import android.content.Context
import com.quran.data.core.QuranInfo
import com.quran.data.core.QuranPageInfo
import com.quran.data.di.ActivityScope
import com.quran.labs.androidquran.data.QuranDisplayData
import com.quran.labs.androidquran.ui.PagerActivity
import com.quran.labs.androidquran.ui.helpers.AyahSelectedListener
import com.quran.labs.androidquran.util.QuranPageInfoImpl
import com.quran.labs.androidquran.util.QuranScreenInfo
import com.quran.labs.androidquran.util.QuranUtils
import com.quran.labs.androidquran.util.TranslationUtil
import com.quran.mobile.di.AyahActionFragmentProvider
import com.quran.mobile.di.qualifier.ActivityContext
import dagger.Module
import dagger.Provides
import dagger.multibindings.ElementsIntoSet

@Module
class PagerActivityModule(private val pagerActivity: PagerActivity) {

@Provides
fun provideAyahSelectedListener(): AyahSelectedListener {
return pagerActivity
}
object PagerActivityModule {

@Provides
fun provideQuranPageInfo(
@ActivityContext context: Context,
quranInfo: QuranInfo,
quranDisplayData: QuranDisplayData
): QuranPageInfo {
return QuranPageInfoImpl(pagerActivity, quranInfo, quranDisplayData)
return QuranPageInfoImpl(context, quranInfo, quranDisplayData)
}

@Provides
@ActivityScope
fun provideImageWidth(screenInfo: QuranScreenInfo): String {
return if (QuranUtils.isDualPages(pagerActivity, screenInfo)) {
fun provideImageWidth(@ActivityContext context: Context, screenInfo: QuranScreenInfo): String {
return if (QuranUtils.isDualPages(context, screenInfo)) {
screenInfo.tabletWidthParam
} else {
screenInfo.widthParam
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.quran.labs.androidquran.di.module.application

import android.app.Application
import android.content.Context
import android.graphics.Point
import android.view.Display
Expand All @@ -15,6 +14,7 @@ import com.quran.labs.androidquran.data.QuranFileConstants
import com.quran.labs.androidquran.util.QuranFileUtils
import com.quran.labs.androidquran.util.QuranSettings
import com.quran.labs.androidquran.util.SettingsImpl
import com.quran.mobile.di.qualifier.ApplicationContext
import com.quran.mobile.di.ExtraPreferencesProvider
import com.quran.mobile.di.ExtraScreenProvider
import dagger.Module
Expand All @@ -28,15 +28,10 @@ import javax.inject.Named
import javax.inject.Singleton

@Module
class ApplicationModule(private val application: Application) {
object ApplicationModule {

@Provides
fun provideApplicationContext(): Context {
return application
}

@Provides
fun provideDisplay(appContext: Context): Display {
fun provideDisplay(@ApplicationContext appContext: Context): Display {
val w = appContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
return w.defaultDisplay
}
Expand All @@ -58,8 +53,8 @@ class ApplicationModule(private val application: Application) {

@Provides
@Singleton
fun provideQuranSettings(): QuranSettings {
return QuranSettings.getInstance(application)
fun provideQuranSettings(@ApplicationContext appContext: Context): QuranSettings {
return QuranSettings.getInstance(appContext)
}

@Provides
Expand Down Expand Up @@ -94,8 +89,8 @@ class ApplicationModule(private val application: Application) {
}

@Provides
fun provideCacheDirectory(): File {
return application.cacheDir
fun provideCacheDirectory(@ApplicationContext appContext: Context): File {
return appContext.cacheDir
}

@Provides
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.quran.data.model.bookmark.BookmarkData;
import com.quran.labs.androidquran.R;
import com.quran.labs.androidquran.database.BookmarksDBAdapter;
import com.quran.mobile.di.qualifier.ApplicationContext;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,7 +31,7 @@ public class BookmarkImportExportModel {
private final BookmarkModel bookmarkModel;

@Inject
BookmarkImportExportModel(Context appContext,
BookmarkImportExportModel(@ApplicationContext Context appContext,
BookmarkJsonModel model, BookmarkModel bookmarkModel) {
this.appContext = appContext;
this.jsonModel = model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import android.content.Context;
import android.database.Cursor;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.quran.data.core.QuranInfo;
import com.quran.data.model.QuranText;
import com.quran.data.model.SuraAyah;
import com.quran.data.model.bookmark.Bookmark;
import com.quran.labs.androidquran.data.QuranDataProvider;
import com.quran.labs.androidquran.data.QuranFileConstants;
import com.quran.data.model.SuraAyah;
import com.quran.labs.androidquran.database.DatabaseHandler;
import com.quran.labs.androidquran.database.DatabaseUtils;
import com.quran.labs.androidquran.util.QuranFileUtils;
import com.quran.mobile.di.qualifier.ApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -21,8 +25,6 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

Expand All @@ -39,7 +41,7 @@ public class ArabicDatabaseUtils {
private DatabaseHandler arabicDatabaseHandler;

@Inject
ArabicDatabaseUtils(Context context, QuranInfo quranInfo, QuranFileUtils quranFileUtils) {
ArabicDatabaseUtils(@ApplicationContext Context context, QuranInfo quranInfo, QuranFileUtils quranFileUtils) {
this.appContext = context;
this.quranInfo = quranInfo;
arabicDatabaseHandler = getArabicDatabaseHandler();
Expand Down
Loading

0 comments on commit 72dd7ee

Please sign in to comment.