Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / ScaffoldLoadingDetector.kt
ScaffoldLoadingDetector.kt141 lines · 7.1 KiBRaw
| 1 | package com.vayunmathur.lint |
| 2 | |
| 3 | import com.android.tools.lint.detector.api.Category |
| 4 | import com.android.tools.lint.detector.api.Detector |
| 5 | import com.android.tools.lint.detector.api.Implementation |
| 6 | import com.android.tools.lint.detector.api.Issue |
| 7 | import com.android.tools.lint.detector.api.JavaContext |
| 8 | import com.android.tools.lint.detector.api.Scope |
| 9 | import com.android.tools.lint.detector.api.Severity |
| 10 | import com.android.tools.lint.detector.api.SourceCodeScanner |
| 11 | import com.intellij.psi.PsiMethod |
| 12 | import org.jetbrains.kotlin.psi.KtFile |
| 13 | import org.jetbrains.uast.UCallExpression |
| 14 | |
| 15 | /** |
| 16 | * Flags standalone loading/refresh icons in app UI. |
| 17 | * |
| 18 | * Refresh indication is owned by the scaffold: `AppScaffold` (and the |
| 19 | * wrappers built on it — `DetailScaffold`, `DetailLazyColumn`, |
| 20 | * `LazyListScaffold`, `SetupScaffold`, `ListPage`) take |
| 21 | * `isRefreshing` / `onRefresh` / `refreshEnabled` and render a Material 3 |
| 22 | * pull-to-refresh indicator at the top while any refresh runs, with the |
| 23 | * content composed underneath. TV screens use the same pattern without a |
| 24 | * gesture via `TvAppFrame(isRefreshing = ...)`; `TvLoading` in |
| 25 | * `library:tv` is that family's sanctioned top indicator and is not |
| 26 | * flagged. No other loading icon belongs in a screen. |
| 27 | * |
| 28 | * Scope: a call is reported when it resolves to `LoadingState`, |
| 29 | * `LoadingIndicator`, `CircularProgressIndicator`, |
| 30 | * `ContainedLoadingIndicator`, `PullToRefreshBox` or `AppPullToRefreshBox` in `com.vayunmathur.library.*` (or the Material 3 |
| 31 | * originals behind them) and the file is in an app package |
| 32 | * (`com.vayunmathur.*` outside `com.vayunmathur.library.*`). There is no |
| 33 | * sized/button/section/determinate exemption any more and no requirement |
| 34 | * that the call sit inside a scaffold: whole/section loading reroutes to |
| 35 | * the scaffold (or a parent scaffold), truly local progress becomes a |
| 36 | * non-icon affordance (a disabled control, progress text), and real |
| 37 | * determinate progress becomes a determinate bar |
| 38 | * (`LinearProgressIndicator(progress = ...)`), which is not flagged. |
| 39 | * Hand-rolled pull-to-refresh containers in screens are likewise flagged: |
| 40 | * the scaffold's box replaces them. |
| 41 | * |
| 42 | * The shared library (`com.vayunmathur.library.*`) is exempt: the |
| 43 | * scaffolds and their indicators are built there. |
| 44 | * |
| 45 | * Per-file opt-out, reserved for library-internal helpers (already exempt |
| 46 | * by package) and genuinely non-screen contexts only: |
| 47 | * `// SCAFFOLD LOADING EXCEPTION: <reason>`. |
| 48 | */ |
| 49 | class ScaffoldLoadingDetector : Detector(), SourceCodeScanner { |
| 50 | |
| 51 | override fun getApplicableMethodNames(): List<String> = LOADING_CALLS.toList() |
| 52 | |
| 53 | override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { |
| 54 | if (!isLoadingCall(method)) return |
| 55 | |
| 56 | val packageName = context.uastFile?.packageName ?: return |
| 57 | if (!packageName.startsWith(APP_PACKAGE_PREFIX)) return |
| 58 | if (isLibraryPackage(packageName)) return |
| 59 | |
| 60 | val ktFile = context.uastFile?.sourcePsi as? KtFile |
| 61 | if (ktFile != null && CommentOnlyMatcher.containsMarkerInComments(ktFile, EXCEPTION_MARKER)) return |
| 62 | |
| 63 | context.report( |
| 64 | ISSUE, |
| 65 | node, |
| 66 | context.getLocation(node), |
| 67 | "Loading/refresh indication is owned by the scaffold: pass " + |
| 68 | "`isRefreshing = <refreshing>` (and `onRefresh`/`refreshEnabled` as needed) to " + |
| 69 | "AppScaffold / DetailScaffold / DetailLazyColumn / LazyListScaffold / " + |
| 70 | "SetupScaffold / ListPage (TV: TvAppFrame `isRefreshing`, or `TvLoading` " + |
| 71 | "as the TV scaffold family's top indicator) instead of rendering " + |
| 72 | "`${node.methodName}` in app UI. Whole/section loading reroutes to the scaffold " + |
| 73 | "or a parent scaffold; truly local progress uses a non-icon affordance " + |
| 74 | "(disabled state, progress text); real determinate progress uses a " + |
| 75 | "determinate `LinearProgressIndicator(progress = ...)` bar, not a spinner. " + |
| 76 | "The exception marker `$EXCEPTION_MARKER <reason>` is reserved for " + |
| 77 | "library-internal helpers and genuinely non-screen contexts.", |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | private fun isLoadingCall(method: PsiMethod): Boolean { |
| 82 | val containing = method.containingClass?.qualifiedName ?: return false |
| 83 | return containing.startsWith(LIBRARY_PREFIX) || |
| 84 | containing.startsWith(MATERIAL3_PREFIX) |
| 85 | } |
| 86 | |
| 87 | companion object { |
| 88 | private const val APP_PACKAGE_PREFIX = "com.vayunmathur." |
| 89 | private const val LIBRARY_PREFIX = "com.vayunmathur.library" |
| 90 | private const val MATERIAL3_PREFIX = "androidx.compose.material3" |
| 91 | |
| 92 | const val EXCEPTION_MARKER = "// SCAFFOLD LOADING EXCEPTION:" |
| 93 | |
| 94 | private val LOADING_CALLS = setOf( |
| 95 | "LoadingState", |
| 96 | "LoadingIndicator", |
| 97 | "CircularProgressIndicator", |
| 98 | "ContainedLoadingIndicator", |
| 99 | "PullToRefreshBox", |
| 100 | "AppPullToRefreshBox", |
| 101 | ) |
| 102 | |
| 103 | private fun isLibraryPackage(packageName: String): Boolean = |
| 104 | packageName == LIBRARY_PREFIX || packageName.startsWith("$LIBRARY_PREFIX.") |
| 105 | |
| 106 | val ISSUE: Issue = Issue.create( |
| 107 | id = "ScaffoldLoading", |
| 108 | briefDescription = "Standalone loading icon in app UI; scaffold owns refresh indication", |
| 109 | explanation = """ |
| 110 | Refresh indication belongs to the scaffold object. AppScaffold and the \ |
| 111 | shared wrappers built on it (DetailScaffold, DetailLazyColumn, \ |
| 112 | LazyListScaffold, SetupScaffold, ListPage) accept \ |
| 113 | `isRefreshing` / `onRefresh` / `refreshEnabled` and show a Material 3 \ |
| 114 | pull-to-refresh indicator at the top while any refresh runs, with the \ |
| 115 | content composed underneath; TV screens use TvAppFrame's \ |
| 116 | `isRefreshing` top indicator (no pull gesture). Rendering a \ |
| 117 | LoadingState, LoadingIndicator, CircularProgressIndicator, \ |
| 118 | ContainedLoadingIndicator, or a hand-rolled \ |
| 119 | PullToRefreshBox/AppPullToRefreshBox anywhere in app UI is flagged here, \ |
| 120 | whatever its size, slot, or determinacy. |
| 121 | |
| 122 | Whole/section loading reroutes to the scaffold or a parent scaffold. \ |
| 123 | Truly local progress uses a non-icon affordance (a disabled control, \ |
| 124 | progress text). Real determinate progress (uploads/downloads with a \ |
| 125 | percentage) is a determinate LinearProgressIndicator bar, which is not \ |
| 126 | flagged. The shared library modules (com.vayunmathur.library.*) are \ |
| 127 | exempt because the scaffolds and their indicators are built there. |
| 128 | |
| 129 | The marker comment `// SCAFFOLD LOADING EXCEPTION: <reason>` is reserved \ |
| 130 | for library-internal helpers and genuinely non-screen contexts only. |
| 131 | """.trimIndent(), |
| 132 | category = Category.CORRECTNESS, |
| 133 | priority = 6, |
| 134 | severity = Severity.ERROR, |
| 135 | implementation = Implementation( |
| 136 | ScaffoldLoadingDetector::class.java, |
| 137 | Scope.JAVA_FILE_SCOPE, |
| 138 | ), |
| 139 | ) |
| 140 | } |
| 141 | } |