Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
devrath committed Jan 4, 2024
1 parent 096df27 commit 3cf30a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ import android.annotation.SuppressLint
import androidx.compose.animation.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import com.istudio.app.service.ServiceHelper
import com.istudio.app.service.StopwatchService
import com.istudio.app.ui.composables.AppButton
import com.istudio.app.ui.composables.AppText
import com.istudio.app.util.Constants.ACTION_SERVICE_CANCEL
import com.istudio.app.util.Constants.ACTION_SERVICE_START
Expand Down Expand Up @@ -62,42 +56,39 @@ fun MainScreen(stopwatchService: StopwatchService) {
AppText(text = seconds)
}
Row(modifier = Modifier.weight(weight = 1f)) {
Button(
AppButton(
modifier = Modifier
.weight(1f)
.fillMaxHeight(0.8f),
text = leftButtonTextAction(currentState),
onClick = {
ServiceHelper.triggerForegroundService(
context = context,
action = if (currentState == StopwatchState.Started) ACTION_SERVICE_STOP
else ACTION_SERVICE_START
)
}, colors = ButtonDefaults.buttonColors(
containerColor = if (currentState == StopwatchState.Started) Color.Red else Color.Blue,
contentColor = Color.White
)
) {
Text(
text = if (currentState == StopwatchState.Started) "Stop"
else if ((currentState == StopwatchState.Stopped)) "Resume"
else "Start"
)
}
})

Spacer(modifier = Modifier.width(30.dp))
Button(

AppButton(
modifier = Modifier
.weight(1f)
.fillMaxHeight(0.8f),
enabled = seconds != "00" && currentState != StopwatchState.Started,
text = "Cancel",
onClick = {
ServiceHelper.triggerForegroundService(
context = context, action = ACTION_SERVICE_CANCEL
)
},
enabled = seconds != "00" && currentState != StopwatchState.Started,
colors = ButtonDefaults.buttonColors(containerColor = Color.Gray)
) {
Text(text = "Cancel")
}
)
}
}
}
}

@Composable
private fun leftButtonTextAction(currentState: StopwatchState) =
if (currentState == StopwatchState.Started) "Stop"
else if ((currentState == StopwatchState.Stopped)) "Resume"
else "Start"
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package com.istudio.app.ui.composables

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


@Composable
Expand All @@ -18,4 +32,24 @@ fun AppText(text : String){
color = Color.White
)
)
}



@Composable
fun AppButton(
modifier: Modifier = Modifier,
text: String,
enabled : Boolean = true,
onClick: () -> Unit
) {

TextButton(
modifier = modifier,
onClick =onClick,
enabled = enabled
) {
Text(text = text)
}

}

0 comments on commit 3cf30a2

Please sign in to comment.