-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use com.google.android.material.appbar.MaterialToolbar
The previous code used `androidx.appcompat.widget.Toolbar` in a several places. It's better to use `MaterialToolbar` as that plays better with other Material components. Update the usage throughout the project. In addition, implement a lint check that will prevent any future use from creeping back in. Fixes #28
- Loading branch information
Showing
14 changed files
with
211 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
plugins { | ||
id "java-library" | ||
id "kotlin" | ||
id "com.android.lint" | ||
} | ||
|
||
lintOptions { | ||
htmlReport(true) | ||
htmlOutput(file("lint-report.html")) | ||
textReport(true) | ||
absolutePaths(false) | ||
ignoreTestSources(true) | ||
} | ||
|
||
jar { | ||
manifest { | ||
attributes("Lint-Registry-v2": "app.pachli.lint.checks.LintRegistry") | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61") | ||
|
||
// Derived from the AGP version. If the AGP version is X.Y.Z the version | ||
// here must by X+23.Y.Z. | ||
// See https://github.com/googlesamples/android-custom-lint-rules#lint-version | ||
def lintVersion = "31.1.2" // = AGP 8.1.2 | ||
|
||
// For a description of the below dependencies, see the main project README | ||
compileOnly("com.android.tools.lint:lint-api:$lintVersion") | ||
compileOnly("com.android.tools.lint:lint-checks:$lintVersion") | ||
testImplementation("com.android.tools.lint:lint:$lintVersion") | ||
testImplementation("com.android.tools.lint:lint-tests:$lintVersion") | ||
|
||
testImplementation("junit:junit:4.13.2") | ||
} |
58 changes: 58 additions & 0 deletions
58
checks/src/main/java/app/pachli/lint/checks/AndroidxToolbarDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package app.pachli.lint.checks | ||
|
||
import com.android.tools.lint.detector.api.Category | ||
import com.android.tools.lint.detector.api.Detector | ||
import com.android.tools.lint.detector.api.Implementation | ||
import com.android.tools.lint.detector.api.Issue | ||
import com.android.tools.lint.detector.api.LintFix | ||
import com.android.tools.lint.detector.api.Scope | ||
import com.android.tools.lint.detector.api.Severity | ||
import com.android.tools.lint.detector.api.XmlContext | ||
import com.android.tools.lint.detector.api.XmlScanner | ||
import org.w3c.dom.Element | ||
|
||
class AndroidxToolbarDetector : Detector(), XmlScanner { | ||
override fun getApplicableElements(): Collection<String> { | ||
return listOf(ANDROIDX_TOOLBAR) | ||
} | ||
|
||
override fun visitElement(context: XmlContext, element: Element) { | ||
val quickFixData = LintFix.create() | ||
.name("Replace with `com.google.android.material.appbar.MaterialToolbar`") | ||
.replace() | ||
.text(element.localName) | ||
.with("com.google.android.material.appbar.MaterialToolbar") | ||
.independent(true) | ||
.build() | ||
|
||
context.report( | ||
issue = ISSUE, | ||
scope = element, | ||
location = context.getElementLocation(element), | ||
message = "Use `com.google.android.material.appbar.MaterialToolbar` instead of `androidx.appcompat.widget.Toolbar`", | ||
quickfixData = quickFixData, | ||
) | ||
} | ||
|
||
companion object { | ||
private const val ANDROIDX_TOOLBAR = "androidx.appcompat.widget.Toolbar" | ||
|
||
@JvmField | ||
val ISSUE: Issue = Issue.create( | ||
id = "AndroidxToolbarDetector", | ||
briefDescription = "Don't use `androidx.appcompat.widget.Toolbar` in this project", | ||
explanation = """ | ||
Use `com.google.android.material.appbar.MaterialToolbar` instead of | ||
`androidx.appcompat.widget.Toolbar` to ensure it works as expected with | ||
other Material components. See https://developer.android.com/reference/com/google/android/material/appbar/MaterialToolbar | ||
""", | ||
category = Category.CORRECTNESS, | ||
priority = 6, | ||
severity = Severity.WARNING, | ||
implementation = Implementation( | ||
AndroidxToolbarDetector::class.java, | ||
Scope.RESOURCE_FILE_SCOPE, | ||
), | ||
) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
checks/src/main/java/app/pachli/lint/checks/LintRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package app.pachli.lint.checks | ||
|
||
import com.android.tools.lint.client.api.IssueRegistry | ||
import com.android.tools.lint.detector.api.CURRENT_API | ||
import com.android.tools.lint.detector.api.Issue | ||
|
||
@Suppress("UnstableApiUsage") | ||
class LintRegistry : IssueRegistry() { | ||
override val issues: List<Issue> | ||
get() = listOf(AndroidxToolbarDetector.ISSUE) | ||
|
||
override val api: Int | ||
get() = CURRENT_API | ||
} |
89 changes: 89 additions & 0 deletions
89
checks/src/test/java/app/pachli/lint/checks/AndroidxToolbarDetectorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package app.pachli.lint.checks | ||
|
||
import com.android.tools.lint.checks.infrastructure.TestFiles.xml | ||
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint | ||
import org.junit.Test | ||
|
||
class AndroidxToolbarDetectorTest { | ||
@Test | ||
fun testError() { | ||
lint().files( | ||
xml( | ||
"res/layout/test.xml", | ||
"""<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="app.pachli.components.search.SearchActivity"> | ||
<com.google.android.material.appbar.AppBarLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:liftOnScroll="true" | ||
app:liftOnScrollTargetViewId="@id/pages" | ||
app:layout_collapseMode="pin"> | ||
<androidx.appcompat.widget.Toolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
app:contentInsetStartWithNavigation="0dp" | ||
app:layout_scrollFlags="scroll|snap|enterAlways" | ||
app:navigationIcon="?attr/homeAsUpIndicator" /> | ||
</com.google.android.material.appbar.AppBarLayout> | ||
</androidx.coordinatorlayout.widget.CoordinatorLayout> | ||
""", | ||
).indented(), | ||
).issues(AndroidxToolbarDetector.ISSUE).allowMissingSdk().run().expectWarningCount(1) | ||
.expect( | ||
"""res/layout/test.xml:16: Warning: Use com.google.android.material.appbar.MaterialToolbar instead of androidx.appcompat.widget.Toolbar [AndroidxToolbarDetector] | ||
<androidx.appcompat.widget.Toolbar | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
0 errors, 1 warnings | ||
""", | ||
) | ||
} | ||
|
||
@Test | ||
fun testNoError() { | ||
lint().files( | ||
xml( | ||
"res/layout/test.xml", | ||
"""<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="app.pachli.components.search.SearchActivity"> | ||
<com.google.android.material.appbar.AppBarLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_collapseMode="pin" | ||
app:liftOnScroll="true" | ||
app:liftOnScrollTargetViewId="@id/pages"> | ||
<com.google.android.material.appbar.MaterialToolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
app:contentInsetStartWithNavigation="0dp" | ||
app:layout_scrollFlags="scroll|snap|enterAlways" | ||
app:navigationIcon="?attr/homeAsUpIndicator" /> | ||
</com.google.android.material.appbar.AppBarLayout> | ||
</androidx.coordinatorlayout.widget.CoordinatorLayout> | ||
""", | ||
).indented(), | ||
).issues(AndroidxToolbarDetector.ISSUE) | ||
.allowMissingSdk() | ||
.run() | ||
.expectWarningCount(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters