Skip to content

Commit

Permalink
refactor: minor rule changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloNetrebchuk committed Nov 14, 2024
1 parent 8938457 commit e08e99a
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 58 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Detekt

on:
workflow_dispatch:
pull_request: { }

env:
GRADLE_OPTS: -Dorg.gradle.daemon=false
CI_GRADLE_ARG_PROPERTIES: --stacktrace

jobs:
linting:
name: Run Detekt
runs-on: ubuntu-latest

steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: 'temurin' # See 'Supported distributions' for available options
java-version: '17'

- name: Run Detekt
run: ./gradlew detektAll

- name: Upload report
uses: github/codeql-action/upload-sarif@v3
if: success() || failure()
with:
sarif_file: build/reports/detekt/detekt.sarif
2 changes: 1 addition & 1 deletion app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ val screenModule = module {
profileRouter = get(),
)
}
viewModel { (account: Account) -> EditProfileViewModel(get(), get(), get(), get(), account) }
viewModel { (account: Account) -> EditProfileViewModel(get(), get(), get(), get(), get(), account) }
viewModel { VideoSettingsViewModel(get(), get(), get(), get()) }
viewModel { (qualityType: String) -> VideoQualityViewModel(qualityType, get(), get(), get()) }
viewModel { DeleteProfileViewModel(get(), get(), get(), get(), get()) }
Expand Down
12 changes: 3 additions & 9 deletions config/detekt.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build:
maxIssues: 100
maxIssues: 0
weights:
complexity: 2
LongParameterList: 1
Expand Down Expand Up @@ -43,19 +43,13 @@ style:
active: true
ignoreAnnotated:
- 'Preview'
ForbiddenComment:
active: false
SerialVersionUIDInSerializableClass:
active: false

complexity:
active: true
LongMethod:
active: true
ignoreAnnotated: [ 'Composable' ]
ignoreFunction: [ 'onCreateView' ]
LargeClass:
active: false
LongParameterList:
active: true
functionThreshold: 15
Expand All @@ -64,8 +58,8 @@ complexity:
ignoreAnnotated: [ 'Composable' ]
TooManyFunctions:
active: true
thresholdInClasses: 30
thresholdInInterfaces: 30
thresholdInClasses: 21
thresholdInInterfaces: 20
ignoreAnnotatedFunctions: [ 'Composable' ]
ignoreOverridden: true
ignorePrivate: true
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/openedx/core/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.google.gson.JsonParser
import org.openedx.core.domain.model.AgreementUrls
import java.io.InputStreamReader

