Can';在Android应用程序Kotlin中导入扩展功能

Can';在Android应用程序Kotlin中导入扩展功能,android,kotlin,extension-function,Android,Kotlin,Extension Function,在导入另一个Kotlin文件(Extensions.kt)中声明的扩展函数的早期遇到问题,从另一个类(ForecastsRepository.kt)调用扩展函数,它不编译,但当我删除它时,构建没有问题。显然,我需要它,不知道为什么进口它会成为一个问题 下面是课堂: import com.benmohammad.climatemvvm.base.Success import com.benmohammad.climatemvvm.custom.errors.ErrorHandler import

在导入另一个Kotlin文件(Extensions.kt)中声明的扩展函数的早期遇到问题,从另一个类(ForecastsRepository.kt)调用扩展函数,它不编译,但当我删除它时,构建没有问题。显然,我需要它,不知道为什么进口它会成为一个问题

下面是课堂:

import com.benmohammad.climatemvvm.base.Success
import com.benmohammad.climatemvvm.custom.errors.ErrorHandler
import com.benmohammad.climatemvvm.custom.errors.NoDataException
import com.benmohammad.climatemvvm.custom.errors.NoResponseException
import com.benmohammad.climatemvvm.entitymappers.forecasts.ForecastMapper
import com.benmohammad.climatemvvm.features.home.di.HomeScope
import com.benmohammad.climatemvvm.network.api.OpenWeatherApi
import com.benmohammad.climatemvvm.network.response.ErrorResponse
import com.benmohammad.climatemvvm.room.dao.forecasts.ForecastDao
import com.benmohammad.climatemvvm.room.dao.utils.StringKeyValueDao
import com.benmohammad.climatemvvm.room.models.forecasts.DbForecast
import com.benmohammad.climatemvvm.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import javax.inject.Inject
import com.benmohammad.climatemvvm.extensions.applyCommonSideEffects//import


