Skip to content

Commit 21b7457

Browse files
committed
snapshot
1 parent ec6cb2e commit 21b7457

File tree

4 files changed

+143
-122
lines changed

4 files changed

+143
-122
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
android:theme="@style/Theme.SubSampler">
1919
<activity
2020
android:name=".MainActivity"
21-
android:exported="true">
21+
android:exported="true"
22+
android:windowSoftInputMode="adjustResize">
2223
<intent-filter>
2324
<action android:name="android.intent.action.MAIN" />
2425
<category android:name="android.intent.category.LAUNCHER" />

app/src/main/java/com/karthek/android/s/subsampler/MainActivity.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ class MainActivity : ComponentActivity() {
7777
fun ScreenContent() {
7878
SubsamplerTheme {
7979
Surface(modifier = Modifier.fillMaxSize()) {
80-
MainScreen(viewModel, selectImageClick = {
81-
mGetContent.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
82-
}) {
83-
mSaveContent.launch(getSaveFileName(viewModel.fileName, viewModel.reqSize))
84-
}
80+
MainScreen(
81+
viewModel = viewModel,
82+
selectImageClick = {
83+
mGetContent.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
84+
},
85+
saveClick = {
86+
mSaveContent.launch(getSaveFileName(viewModel.fileName, viewModel.reqSize))
87+
}
88+
)
8589
}
8690
}
8791
}

app/src/main/java/com/karthek/android/s/subsampler/state/SubsampleScreenViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import java.io.FileInputStream
1717

1818
class SubsampleScreenViewModel(appContext: Application) : AndroidViewModel(appContext) {
1919
var imageUri by mutableStateOf<Uri?>(null)
20+
var textFieldValue by mutableStateOf("")
2021
var samplingInProgress by mutableStateOf(false)
2122
var showSaved by mutableStateOf(false)
2223
var inputStream: FileInputStream? = null

app/src/main/java/com/karthek/android/s/subsampler/ui/screens/MainScreen.kt

Lines changed: 131 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -33,150 +33,165 @@ import com.karthek.android.s.subsampler.R
3333
import com.karthek.android.s.subsampler.SettingsActivity
3434
import com.karthek.android.s.subsampler.state.SubsampleScreenViewModel
3535

36-
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
36+
@OptIn(ExperimentalMaterial3Api::class)
3737
@Composable
3838
fun MainScreen(
3939
viewModel: SubsampleScreenViewModel,
4040
selectImageClick: () -> Unit,
41-
saveClick: () -> Unit
41+
saveClick: () -> Unit,
4242
) {
4343
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
4444
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)
5159
)
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
6562
)
6663
}, modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection)) { innerPadding ->
6764
Column(
6865
modifier = Modifier
6966
.padding(innerPadding)
7067
.padding(8.dp)
68+
.imePadding()
7169
.verticalScroll(rememberScrollState())
7270
) {
73-
Card(
74-
onClick = selectImageClick, modifier = Modifier
71+
ImagePreview(
72+
viewModel = viewModel,
73+
selectImageClick = selectImageClick,
74+
modifier = Modifier
7575
.fillMaxWidth()
7676
.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+
}
8182

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) {
8695

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),
11999
)
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))
127107
},
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(
131113
modifier = Modifier
132114
.fillMaxWidth()
133-
.padding(16.dp)
115+
.align(Alignment.BottomCenter)
134116
)
117+
}
118+
}
119+
}
120+
}
135121

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+
)
139151

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) }
170154

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))
180195
}
181196
}
182197
}

0 commit comments

Comments
 (0)