Skip to content

Commit

Permalink
gradle: 引入Design库
Browse files Browse the repository at this point in the history
  • Loading branch information
XayahSuSuSu committed Mar 28, 2022
1 parent 9e6fe41 commit ef47474
Show file tree
Hide file tree
Showing 33 changed files with 1,242 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ dependencies {
def lifecycle_version = "2.4.1"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

// Design
implementation project(":design")
}
2 changes: 2 additions & 0 deletions design/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
/release
58 changes: 58 additions & 0 deletions design/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}

android {
namespace 'com.xayah.design'
compileSdk 32

viewBinding {
enabled = true
}

dataBinding {
enabled = true
}

defaultConfig {
minSdk 26
targetSdk 32

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

def lifecycle_version = "2.4.1"
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
// ViewModel utilities for Compose
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
}
Empty file added design/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions design/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xayah.design

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.xayah.design.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions design/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
61 changes: 61 additions & 0 deletions design/src/main/java/com/xayah/design/ActionLabel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.xayah.design

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import com.xayah.design.databinding.ComponentActionLabelBinding

class ActionLabel @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0
) : FrameLayout(context, attributeSet, defStyleAttr, defStyleRes) {
private val binding = ComponentActionLabelBinding
.inflate(LayoutInflater.from(context), this, true)

var icon: Drawable?
get() = binding.iconView.background
set(value) {
binding.iconView.background = value
}

var text: CharSequence?
get() = binding.textView.text
set(value) {
binding.textView.text = value
}

var subtext: CharSequence?
get() = binding.subtextView.text
set(value) {
binding.subtextView.text = value
binding.subtextView.visibility = if (value == null) View.GONE else View.VISIBLE
}

override fun setOnClickListener(l: OnClickListener?) {
binding.root.setOnClickListener(l)
}

init {
context.theme.obtainStyledAttributes(
attributeSet,
R.styleable.ActionLabel,
defStyleAttr,
defStyleRes
).apply {
try {
icon = getDrawable(R.styleable.ActionLabel_icon)
text = getString(R.styleable.ActionLabel_text)
subtext = getString(R.styleable.ActionLabel_subtext)
} finally {
recycle()
}
}
}
}
71 changes: 71 additions & 0 deletions design/src/main/java/com/xayah/design/LargeActionCard.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.xayah.design

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.annotation.AttrRes
import androidx.annotation.DimenRes
import com.google.android.material.card.MaterialCardView
import com.xayah.design.databinding.ComponentLargeActionLabelBinding
import com.xayah.design.util.resolveClickableAttrs
import com.xayah.design.util.selectableItemBackground

class LargeActionCard @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0
) : MaterialCardView(context, attributeSet, defStyleAttr) {
private val binding = ComponentLargeActionLabelBinding
.inflate(LayoutInflater.from(context), this, true)

var text: CharSequence?
get() = binding.textView.text
set(value) {
binding.textView.text = value
}

var subtext: CharSequence?
get() = binding.subtextView.text
set(value) {
binding.subtextView.text = value
}

var icon: Drawable?
get() = binding.iconView.background
set(value) {
binding.iconView.background = value
}

init {
context.resolveClickableAttrs(attributeSet, defStyleAttr) {
isFocusable = focusable(true)
isClickable = clickable(true)
foreground = foreground() ?: context.selectableItemBackground
}

context.theme.obtainStyledAttributes(
attributeSet,
R.styleable.LargeActionCard,
defStyleAttr,
0
).apply {
try {
icon = getDrawable(R.styleable.LargeActionCard_icon)
text = getString(R.styleable.LargeActionCard_text)
subtext = getString(R.styleable.LargeActionCard_subtext)
} finally {
recycle()
}
}

minimumHeight = getPixels(R.dimen.large_action_card_min_height)
radius = getPixels(R.dimen.large_action_card_radius).toFloat()
// elevation = getPixels(R.dimen.large_action_card_elevation).toFloat()
}

private fun getPixels(@DimenRes resId: Int): Int {
return resources.getDimensionPixelSize(resId)
}

}
74 changes: 74 additions & 0 deletions design/src/main/java/com/xayah/design/LargeActionLabel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.xayah.design

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import com.xayah.design.databinding.ComponentLargeActionLabelBinding
import com.xayah.design.util.resolveClickableAttrs
import com.xayah.design.util.selectableItemBackground

class LargeActionLabel @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0
) : FrameLayout(context, attributeSet, defStyleAttr, defStyleRes) {
private val binding = ComponentLargeActionLabelBinding
.inflate(LayoutInflater.from(context), this, true)

var icon: Drawable?
get() = binding.iconView.background
set(value) {
binding.iconView.background = value
}

var text: CharSequence?
get() = binding.textView.text
set(value) {
binding.textView.text = value
}

var subtext: CharSequence?
get() = binding.subtextView.text
set(value) {
binding.subtextView.text = value

if (value == null) {
binding.subtextView.visibility = View.GONE
} else {
binding.subtextView.visibility = View.VISIBLE
}
}

init {
context.resolveClickableAttrs(
attributeSet,
defStyleAttr,
defStyleRes
) {
isFocusable = focusable(true)
isClickable = clickable(true)
background = background() ?: context.selectableItemBackground
}

context.theme.obtainStyledAttributes(
attributeSet,
R.styleable.LargeActionLabel,
defStyleAttr,
defStyleRes
).apply {
try {
icon = getDrawable(R.styleable.LargeActionLabel_icon)
text = getString(R.styleable.LargeActionLabel_text)
subtext = getString(R.styleable.LargeActionLabel_subtext)
} finally {
recycle()
}
}
}
}
53 changes: 53 additions & 0 deletions design/src/main/java/com/xayah/design/adapter/ViewAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.xayah.design.adapter

import android.widget.CompoundButton
import androidx.databinding.BindingAdapter
import com.xayah.design.preference.Clickable
import com.xayah.design.preference.EditableText
import com.xayah.design.preference.SelectableList
import com.xayah.design.preference.Switch

class ViewAdapter {
companion object {
@BindingAdapter("items")
@JvmStatic
fun setItems(v: SelectableList, array: Array<String>?) {
if (array != null) {
v.setItems(array)
}
}

@BindingAdapter("onConfirm")
@JvmStatic
fun setSelectableListOnConfirmListener(
v: SelectableList,
listener: ((v: SelectableList, choice: Int) -> Unit)
) {
v.setOnConfirmListener(listener)
}

@BindingAdapter("onConfirm")
@JvmStatic
fun setEditableTextOnConfirmListener(
v: EditableText,
listener: ((v: EditableText, content: CharSequence?) -> Unit)
) {
v.setOnConfirmListener(listener)
}

@BindingAdapter("onCheckedChange")
@JvmStatic
fun setOnCheckedChangeListener(
v: Switch,
listener: ((buttonView: CompoundButton, isChecked: Boolean) -> Unit)
) {
v.setOnCheckedChangeListener(listener)
}

@BindingAdapter("isRound")
@JvmStatic
fun setRound(view: Clickable, isRound: Boolean) {
view.setRound(isRound)
}
}
}
Loading

0 comments on commit ef47474

Please sign in to comment.