@HomeScope
class ForecastsRepository @Inject constructor(
    private val openWeatherApi: OpenWeatherApi,
    private val forecastDao: ForecastDao,
    private val stringKeyValueDao: StringKeyValueDao
) {

    private val forecastCacheThresholdMillis = 3 * 3600000L //3 hours//

    fun getForecasts(cityId: Int) = flow {
        stringKeyValueDao.get(Utils.LAST_FORECASTS_API_CALL_TIMESTAMP)
            ?.takeIf { !Utils.shouldCallApi(it.value, forecastCacheThresholdMillis) }
            ?.let { emit(getDataOrError(NoDataException())) }
            ?: emit((getForecastFromAPI(cityId)))
    }
        //.applyCommonSideEffects()
        .catch {
            emit(getDataOrError(it))
        }

    private suspend fun getForecastFromAPI(cityId: Int) = openWeatherApi.getWeatherForecast(cityId)
        .run {
            if (isSuccessful && body() != null) {
                stringKeyValueDao.insert(
                    Utils.getCurrentTimeKeyValuePair(Utils.LAST_FORECASTS_API_CALL_TIMESTAMP)
                )
                forecastDao.deleteAllAndInsert(ForecastMapper(body()!!).map())
                getDataOrError(NoDataException())
            } else {
                Error(
                    NoResponseException(
                        ErrorHandler.parseError<ErrorResponse>(errorBody())?.message
                    )
                )
            }
        }

    private suspend fun getDataOrError(throwable: Throwable) =
        forecastDao.get()
            ?.let { dbValue -> Success(getForecastList(dbValue)) }
            ?: Error(throwable)

    private suspend fun getForecastList(dbForecast: DbForecast) = withContext(Dispatchers.Default) {
        dbForecast.list.map { it.forecast }
    }
}
import com.benmohammad.climatemvvm.base.Success
导入com.benmohammad.climatemvvm.custom.errors.ErrorHandler
导入com.benmohammad.climatemvvm.custom.errors.nodata异常
导入com.benmohammad.climatemvvm.custom.errors.NoResponseException
导入com.benmohammad.climatemvvm.entitymappers.forecast.ForecastMapper
导入com.benmohammad.climatemvvm.features.home.di.HomeScope
导入com.benmohammad.climatemvvm.network.api.OpenWeatherApi
导入com.benmohammad.climatemvvm.network.response.ErrorResponse
导入com.benmohammad.climatemvvm.room.dao.forecast.ForecastDao
导入com.benmohammad.climatemvvm.room.dao.utils.StringKeyValueDao
导入com.benmohammad.climatemvvm.room.models.forecast.DbForecast
导入com.benmohammad.climatemvvm.utils.utils
导入kotlinx.coroutines.Dispatchers
导入kotlinx.coroutines.flow.catch
导入kotlinx.coroutines.flow.flow
导入kotlinx.coroutines.withContext
导入javax.inject.inject
导入com.benmohammad.climatemvvm.extensions.applyCommonSideEffects//import
@家庭镜
类ForecastsRepository@Inject构造函数(
私有val openWeatherApi:openWeatherApi,
私人val预报道:预报道,
私有val stringKeyValueDao:stringKeyValueDao
) {
私人val ForecastCacheSthresholdMillis=3*3600000L//3小时//
fun getforecast(cityId:Int)=流量{
获取(Utils.LAST\u预测\u API\u调用\u时间戳)
?.takeIf{!Utils.shouldCallApi(it.value,forecastCacheSthresholdMillis)}
?.让{emit(getDataOrError(NoDataException())}
?:emit((getForecastFromAPI(cityId)))
}
//.applyCommonSideEffects()
.接住{
发出(getDataOrError(it))
}
私有getForecastFromAPI(cityId:Int)=openWeatherApi.getWeatherForecast(cityId)
.跑{
如果(isSuccessful&&body()!=null){
stringKeyValueDao.insert(
Utils.getCurrentTimeKeyValuePair(Utils.LAST\u预测\u API\u调用\u时间戳)
)
forecastDao.deleteAllAndInsert(ForecastMapper(body()!!).map())
getDataOrError(NoDataException())
}否则{
错误(
无应答异常(
ErrorHandler.parseError(errorBody())?.message
)
)
}
}
private suspend fun getDataOrError(可丢弃:可丢弃)=
forecastDao.get()
?.让{dbValue->Success(getForecastList(dbValue))}
?:错误(可丢弃)
私有getForecastList(dbForecast:dbForecast)=withContext(Dispatchers.Default){
dbForecast.list.map{it.forecast}
}
}
下面是扩展函数的文件:

package com.benmohammad.climatemvvm.extensions

import com.benmohammad.climatemvvm.base.Progress
import com.benmohammad.climatemvvm.base.Result
import com.benmohammad.climatemvvm.utils.Utils
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.retryWhen
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
import retrofit2.Retrofit
import java.io.IOException




fun String.capitalizeWords(): String = this.split(' ').joinToString(" "){it.capitalize()}

@PublishedApi
internal inline fun Retrofit.Builder.callFactory(crossinline body: (Request) -> Call) =
    callFactory(object: Call.Factory {
        override fun newCall(request: Request): Call = body(request)
    })

@Suppress("NOTHING_TO_INLINE")
inline fun Retrofit.Builder.delegatingCallFactory(delegate: dagger.Lazy<OkHttpClient>): Retrofit.Builder =
    callFactory {
        delegate.get().newCall(it) }

fun < T: Any> Flow<Result<T>>.applyCommonSideEffects() =  //<<-----------T H I S Function!!!!HERE
    retryWhen {  cause, attempt ->
        when {
            (cause is IOException && attempt < Utils.MAX_RETRIES) -> {
                delay(Utils.getBackOffDelay(attempt))
                true
            }
            else -> {
                false
            }
        }
    }
        .onStart { emit(Progress(isLoading = true)) }
        .onCompletion { emit(Progress(isLoading = false)) }




    fun Job?.cancelIfActive() {
        if(this?.isActive == true) {
        cancel()
    }
}
包com.benmohammad.climatemvvm.extensions
导入com.benmohammad.climatemvvm.base.Progress
导入com.benmohammad.climatemvvm.base.Result
导入com.benmohammad.climatemvvm.utils.utils
导入kotlinx.coroutines.Job
导入kotlinx.coroutines.delay
导入kotlinx.coroutines.flow.flow
导入kotlinx.coroutines.flow.onCompletion
导入kotlinx.coroutines.flow.onStart
导入kotlinx.coroutines.flow.retryWhen
导入okhttp3.Call
导入okhttp3.OkHttpClient
导入okhttp3.Request
进口改装2.改装
导入java.io.IOException
fun String.capitalizeWords():String=this.split(“”).joinToString(“”{it.capitalize()}
@达皮
内部内联fun改装.Builder.callFactory(交叉内联主体:(请求)->Call)=
callFactory(对象:Call.Factory{
重写fun newCall(请求:请求):Call=body(请求)
})
@抑制(“无内容到内联”)
内联fun改装.Builder.delegatingCallFactory(委托:dagger.Lazy):改装.Builder=
呼叫工厂{
delegate.get().newCall(it)}
乐趣Flow.applyCommonSideEffects()=//{
延迟(Utils.getBackOffDelay(尝试))
真的
}
其他->{
假的
}
}
}
.onStart{emit(Progress(isload=true))}
.onCompletion{emit(Progress(isLoading=false))}
有趣的工作?.cancelIfActive(){
if(this?.isActive==true){
取消
}
}
因为它没有编译,所以我觉得这个bug更深

IDE还增加了函数调用,说明它是“未解析引用”

谢谢你的建议,谢谢