-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EuphonyTxPanel Component & Documentation #262
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b9676e9
Add EuphonyTxPanel (#231)
zion830 83a76b1
Remove EuphonyTxPanel preview
zion830 37e1b61
Add TxPanelViewModelTest
zion830 1a16ad8
Add document about Euphony UI
zion830 5abf9b9
Add Euphony UI link in README
zion830 f4099cb
Change screenshot image url
zion830 953ff77
Change screenshot width size
zion830 ae49922
Add EuphonyTxPanelKtTest ui test
zion830 8988d46
Add cornerRadius as a new argument of EuphonyTxPanel
zion830 15c0d37
Fix panel height error
zion830 286413d
Merge branch 'master' of https://github.com/euphony-io/euphony
zion830 9be03e5
Apply EuTxManager's latest singleton pattern patch
zion830 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Euphony Ui | ||
|
||
## Introduction | ||
|
||
Euphony provides basic UI components for easy use. These work based on Jetpack Compose. | ||
|
||
## How to use | ||
|
||
1. [Add Jetpack Compose](https://developer.android.com/jetpack/compose/interop/adding) to your | ||
project. | ||
2. Declare Euphony UI where necessary | ||
```kotlin | ||
@Composable | ||
fun MainScreen() { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxHeight() | ||
.fillMaxWidth() | ||
.padding(horizontal = 16.dp, vertical = 40.dp) | ||
) { | ||
EuphonyTxPanel( | ||
hint = "hint text", | ||
textColor = Red, | ||
panelHeight = 60.dp | ||
) | ||
} | ||
} | ||
``` | ||
|
||
## Components | ||
|
||
### EuphonyTxPanel | ||
|
||
Write text to transmit and click button to create sound waves. | ||
|
||
#### Preview | ||
|
||
<img src='https://github.com/zion830/euphony/blob/master/assets/eutxpanel_screenshot.png?raw=true' width='350px' /> | ||
|
||
#### Options | ||
|
||
|Name|Type|Description| | ||
|---|---|---| | ||
|hint|String|Hint text in the the text field.| | ||
|textColor|Color|Text color of the text field. Default color is Black.| | ||
|hintTextColor|Color|Text color of the hint text. Default color is LightGray.| | ||
|textFieldBackgroundColor|Color|Background color of the text field. Default color is LightSkyBlue.| | ||
|buttonBackgroundColor|Color|Background color of the right side button. Default color is LightBlue.| | ||
|panelHeight|Dp|Height of this component.| | ||
|cornerRadius|Dp|Corner radius of this component.| | ||
|iconTintColor|Color|Icon color of the right side button. Default color is White.| |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions
47
euphony/src/androidTest/java/co/euphony/ui/EuphonyTxPanelKtTest.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,47 @@ | ||
package co.euphony.ui | ||
|
||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.test.* | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import co.euphony.common.Constants | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.junit.MockitoJUnitRunner | ||
|
||
|
||
@ExperimentalMaterialApi | ||
@ExperimentalComposeUiApi | ||
@RunWith(MockitoJUnitRunner::class) | ||
class EuphonyTxPanelKtTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Before | ||
fun setup() { | ||
composeTestRule.setContent { | ||
EuphonyTxPanel( | ||
hint = "hint text" | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun haveEmptyValueAtFirst() { | ||
composeTestRule.onNodeWithText("hint text").assertExists() | ||
composeTestRule.onNodeWithContentDescription(Constants.TAG_TX_ICON_START).assertExists() | ||
} | ||
|
||
@Test | ||
fun startSendTextAndFinishSending() { | ||
composeTestRule.onNodeWithText("").performTextInput("Text") | ||
composeTestRule.onNodeWithTag(Constants.TAG_TX_BTN).performClick() | ||
composeTestRule.onNodeWithContentDescription(Constants.TAG_TX_ICON_STOP).assertExists() | ||
|
||
composeTestRule.onNodeWithTag(Constants.TAG_TX_BTN).performClick() | ||
composeTestRule.onNodeWithContentDescription(Constants.TAG_TX_ICON_START).assertExists() | ||
} | ||
} |
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,100 @@ | ||
package co.euphony.ui | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.* | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Close | ||
import androidx.compose.material.icons.filled.Send | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Color.Companion.Black | ||
import androidx.compose.ui.graphics.Color.Companion.LightGray | ||
import androidx.compose.ui.graphics.Color.Companion.White | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import co.euphony.common.Constants.* | ||
import co.euphony.tx.EuTxManager | ||
import co.euphony.ui.theme.LightBlue | ||
import co.euphony.ui.theme.LightSkyBlue | ||
import co.euphony.ui.viewmodel.TxPanelViewModel | ||
import co.euphony.ui.viewmodel.TxPanelViewModelFactory | ||
|
||
@Composable | ||
fun EuphonyTxPanel( | ||
hint: String = "", | ||
textColor: Color = Black, | ||
hintTextColor: Color = LightGray, | ||
textFieldBackgroundColor: Color = LightSkyBlue, | ||
buttonBackgroundColor: Color = LightBlue, | ||
panelHeight: Dp = 54.dp, | ||
cornerRadius: Dp = 8.dp, | ||
iconTintColor: Color = White | ||
) { | ||
val viewModel: TxPanelViewModel = | ||
viewModel(factory = TxPanelViewModelFactory(EuTxManager.getInstance())) | ||
|
||
val btnStatus = viewModel.isProcessing.collectAsState() | ||
var textData by remember { mutableStateOf("") } | ||
|
||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.height(panelHeight), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
TextField( | ||
modifier = Modifier | ||
.weight(1f) | ||
.height(panelHeight), | ||
value = textData, | ||
onValueChange = { | ||
textData = it | ||
}, | ||
shape = RoundedCornerShape(topStart = cornerRadius, bottomStart = cornerRadius), | ||
colors = TextFieldDefaults.textFieldColors( | ||
textColor = textColor, | ||
backgroundColor = textFieldBackgroundColor, | ||
focusedIndicatorColor = Color.Transparent, | ||
unfocusedIndicatorColor = Color.Transparent, | ||
), | ||
placeholder = { | ||
Text( | ||
text = hint, | ||
color = hintTextColor | ||
) | ||
} | ||
) | ||
Button( | ||
onClick = { | ||
viewModel.onBtnClick(textData) | ||
}, | ||
modifier = Modifier | ||
.height(panelHeight) | ||
.testTag(TAG_TX_BTN), | ||
shape = RoundedCornerShape(bottomEnd = cornerRadius, topEnd = cornerRadius), | ||
colors = ButtonDefaults.buttonColors(backgroundColor = buttonBackgroundColor), | ||
elevation = null, | ||
) { | ||
Icon( | ||
imageVector = if (btnStatus.value) { | ||
Icons.Default.Close | ||
} else { | ||
Icons.Default.Send | ||
}, | ||
contentDescription = if (btnStatus.value) { | ||
TAG_TX_ICON_STOP | ||
} else { | ||
TAG_TX_ICON_START | ||
}, | ||
tint = iconTintColor, | ||
) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
euphony/src/main/java/co/euphony/ui/viewmodel/TxPanelViewModel.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,51 @@ | ||
package co.euphony.ui.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import co.euphony.tx.EuTxManager | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
class TxPanelViewModel( | ||
private val txManager: EuTxManager | ||
) : ViewModel() { | ||
|
||
private val _isProcessing = MutableStateFlow(false) | ||
val isProcessing: StateFlow<Boolean> = _isProcessing | ||
|
||
fun onBtnClick(data: String) { | ||
if (_isProcessing.value) { | ||
stop() | ||
} else { | ||
start(data) | ||
} | ||
} | ||
|
||
fun stop() { | ||
txManager.stop() | ||
if (_isProcessing.value) { | ||
_isProcessing.value = false | ||
} | ||
} | ||
|
||
private fun start(data: String) { | ||
txManager.code = data | ||
txManager.play(-1) | ||
_isProcessing.value = true | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
txManager.stop() | ||
_isProcessing.value = false | ||
} | ||
} | ||
|
||
class TxPanelViewModelFactory( | ||
private val euTxManager: EuTxManager | ||
) : ViewModelProvider.NewInstanceFactory() { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T = | ||
TxPanelViewModel(euTxManager) as T | ||
} |
46 changes: 46 additions & 0 deletions
46
euphony/src/test/java/co/euphony/ui/viewmodel/TxPanelViewModelTest.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,46 @@ | ||
package co.euphony.ui.viewmodel | ||
|
||
import co.euphony.tx.EuTxManager | ||
import org.junit.After | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class TxPanelViewModelTest { | ||
|
||
private lateinit var viewModel: TxPanelViewModel | ||
|
||
@Mock | ||
private lateinit var txManager: EuTxManager | ||
|
||
@Before | ||
fun setUp() { | ||
viewModel = TxPanelViewModel(txManager) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
viewModel.stop() | ||
} | ||
|
||
@Test | ||
fun `if onBtnClick starts, isProcessing becomes true`() { | ||
assertFalse(viewModel.isProcessing.value) | ||
viewModel.onBtnClick("") | ||
assertTrue(viewModel.isProcessing.value) | ||
} | ||
|
||
@Test | ||
fun `if stop() is called, isProcessing becomes false`() { | ||
viewModel.onBtnClick("") | ||
assertTrue(viewModel.isProcessing.value) | ||
|
||
viewModel.stop() | ||
assertFalse(viewModel.isProcessing.value) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WoW! So nice options 👍 It will help to customize
EuphonyTxPanel
!