Skip to content

Commit 8ef724c

Browse files
author
kyle
committed
init
0 parents  commit 8ef724c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1303
-0
lines changed

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdk 30
8+
buildToolsVersion "30.0.3"
9+
10+
defaultConfig {
11+
applicationId "com.example.mycompose"
12+
minSdk 28
13+
targetSdk 30
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
32+
useIR = true
33+
}
34+
buildFeatures {
35+
compose true
36+
}
37+
composeOptions {
38+
kotlinCompilerExtensionVersion compose_version
39+
kotlinCompilerVersion '1.4.30'
40+
}
41+
}
42+
43+
dependencies {
44+
45+
implementation 'androidx.core:core-ktx:1.3.2'
46+
implementation 'androidx.appcompat:appcompat:1.2.0'
47+
implementation 'com.google.android.material:material:1.2.1'
48+
implementation "androidx.compose.ui:ui:$compose_version"
49+
implementation "androidx.compose.material:material:$compose_version"
50+
implementation "androidx.compose.ui:ui-tooling:$compose_version"
51+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
52+
implementation 'androidx.activity:activity-compose:1.3.0-alpha05'
53+
testImplementation 'junit:junit:4.+'
54+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
55+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
56+
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
57+
58+
implementation 'com.google.accompanist:accompanist-glide:0.7.1'
59+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.mycompose
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.example.mycompose", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.mycompose">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.MyCompose">
12+
<activity
13+
android:name=".MainActivity"
14+
android:exported="true"
15+
android:label="@string/app_name"
16+
android:theme="@style/Theme.MyCompose.NoActionBar">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.mycompose
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.lazy.LazyColumn
7+
import androidx.compose.material.Text
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.ui.tooling.preview.Preview
10+
import com.example.mycompose.case2.ContactListItem
11+
import com.example.mycompose.case2.mockData
12+
import com.example.mycompose.ui.theme.MyComposeTheme
13+
14+
class MainActivity : ComponentActivity() {
15+
override fun onCreate(savedInstanceState: Bundle?) {
16+
super.onCreate(savedInstanceState)
17+
setContent {
18+
var dataSource = mockData()
19+
LazyColumn() {
20+
dataSource.forEach { contact ->
21+
item {
22+
ContactListItem(contact)
23+
}
24+
}
25+
}
26+
}
27+
28+
}
29+
}
30+
31+
@Composable
32+
fun Greeting(name: String) {
33+
Text(text = "Hello $name!")
34+
}
35+
36+
@Preview(showBackground = true)
37+
@Composable
38+
fun DefaultPreview() {
39+
MyComposeTheme {
40+
Greeting("Android")
41+
}
42+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.mycompose.case1
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.layout.*
5+
import androidx.compose.material.Text
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.Alignment
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.graphics.Color
10+
import androidx.compose.ui.text.style.TextAlign
11+
import androidx.compose.ui.tooling.preview.Preview
12+
import androidx.compose.ui.unit.dp
13+
14+
15+
@Preview("ColumnLayout")
16+
@Composable
17+
fun ColumnLayout() {
18+
Column(
19+
modifier = Modifier
20+
.padding(16.dp)
21+
.size(150.dp)
22+
.background(Color.Blue),
23+
verticalArrangement = Arrangement.Center,
24+
horizontalAlignment = Alignment.CenterHorizontally
25+
) {
26+
Text(
27+
text = "Text1",
28+
modifier = Modifier
29+
.weight(1f)
30+
.fillMaxWidth(),
31+
textAlign = TextAlign.Center
32+
)
33+
Text(
34+
text = "Text2", modifier = Modifier
35+
.weight(1f)
36+
.fillMaxWidth()
37+
)
38+
Text(
39+
text = "Text3", modifier = Modifier
40+
.weight(1f)
41+
.fillMaxWidth()
42+
)
43+
}
44+
}
45+
46+
@Preview("rowLayout")
47+
@Composable
48+
fun RowLayout() {
49+
Row(
50+
modifier = Modifier
51+
.size(200.dp)
52+
.background(Color.Blue)
53+
) {
54+
Text(text = "Text1")
55+
Text(text = "Text2")
56+
Text(text = "Text3")
57+
}
58+
}
59+
60+
@Preview("box")
61+
@Composable
62+
fun BoxLayout() {
63+
Box(modifier = Modifier.size(200.dp)) {
64+
Text(text = "text1")
65+
Text(text = "text2")
66+
}
67+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.example.mycompose.case2
2+
3+
import android.graphics.Color.pack
4+
import androidx.compose.foundation.background
5+
import androidx.compose.foundation.clickable
6+
import androidx.compose.foundation.layout.*
7+
import androidx.compose.foundation.shape.CircleShape
8+
import androidx.compose.material.Text
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.ui.Alignment
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.graphics.Color
13+
import androidx.compose.ui.tooling.preview.Preview
14+
import androidx.compose.ui.tooling.preview.PreviewParameter
15+
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
16+
import androidx.compose.ui.unit.dp
17+
import androidx.compose.ui.unit.sp
18+
import java.util.*
19+
20+
21+
@Preview
22+
@Composable
23+
fun ContactListItem(
24+
@PreviewParameter(ContactDefault::class) contact: Contact
25+
) {
26+
Row(
27+
modifier = Modifier
28+
.height(60.dp)
29+
.fillMaxWidth()
30+
.background(Color.White)
31+
.clickable {
32+
33+
}
34+
.padding(6.dp),
35+
verticalAlignment = Alignment.CenterVertically
36+
) {
37+
38+
Text(
39+
text = "",
40+
modifier = Modifier
41+
.size(40.dp, 40.dp)
42+
.background(color = contact.userIcon, shape = CircleShape),
43+
)
44+
45+
Column(Modifier.padding(10.dp, 0.dp, 0.dp, 0.dp)) {
46+
Text(text = contact.userName, fontSize = 16.sp)
47+
Text(text = contact.email, fontSize = 10.sp, color = Color(0x52030303))
48+
}
49+
}
50+
}
51+
52+
53+
class ContactDefault : PreviewParameterProvider<Contact> {
54+
override val values = sequenceOf(Contact("zhongliuwang", "1662088888", "[email protected]", Color.Red))
55+
}
56+
57+
58+
data class Contact(
59+
var userName: String,
60+
var phoneNumber: String = "",
61+
var email: String,
62+
var userIcon: Color
63+
)
64+
65+
fun mockData(): List<Contact> {
66+
var contacts = mutableListOf<Contact>()
67+
for (index in 0..100) {
68+
contacts.add(
69+
Contact(
70+
"H${index}",
71+
"${10000 + index}",
72+
"${133343 + index}@qq.com",
73+
userIcon = randomColor()
74+
)
75+
)
76+
}
77+
return contacts
78+
}
79+
80+
fun randomColor(): Color {
81+
var random = Random()
82+
return Color(random.nextInt(256), random.nextInt(256), random.nextInt(256), 0xFF)
83+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.mycompose.case3
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material.OutlinedTextField
7+
import androidx.compose.material.Text
8+
import androidx.compose.runtime.*
9+
import androidx.compose.runtime.saveable.rememberSaveable
10+
import androidx.compose.ui.Modifier
11+
import androidx.compose.ui.tooling.preview.Preview
12+
import androidx.compose.ui.unit.dp
13+
14+
@Preview("state_manager")
15+
@Composable
16+
fun StateManagerView() {
17+
18+
var name by rememberSaveable { mutableStateOf("") }
19+
20+
Column(
21+
modifier = Modifier
22+
.fillMaxSize()
23+
.padding(16.dp)
24+
) {
25+
Text(text = "Hello")
26+
OutlinedTextField(value = name,
27+
onValueChange = { name = it },
28+
label = { Text(text = "name") }
29+
)
30+
}
31+
}

0 commit comments

Comments
 (0)