@Suppress("TooManyFunctions")
class Config(context: Context) {

private var configProperties: JsonObject = try {
Expand Down
10 changes: 7 additions & 3 deletions core/src/main/java/org/openedx/core/domain/model/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.openedx.core.domain.model

import java.io.Serializable
import com.google.gson.annotations.SerializedName

data class AppConfig(
val courseDatesCalendarSync: CourseDatesCalendarSync = CourseDatesCalendarSync(),
) : Serializable
)

data class CourseDatesCalendarSync(
@SerializedName("is_enabled")
val isEnabled: Boolean = false,
@SerializedName("is_self_paced_enabled")
val isSelfPacedEnabled: Boolean = false,
@SerializedName("is_instructor_paced_enabled")
val isInstructorPacedEnabled: Boolean = false,
@SerializedName("is_deep_link_enabled")
val isDeepLinkEnabled: Boolean = false,
) : Serializable
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ data class CourseDateBlock(
val assignmentType: String? = "",
) : Parcelable {
fun isCompleted(): Boolean {
return complete || (
dateType in setOf(
DateType.COURSE_START_DATE,
DateType.COURSE_END_DATE,
DateType.CERTIFICATE_AVAILABLE_DATE,
DateType.VERIFIED_UPGRADE_DEADLINE,
DateType.VERIFICATION_DEADLINE_DATE,
) && date.before(
Date()
)
)
val dateTypeInSet = dateType in setOf(
DateType.COURSE_START_DATE,
DateType.COURSE_END_DATE,
DateType.CERTIFICATE_AVAILABLE_DATE,
DateType.VERIFIED_UPGRADE_DEADLINE,
DateType.VERIFICATION_DEADLINE_DATE
)
return complete || (dateTypeInSet && date.before(Date()))
}

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class AppReviewManager(
isDialogShowed = true
val currentVersionName = reviewPreferences.formatVersionName(appData.versionName)
// Check is app wasn't positive rated AND 2 minor OR 1 major app versions passed since the last review
if (
!reviewPreferences.wasPositiveRated &&
(
currentVersionName.minorVersion - 2 >= reviewPreferences.lastReviewVersion.minorVersion ||
currentVersionName.majorVersion - 1 >= reviewPreferences.lastReviewVersion.majorVersion
)
) {
val minorVersionPassed =
currentVersionName.minorVersion - 2 >= reviewPreferences.lastReviewVersion.minorVersion
val majorVersionPassed =
currentVersionName.majorVersion - 1 >= reviewPreferences.lastReviewVersion.majorVersion
if (!reviewPreferences.wasPositiveRated && (minorVersionPassed || majorVersionPassed)) {
val dialog = RateDialogFragment.newInstance()
dialog.show(
supportFragmentManager,
Expand Down
7 changes: 1 addition & 6 deletions core/src/main/java/org/openedx/core/ui/WebContentScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ private fun WebViewContent(
request: WebResourceRequest?
): Boolean {
val clickUrl = request?.url?.toString() ?: ""
return if (clickUrl.isNotEmpty() &&
(
clickUrl.startsWith("http://") ||
clickUrl.startsWith("https://")
)
) {
return if (clickUrl.isNotEmpty() && clickUrl.startsWith("http")) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(clickUrl)))
true
} else if (clickUrl.startsWith("mailto:")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,7 @@ private fun HTMLContentView(
request: WebResourceRequest?
): Boolean {
val clickUrl = request?.url?.toString() ?: ""
return if (clickUrl.isNotEmpty() &&
(
clickUrl.startsWith("http://") ||
clickUrl.startsWith("https://")
)
) {
return if (clickUrl.isNotEmpty() && clickUrl.startsWith("http")) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(clickUrl)))
true
} else if (clickUrl.startsWith("mailto:")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,7 @@ private fun CourseDescription(
request: WebResourceRequest?
): Boolean {
val clickUrl = request?.url?.toString() ?: ""
return if (clickUrl.isNotEmpty() &&
(
clickUrl.startsWith("http://") ||
clickUrl.startsWith("https://")
)
) {
return if (clickUrl.isNotEmpty() && clickUrl.startsWith("http")) {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse(clickUrl))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.openedx.foundation.presentation.UIMessage
import org.openedx.foundation.system.ResourceManager
import java.net.UnknownHostException

@Suppress("LargeClass")
@OptIn(ExperimentalCoroutinesApi::class)
class DiscussionCommentsViewModelTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,9 @@ class EditProfileFragment : Fragment() {
fos.write(bitmapData)
fos.flush()
fos.close()
// TODO: get applicationId instead of packageName
return FileProvider.getUriForFile(
requireContext(),
requireContext().packageName + ".fileprovider",
viewModel.config.getAppId() + ".fileprovider",
newFile
)!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.openedx.core.R
import org.openedx.core.config.Config
import org.openedx.foundation.extension.isInternetError
import org.openedx.foundation.presentation.BaseViewModel
import org.openedx.foundation.presentation.UIMessage
Expand All @@ -24,6 +25,7 @@ class EditProfileViewModel(
private val resourceManager: ResourceManager,
private val notifier: ProfileNotifier,
private val analytics: ProfileAnalytics,
val config: Config,
account: Account,
) : BaseViewModel() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.openedx.core.R
import org.openedx.core.config.Config
import org.openedx.core.domain.model.ProfileImage
import org.openedx.foundation.presentation.UIMessage
import org.openedx.foundation.system.ResourceManager
Expand All @@ -43,6 +44,7 @@ class EditProfileViewModelTest {
private val interactor = mockk<ProfileInteractor>()
private val notifier = mockk<ProfileNotifier>()
private val analytics = mockk<ProfileAnalytics>()
private val config = mockk<Config>()

private val account = Account(
username = "thom84",
Expand Down Expand Up @@ -84,7 +86,7 @@ class EditProfileViewModelTest {
@Test
fun `updateAccount no internet connection`() = runTest {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
coEvery { interactor.updateAccount(any()) } throws UnknownHostException()
viewModel.updateAccount(emptyMap())
advanceUntilIdle()
Expand All @@ -99,7 +101,7 @@ class EditProfileViewModelTest {
@Test
fun `updateAccount unknown exception`() = runTest {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
coEvery { interactor.updateAccount(any()) } throws Exception()

viewModel.updateAccount(emptyMap())
Expand All @@ -115,7 +117,7 @@ class EditProfileViewModelTest {
@Test
fun `updateAccount success`() = runTest {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
coEvery { interactor.updateAccount(any()) } returns account
coEvery { notifier.send(any<AccountUpdated>()) } returns Unit
every { analytics.logEvent(any(), any()) } returns Unit
Expand All @@ -132,7 +134,7 @@ class EditProfileViewModelTest {
@Test
fun `updateAccountAndImage no internet connection`() = runTest {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
coEvery { interactor.setProfileImage(any(), any()) } throws UnknownHostException()
coEvery { interactor.updateAccount(any()) } returns account
coEvery { notifier.send(AccountUpdated()) } returns Unit
Expand All @@ -152,7 +154,7 @@ class EditProfileViewModelTest {
@Test
fun `updateAccountAndImage unknown exception`() = runTest {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
coEvery { interactor.setProfileImage(any(), any()) } throws Exception()
coEvery { interactor.updateAccount(any()) } returns account
coEvery { notifier.send(AccountUpdated()) } returns Unit
Expand All @@ -172,7 +174,7 @@ class EditProfileViewModelTest {
@Test
fun `updateAccountAndImage success`() = runTest {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
coEvery { interactor.setProfileImage(any(), any()) } returns Unit
coEvery { interactor.updateAccount(any()) } returns account
coEvery { notifier.send(any<AccountUpdated>()) } returns Unit
Expand All @@ -194,7 +196,7 @@ class EditProfileViewModelTest {
@Test
fun `setImageUri set new value`() {
val viewModel =
EditProfileViewModel(interactor, resourceManager, notifier, analytics, account)
EditProfileViewModel(interactor, resourceManager, notifier, analytics, config, account)
viewModel.setImageUri(mockk())

assert(viewModel.selectedImageUri.value != null)
Expand Down

0 comments on commit e08e99a

Please sign in to comment.