Skip to content

Commit

Permalink
Tidying code and change design
Browse files Browse the repository at this point in the history
  • Loading branch information
Z-Siqi committed Sep 1, 2023
1 parent afd2fa6 commit b65a278
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 61 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.sqz.writingboard"
minSdk 31
targetSdk 34
versionCode 4
versionName "0.0.3"
versionCode 5
versionName "0.0.5"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/sqz/writingboard/ClassActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel

class TextViewModel : ViewModel() {
class TextBoard : ViewModel() {
var textState by mutableStateOf(TextFieldValue())
}

class DoneButton : ViewModel() {
var doneButton by mutableStateOf(false)
}
85 changes: 44 additions & 41 deletions app/src/main/java/com/sqz/writingboard/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -70,17 +66,9 @@ class MainActivity : ComponentActivity() {
@Composable
fun WritingBoardLayout(modifier: Modifier = Modifier) {

val viewModel: TextViewModel = viewModel()
val context = LocalContext.current
val sharedPreferences = context.getSharedPreferences("WritingBoard", Context.MODE_PRIVATE)
LaunchedEffect(true) {
val savedText = sharedPreferences.getString("saved_text", "")
viewModel.textState = TextFieldValue(savedText ?: "")
}

val doneButtonState: DoneButton = viewModel()
val keyboardDone = LocalSoftwareKeyboardController.current
val focusManager = LocalFocusManager.current
var doneButton by remember { mutableStateOf(false) }

Surface(
modifier = modifier
Expand All @@ -90,54 +78,33 @@ fun WritingBoardLayout(modifier: Modifier = Modifier) {
detectTapGestures { _ ->
keyboardDone?.hide()
focusManager.clearFocus()
doneButton = false
doneButtonState.doneButton = false
}
},
color = MaterialTheme.colorScheme.secondaryContainer
color = MaterialTheme.colorScheme.surfaceVariant
) {
Column(
modifier = modifier
.padding(20.dp)
.shadow(5.dp, RoundedCornerShape(26.dp))
.border(
5.dp,
4.dp,
color = MaterialTheme.colorScheme.primary,
RoundedCornerShape(26.dp)
),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Surface(
color = MaterialTheme.colorScheme.onSecondary,
color = MaterialTheme.colorScheme.onPrimary,
modifier = modifier
.fillMaxSize(),
shape = RoundedCornerShape(26.dp)
) {
BasicTextField(
value = viewModel.textState,
onValueChange = { newValue ->
viewModel.textState = newValue
Handler(Looper.getMainLooper()).postDelayed(1200) {
sharedPreferences.edit()
.putString(
"saved_text",
viewModel.textState.text
.trimEnd { it.isWhitespace() })
.apply()
}
doneButton = true
},
singleLine = false,
modifier = modifier.padding(16.dp),
textStyle = TextStyle.Default.copy(
fontSize = 23.sp,
fontWeight = FontWeight.SemiBold,
color = MaterialTheme.colorScheme.secondary
),
)
WritingBoardText()
}
}
if (doneButton) {
if (doneButtonState.doneButton) {
Column(
modifier = modifier
.fillMaxSize()
Expand All @@ -148,7 +115,7 @@ fun WritingBoardLayout(modifier: Modifier = Modifier) {
FloatingActionButton(onClick = {
keyboardDone?.hide()
focusManager.clearFocus()
doneButton = false
doneButtonState.doneButton = false
}) {
Image(
painter = painterResource(R.drawable.baseline_done_24),
Expand All @@ -160,6 +127,42 @@ fun WritingBoardLayout(modifier: Modifier = Modifier) {
}
}

@Composable
fun WritingBoardText(modifier: Modifier = Modifier) {

val doneButtonState: DoneButton = viewModel()
val viewModel: TextBoard = viewModel()
val context = LocalContext.current
val sharedPreferences = context.getSharedPreferences("WritingBoard", Context.MODE_PRIVATE)
LaunchedEffect(true) {
val savedText = sharedPreferences.getString("saved_text", "")
viewModel.textState = TextFieldValue(savedText ?: "")
}

BasicTextField(
value = viewModel.textState,
onValueChange = { newValue ->
viewModel.textState = newValue
Handler(Looper.getMainLooper()).postDelayed(1200) {
sharedPreferences.edit()
.putString(
"saved_text",
viewModel.textState.text
.trimEnd { it.isWhitespace() })
.apply()
}
doneButtonState.doneButton = true
},
singleLine = false,
modifier = modifier.padding(16.dp),
textStyle = TextStyle.Default.copy(
fontSize = 23.sp,
fontWeight = FontWeight.SemiBold,
color = MaterialTheme.colorScheme.secondary
)
)
}

@Preview(showBackground = true)
@Composable
fun WritingBoardPreview() {
Expand Down

0 comments on commit b65a278

Please sign in to comment.