@@ -33,150 +33,165 @@ import com.karthek.android.s.subsampler.R
33
33
import com.karthek.android.s.subsampler.SettingsActivity
34
34
import com.karthek.android.s.subsampler.state.SubsampleScreenViewModel
35
35
36
- @OptIn(ExperimentalMaterial3Api ::class , ExperimentalComposeUiApi :: class )
36
+ @OptIn(ExperimentalMaterial3Api ::class )
37
37
@Composable
38
38
fun MainScreen (
39
39
viewModel : SubsampleScreenViewModel ,
40
40
selectImageClick : () -> Unit ,
41
- saveClick : () -> Unit
41
+ saveClick : () -> Unit ,
42
42
) {
43
43
val scrollBehavior = TopAppBarDefaults .pinnedScrollBehavior()
44
44
Scaffold (topBar = {
45
- TopAppBar (
46
- title = {
47
- Text (
48
- text = stringResource(id = R .string.app_name),
49
- maxLines = 1 ,
50
- overflow = TextOverflow .Ellipsis
45
+ TopAppBar (title = {
46
+ Text (
47
+ text = stringResource(id = R .string.app_name),
48
+ maxLines = 1 ,
49
+ overflow = TextOverflow .Ellipsis
50
+ )
51
+ }, actions = {
52
+ val context = LocalContext .current
53
+ IconButton (onClick = {
54
+ context.startActivity(Intent (context, SettingsActivity ::class .java))
55
+ }) {
56
+ Icon (
57
+ imageVector = Icons .Outlined .MoreVert ,
58
+ contentDescription = stringResource(id = R .string.more)
51
59
)
52
- },
53
- actions = {
54
- val context = LocalContext .current
55
- IconButton (onClick = {
56
- context.startActivity(Intent (context, SettingsActivity ::class .java))
57
- }) {
58
- Icon (
59
- imageVector = Icons .Outlined .MoreVert ,
60
- contentDescription = stringResource(id = R .string.more)
61
- )
62
- }
63
- },
64
- scrollBehavior = scrollBehavior
60
+ }
61
+ }, scrollBehavior = scrollBehavior
65
62
)
66
63
}, modifier = Modifier .nestedScroll(scrollBehavior.nestedScrollConnection)) { innerPadding ->
67
64
Column (
68
65
modifier = Modifier
69
66
.padding(innerPadding)
70
67
.padding(8 .dp)
68
+ .imePadding()
71
69
.verticalScroll(rememberScrollState())
72
70
) {
73
- Card (
74
- onClick = selectImageClick, modifier = Modifier
71
+ ImagePreview (
72
+ viewModel = viewModel,
73
+ selectImageClick = selectImageClick,
74
+ modifier = Modifier
75
75
.fillMaxWidth()
76
76
.height(240 .dp)
77
- .padding(8 .dp)
78
- ) {
79
- Box (contentAlignment = Alignment .Center , modifier = Modifier .fillMaxSize()) {
80
- if (viewModel.imageUri == null ) {
77
+ )
78
+ ActionContent (viewModel, saveClick)
79
+ }
80
+ }
81
+ }
81
82
82
- Icon (
83
- imageVector = Icons .Outlined .AddAPhoto ,
84
- contentDescription = stringResource(id = R .string.select_photo),
85
- )
83
+ @OptIn(ExperimentalMaterial3Api ::class )
84
+ @Composable
85
+ fun ImagePreview (
86
+ viewModel : SubsampleScreenViewModel ,
87
+ selectImageClick : () -> Unit ,
88
+ modifier : Modifier ,
89
+ ) {
90
+ Card (
91
+ onClick = selectImageClick, modifier = modifier.padding(8 .dp)
92
+ ) {
93
+ Box (contentAlignment = Alignment .Center , modifier = Modifier .fillMaxSize()) {
94
+ if (viewModel.imageUri == null ) {
86
95
87
- } else {
88
- SubcomposeAsyncImage (
89
- model = viewModel.imageUri,
90
- contentDescription = stringResource(id = R .string.select_photo),
91
- loading = {
92
- CircularProgressIndicator (modifier = Modifier .requiredSize(48 .dp))
93
- },
94
- modifier = Modifier
95
- .fillMaxSize()
96
- )
97
- }
98
- if (viewModel.samplingInProgress) {
99
- LinearProgressIndicator (
100
- modifier = Modifier
101
- .fillMaxWidth()
102
- .align(Alignment .BottomCenter )
103
- )
104
- }
105
- }
106
- }
107
- if (viewModel.imageUri != null ) {
108
- Text (
109
- text = " ${stringResource(id = R .string.orig_size)} : ${
110
- Formatter .formatFileSize(
111
- LocalContext .current,
112
- viewModel.origSize
113
- )
114
- } " ,
115
- textAlign = TextAlign .Center ,
116
- modifier = Modifier
117
- .fillMaxWidth()
118
- .padding(16 .dp)
96
+ Icon (
97
+ imageVector = Icons .Outlined .AddAPhoto ,
98
+ contentDescription = stringResource(id = R .string.select_photo),
119
99
)
120
- Text (
121
- text = if (viewModel.showSaved) {
122
- " ${stringResource(id = R .string.new_size)} : ${
123
- viewModel.gotSize / 1024
124
- } KB "
125
- } else {
126
- stringResource(id = R .string.message1 )
100
+
101
+ } else {
102
+ SubcomposeAsyncImage (
103
+ model = viewModel.imageUri,
104
+ contentDescription = stringResource(id = R .string.select_photo),
105
+ loading = {
106
+ CircularProgressIndicator (modifier = Modifier .requiredSize( 48 .dp) )
127
107
},
128
- color = if (viewModel.showSaved) Color .Unspecified
129
- else MaterialTheme .colorScheme.primary,
130
- textAlign = TextAlign .Center ,
108
+ modifier = Modifier .fillMaxSize()
109
+ )
110
+ }
111
+ if (viewModel.samplingInProgress) {
112
+ LinearProgressIndicator (
131
113
modifier = Modifier
132
114
.fillMaxWidth()
133
- .padding( 16 .dp )
115
+ .align( Alignment . BottomCenter )
134
116
)
117
+ }
118
+ }
119
+ }
120
+ }
135
121
136
- val keyboardController = LocalSoftwareKeyboardController .current
137
- var textFieldValue by rememberSaveable { mutableStateOf(" " ) }
138
- var isError by rememberSaveable { mutableStateOf(false ) }
122
+ @OptIn(ExperimentalMaterial3Api ::class , ExperimentalComposeUiApi ::class )
123
+ @Composable
124
+ fun ActionContent (viewModel : SubsampleScreenViewModel , saveClick : () -> Unit ) {
125
+ if (viewModel.imageUri != null ) {
126
+ Column {
127
+ Text (
128
+ text = " ${stringResource(id = R .string.orig_size)} : ${
129
+ Formatter .formatFileSize(
130
+ LocalContext .current, viewModel.origSize
131
+ )
132
+ } " , textAlign = TextAlign .Center , modifier = Modifier
133
+ .fillMaxWidth()
134
+ .padding(16 .dp)
135
+ )
136
+ Text (
137
+ text = if (viewModel.showSaved) {
138
+ " ${stringResource(id = R .string.new_size)} : ${
139
+ viewModel.gotSize / 1024
140
+ } KB"
141
+ } else {
142
+ stringResource(id = R .string.message1)
143
+ },
144
+ color = if (viewModel.showSaved) Color .Unspecified
145
+ else MaterialTheme .colorScheme.primary,
146
+ textAlign = TextAlign .Center ,
147
+ modifier = Modifier
148
+ .fillMaxWidth()
149
+ .padding(16 .dp)
150
+ )
139
151
140
- OutlinedTextField (
141
- value = textFieldValue,
142
- onValueChange = { e_s ->
143
- textFieldValue = e_s
144
- val parsedValue = textFieldValue.toLongOrNull()
145
- isError = ((parsedValue == null ) || (parsedValue <= 0L ))
146
- if (! isError && parsedValue != null ) {
147
- viewModel.reqSize = parsedValue
148
- }
149
- }, isError = isError,
150
- label = { Text (text = stringResource(id = R .string.req_size)) },
151
- singleLine = true ,
152
- keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
153
- keyboardActions = KeyboardActions (onDone = { keyboardController?.hide() }),
154
- modifier = Modifier
155
- .fillMaxWidth()
156
- .padding(16 .dp)
157
- .align(Alignment .CenterHorizontally )
158
- )
159
- Button (
160
- enabled = (! (isError)
161
- && (textFieldValue.isNotEmpty())
162
- && (! viewModel.samplingInProgress)),
163
- onClick = { viewModel.runForSize() },
164
- modifier = Modifier
165
- .padding(16 .dp)
166
- .align(Alignment .CenterHorizontally )
167
- ) {
168
- Text (text = stringResource(id = R .string.run))
169
- }
152
+ val keyboardController = LocalSoftwareKeyboardController .current
153
+ var isError by rememberSaveable { mutableStateOf(false ) }
170
154
171
- Button (
172
- enabled = viewModel.showSaved,
173
- onClick = saveClick,
174
- modifier = Modifier
175
- .fillMaxWidth()
176
- .padding(16 .dp)
177
- ) {
178
- Text (text = stringResource(id = R .string.save))
179
- }
155
+ OutlinedTextField (
156
+ value = viewModel.textFieldValue,
157
+ onValueChange = { e_s ->
158
+ viewModel.textFieldValue = e_s
159
+ val parsedValue = e_s.toLongOrNull()
160
+ isError = ((parsedValue == null ) || (parsedValue <= 0L ))
161
+ if (! isError && parsedValue != null ) {
162
+ viewModel.reqSize = parsedValue
163
+ }
164
+ },
165
+ isError = isError,
166
+ label = { Text (text = stringResource(id = R .string.req_size)) },
167
+ singleLine = true ,
168
+ keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number ),
169
+ keyboardActions = KeyboardActions (onDone = { keyboardController?.hide() }),
170
+ modifier = Modifier
171
+ .fillMaxWidth()
172
+ .padding(16 .dp)
173
+ .align(Alignment .CenterHorizontally )
174
+ )
175
+ Button (
176
+ enabled = (! (isError)
177
+ && (viewModel.textFieldValue.isNotEmpty())
178
+ && (! viewModel.samplingInProgress)),
179
+ onClick = { viewModel.runForSize() },
180
+ modifier = Modifier
181
+ .padding(16 .dp)
182
+ .align(Alignment .CenterHorizontally )
183
+ ) {
184
+ Text (text = stringResource(id = R .string.run))
185
+ }
186
+
187
+ Button (
188
+ enabled = viewModel.showSaved,
189
+ onClick = saveClick,
190
+ modifier = Modifier
191
+ .fillMaxWidth()
192
+ .padding(16 .dp)
193
+ ) {
194
+ Text (text = stringResource(id = R .string.save))
180
195
}
181
196
}
182
197
}
0 commit comments