Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
QuasselDroid-ng
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Janne Mareike Koschinski
QuasselDroid-ng
Commits
a26104e1
Verified
Commit
a26104e1
authored
4 years ago
by
Janne Mareike Koschinski
Browse files
Options
Downloads
Patches
Plain Diff
Experimenting with jetpack compose and flow
parent
d6650e90
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!2
Draft: Jetpack compose rewrite
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/build.gradle.kts
+2
-0
2 additions, 0 deletions
app/build.gradle.kts
app/src/main/java/de/kuschku/quasseldroid/MainActivity.kt
+203
-21
203 additions, 21 deletions
app/src/main/java/de/kuschku/quasseldroid/MainActivity.kt
with
205 additions
and
21 deletions
app/build.gradle.kts
+
2
−
0
View file @
a26104e1
...
...
@@ -60,6 +60,8 @@ dependencies {
val
androidxLifecycleVersion
:
String
by
project
.
extra
implementation
(
"androidx.lifecycle"
,
"lifecycle-runtime-ktx"
,
androidxLifecycleVersion
)
implementation
(
"org.threeten"
,
"threetenbp"
,
"1.4.0"
)
implementation
(
"io.coil-kt"
,
"coil"
,
"1.1.1"
)
implementation
(
"dev.chrisbanes.accompanist"
,
"accompanist-coil"
,
"0.5.0"
)
...
...
This diff is collapsed.
Click to expand it.
app/src/main/java/de/kuschku/quasseldroid/MainActivity.kt
+
203
−
21
View file @
a26104e1
...
...
@@ -2,37 +2,219 @@ package de.kuschku.quasseldroid
import
android.os.Bundle
import
androidx.appcompat.app.AppCompatActivity
import
androidx.compose.material.MaterialTheme
import
androidx.compose.material.Surface
import
androidx.compose.material.Text
import
androidx.compose.runtime.Composable
import
androidx.compose.foundation.border
import
androidx.compose.foundation.layout.*
import
androidx.compose.foundation.shape.CircleShape
import
androidx.compose.material.*
import
androidx.compose.material.icons.Icons
import
androidx.compose.material.icons.outlined.MoreVert
import
androidx.compose.runtime.*
import
androidx.compose.ui.Alignment
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.draw.clip
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.platform.setContent
import
androidx.compose.ui.text.AnnotatedString
import
androidx.compose.ui.text.SpanStyle
import
androidx.compose.ui.text.font.FontFamily
import
androidx.compose.ui.tooling.preview.Preview
import
androidx.compose.ui.unit.dp
import
de.kuschku.quasseldroid.ui.theme.QuasseldroidTheme
import
de.kuschku.quasseldroid.ui.theme.shapes
import
de.kuschku.quasseldroid.ui.theme.typography
import
dev.chrisbanes.accompanist.coil.CoilImage
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.delay
import
kotlinx.coroutines.flow.flow
import
org.threeten.bp.ZonedDateTime
import
org.threeten.bp.format.DateTimeFormatter
import
kotlin.random.Random
val
time
=
flow
<
ZonedDateTime
>
{
emit
(
ZonedDateTime
.
now
())
delay
(
1000
)
}
class
MainActivity
:
AppCompatActivity
()
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContent
{
QuasseldroidTheme
{
// A surface container using the 'background' color from the theme
Surface
(
color
=
MaterialTheme
.
colors
.
background
)
{
Greeting
(
"Android"
)
}
}
}
var
timer
:
Job
?
=
null
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContent
{
JetChat
()
}
}
}
fun
randomColor
():
Color
=
Color
(
red
=
Random
.
nextInt
(
0
,
255
),
green
=
Random
.
nextInt
(
0
,
255
),
blue
=
Random
.
nextInt
(
0
,
255
)
)
@Composable
fun
Greeting
(
name
:
String
)
{
Text
(
text
=
"Hello $name!"
)
fun
UserPhoto
(
imageUrl
:
String
,
modifier
:
Modifier
=
Modifier
,
)
{
val
ringColor
=
remember
{
randomColor
()
}
CoilImage
(
data
=
imageUrl
as
Any
,
contentDescription
=
null
,
modifier
=
modifier
.
border
(
2
.
dp
,
ringColor
,
CircleShape
)
.
padding
(
4
.
dp
)
.
clip
(
CircleShape
)
.
size
(
38
.
dp
)
)
}
fun
parseString
(
text
:
String
):
AnnotatedString
{
val
builder
=
AnnotatedString
.
Builder
()
var
monospace
=
false
var
lastIndex
=
0
fun
addText
(
index
:
Int
)
{
val
before
=
builder
.
length
builder
.
append
(
text
.
substring
(
lastIndex
,
index
))
val
after
=
builder
.
length
if
(
monospace
)
builder
.
addStyle
(
SpanStyle
(
fontFamily
=
FontFamily
.
Monospace
,
background
=
Color
.
Gray
,
),
before
,
after
)
lastIndex
=
index
+
1
}
for
(
i
in
text
.
indices
)
{
if
(
text
[
i
]
==
'`'
)
{
addText
(
i
)
monospace
=
!
monospace
}
}
addText
(
text
.
length
)
return
builder
.
toAnnotatedString
()
}
@Preview
(
showBackground
=
true
)
@Composable
fun
DefaultPreview
()
{
QuasseldroidTheme
{
Greeting
(
"Android"
)
fun
ChatText
(
text
:
String
,
modifier
:
Modifier
=
Modifier
)
{
Text
(
parseString
(
text
),
style
=
typography
.
body1
,
modifier
=
modifier
.
padding
(
8
.
dp
)
)
}
@Composable
fun
ChatBubble
(
content
:
@Composable
()
->
Unit
)
{
Surface
(
color
=
Color
.
LightGray
,
shape
=
shapes
.
medium
,
modifier
=
Modifier
.
padding
(
2
.
dp
)
)
{
content
()
}
}
@Composable
fun
ChatMessage
(
authorName
:
String
,
authorImageUrl
:
String
,
dateSent
:
String
,
content
:
@Composable
()
->
Unit
)
{
Row
{
UserPhoto
(
authorImageUrl
,
modifier
=
Modifier
.
padding
(
start
=
4
.
dp
,
top
=
4
.
dp
,
bottom
=
4
.
dp
,
end
=
12
.
dp
)
)
Column
{
Row
(
verticalAlignment
=
Alignment
.
CenterVertically
)
{
Text
(
authorName
,
style
=
typography
.
h6
)
Spacer
(
modifier
=
Modifier
.
padding
(
4
.
dp
))
Text
(
dateSent
,
style
=
typography
.
body2
.
copy
(
color
=
MaterialTheme
.
colors
.
onSurface
)
)
}
content
()
}
}
\ No newline at end of file
}
}
@Composable
fun
InputLine
()
{
var
text
by
remember
{
mutableStateOf
(
""
)
}
Row
{
Box
{
TextField
(
value
=
text
,
onValueChange
=
{
text
=
it
},
textStyle
=
typography
.
body1
,
)
if
(
text
.
isEmpty
())
{
Text
(
"Send a message"
)
}
}
Icon
(
Icons
.
Outlined
.
MoreVert
,
"more"
)
}
}
@Composable
fun
TimeDisplay
()
{
val
time
:
ZonedDateTime
by
time
.
collectAsState
(
ZonedDateTime
.
now
())
Text
(
time
.
format
(
DateTimeFormatter
.
ISO_DATE_TIME
))
}
@Preview
@Composable
fun
JetChat
()
{
QuasseldroidTheme
{
Scaffold
(
topBar
=
{
TimeDisplay
()
},
bottomBar
=
{
InputLine
()
},
bodyContent
=
{
Column
{
ChatMessage
(
authorName
=
"Ali Conors"
,
authorImageUrl
=
"https://picsum.photos/300/300"
,
dateSent
=
"3:50 PM"
,
)
{
ChatBubble
{
ChatText
(
"Yeah i’ve been mainly referring to the JetNews Sample :+1:"
)
}
}
ChatMessage
(
authorName
=
"Taylor Brooks"
,
authorImageUrl
=
"https://picsum.photos/300/300"
,
dateSent
=
"10:50 PM"
,
)
{
ChatBubble
{
ChatText
(
"Take a look at the `Flow.collectAsState()` APIs"
)
}
ChatBubble
{
ChatText
(
"You can use all the same stuff"
)
}
}
}
}
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment