@@ -6,14 +6,15 @@ import androidx.compose.foundation.layout.WindowInsets
6
6
import androidx.compose.foundation.layout.fillMaxSize
7
7
import androidx.compose.foundation.layout.fillMaxWidth
8
8
import androidx.compose.foundation.layout.padding
9
- import androidx.compose.foundation.layout.safeContent
10
9
import androidx.compose.foundation.layout.safeDrawing
11
10
import androidx.compose.foundation.layout.width
12
11
import androidx.compose.foundation.shape.RoundedCornerShape
13
12
import androidx.compose.foundation.text.KeyboardActions
14
13
import androidx.compose.material.icons.Icons
15
14
import androidx.compose.material.icons.automirrored.filled.ArrowBack
16
15
import androidx.compose.material.icons.filled.Done
16
+ import androidx.compose.material.icons.filled.Lock
17
+ import androidx.compose.material.icons.filled.LockOpen
17
18
import androidx.compose.material.icons.filled.Save
18
19
import androidx.compose.material3.Button
19
20
import androidx.compose.material3.ButtonDefaults
@@ -44,6 +45,9 @@ import androidx.compose.ui.graphics.Color
44
45
import androidx.compose.ui.res.stringResource
45
46
import androidx.compose.ui.text.style.TextOverflow
46
47
import androidx.compose.ui.unit.dp
48
+ import androidx.lifecycle.viewModelScope
49
+ import com.halilibo.richtext.commonmark.Markdown
50
+ import com.halilibo.richtext.ui.material3.RichText
47
51
import io.github.wiiznokes.gitnote.R
48
52
import io.github.wiiznokes.gitnote.ui.component.CustomDropDown
49
53
import io.github.wiiznokes.gitnote.ui.component.CustomDropDownModel
@@ -52,6 +56,7 @@ import io.github.wiiznokes.gitnote.ui.destination.EditParams
52
56
import io.github.wiiznokes.gitnote.ui.model.EditType
53
57
import io.github.wiiznokes.gitnote.ui.model.FileExtension
54
58
import io.github.wiiznokes.gitnote.ui.viewmodel.newEditViewModel
59
+ import kotlinx.coroutines.launch
55
60
56
61
57
62
private const val TAG = " EditScreen"
@@ -83,11 +88,11 @@ fun EditScreen(
83
88
}
84
89
}
85
90
91
+ val isReadOnlyModeActive = vm.prefs.isReadOnlyModeActive.getAsState().value
92
+
86
93
Scaffold (
87
94
contentWindowInsets = WindowInsets .safeDrawing,
88
- contentColor = MaterialTheme .colorScheme.background,
89
95
topBar = {
90
-
91
96
val backgroundColor = MaterialTheme .colorScheme.surfaceColorAtElevation(15 .dp)
92
97
93
98
TopAppBar (
@@ -97,7 +102,7 @@ fun EditScreen(
97
102
navigationIcon = {
98
103
IconButton (
99
104
onClick = {
100
- vm.onFinish()
105
+ vm.shouldSaveWhenQuitting = false
101
106
onFinished()
102
107
},
103
108
) {
@@ -117,6 +122,7 @@ fun EditScreen(
117
122
onValueChange = {
118
123
vm.name.value = it
119
124
},
125
+ readOnly = isReadOnlyModeActive,
120
126
singleLine = true ,
121
127
placeholder = {
122
128
Text (text = stringResource(R .string.note_name))
@@ -137,36 +143,57 @@ fun EditScreen(
137
143
)
138
144
},
139
145
actions = {
140
- ExtensionChooser (vm.fileExtension)
146
+ ExtensionChooser (vm.fileExtension, isReadOnlyModeActive )
141
147
Spacer (modifier = Modifier .width(10 .dp))
142
148
IconButton (
143
149
colors = IconButtonDefaults .iconButtonColors(
144
150
containerColor = MaterialTheme .colorScheme.primary,
145
151
contentColor = MaterialTheme .colorScheme.onPrimary
146
152
),
147
153
onClick = {
148
- vm.onValidation(onSuccess = null )
149
- }
154
+ vm.save()
155
+ },
156
+ enabled = ! isReadOnlyModeActive
150
157
) {
151
158
SimpleIcon (
152
159
imageVector = Icons .Default .Save ,
153
160
)
154
161
}
162
+
163
+ Spacer (modifier = Modifier .width(10 .dp))
164
+
165
+ IconButton (
166
+ colors = IconButtonDefaults .iconButtonColors(
167
+ containerColor = MaterialTheme .colorScheme.primary,
168
+ contentColor = MaterialTheme .colorScheme.onPrimary
169
+ ),
170
+ onClick = {
171
+ vm.viewModelScope.launch {
172
+ vm.prefs.isReadOnlyModeActive.update(! isReadOnlyModeActive)
173
+ }
174
+ },
175
+ ) {
176
+ SimpleIcon (
177
+ imageVector = if (isReadOnlyModeActive) {
178
+ Icons .Default .Lock
179
+ } else {
180
+ Icons .Default .LockOpen
181
+ },
182
+ )
183
+ }
155
184
}
156
185
)
157
186
},
158
187
floatingActionButton = {
159
-
160
188
// bug: https://issuetracker.google.com/issues/224005027
161
189
// AnimatedVisibility(visible = currentNoteFolderRelativePath.isNotEmpty()) {
162
- if (vm.name.value.text.isNotEmpty()) {
190
+ if (! isReadOnlyModeActive && vm.name.value.text.isNotEmpty()) {
163
191
FloatingActionButton (
164
192
modifier = Modifier ,
165
193
containerColor = MaterialTheme .colorScheme.primary,
166
194
shape = RoundedCornerShape (20 .dp),
167
195
onClick = {
168
- vm.onFinish()
169
- vm.onValidation(onSuccess = onFinished)
196
+ vm.save(onSuccess = onFinished)
170
197
}
171
198
) {
172
199
SimpleIcon (
@@ -177,48 +204,60 @@ fun EditScreen(
177
204
}
178
205
}
179
206
) { paddingValues ->
180
- TextField (
181
- modifier = Modifier
182
- .padding(paddingValues)
183
- .fillMaxSize()
184
- .focusRequester(textFocusRequester),
185
- value = vm.content.value,
186
- onValueChange = {
187
- vm.content.value = it
188
- },
189
- colors = TextFieldDefaults .colors(
190
- focusedContainerColor = MaterialTheme .colorScheme.background,
191
- unfocusedContainerColor = MaterialTheme .colorScheme.background,
192
- focusedTextColor = MaterialTheme .colorScheme.onBackground,
193
- unfocusedTextColor = MaterialTheme .colorScheme.onBackground,
194
- focusedIndicatorColor = Color .Transparent ,
195
- unfocusedIndicatorColor = Color .Transparent ,
196
- ),
197
- keyboardActions = KeyboardActions (
198
- onDone = {
199
- vm.onFinish()
200
- vm.onValidation(onSuccess = onFinished)
201
- }
202
- )
203
- )
204
207
208
+ if (isReadOnlyModeActive && vm.fileExtension.value is FileExtension .Md ) {
209
+ RichText (
210
+ modifier = Modifier
211
+ .padding(paddingValues)
212
+ .fillMaxSize()
213
+ ) {
214
+ Markdown (vm.content.value.text)
215
+ }
216
+
217
+
218
+ } else {
219
+ TextField (
220
+ modifier = Modifier
221
+ .padding(paddingValues)
222
+ .fillMaxSize()
223
+ .focusRequester(textFocusRequester),
224
+ value = vm.content.value,
225
+ onValueChange = {
226
+ vm.content.value = it
227
+ },
228
+ colors = TextFieldDefaults .colors(
229
+ focusedContainerColor = MaterialTheme .colorScheme.background,
230
+ unfocusedContainerColor = MaterialTheme .colorScheme.background,
231
+ focusedTextColor = MaterialTheme .colorScheme.onBackground,
232
+ unfocusedTextColor = MaterialTheme .colorScheme.onBackground,
233
+ focusedIndicatorColor = Color .Transparent ,
234
+ unfocusedIndicatorColor = Color .Transparent ,
235
+ ),
236
+ keyboardActions = KeyboardActions (
237
+ onDone = {
238
+ vm.save(onSuccess = onFinished)
239
+ }
240
+ ),
241
+ readOnly = isReadOnlyModeActive
242
+ )
243
+ }
205
244
}
206
245
}
207
246
208
247
209
248
@Composable
210
249
private fun ExtensionChooser (
211
- fileExtension : MutableState <FileExtension >
250
+ fileExtension : MutableState <FileExtension >,
251
+ isReadOnly : Boolean ,
212
252
) {
213
253
Box {
214
254
val expanded = remember { mutableStateOf(false ) }
215
255
Button (
216
256
colors = ButtonDefaults .buttonColors(
217
257
containerColor = MaterialTheme .colorScheme.secondary
218
258
),
219
- onClick = {
220
- expanded.value = true
221
- }
259
+ onClick = { expanded.value = true },
260
+ enabled = ! isReadOnly
222
261
) {
223
262
Text (
224
263
text = ' .' + fileExtension.value.text,
0 commit comments