@@ -122,6 +122,11 @@ class MultiEditorViewController: UIViewController {
122
122
}
123
123
}
124
124
125
+ override func viewDidAppear( _ animated: Bool ) {
126
+ super. viewDidAppear ( animated)
127
+ clipsController. select ( index: selected ?? 0 )
128
+ }
129
+
125
130
func loadEditor( for index: Int ) {
126
131
let frame = frames [ index]
127
132
if let editor = delegate? . editor ( segment: frame. segment, edit: frame. edit) {
@@ -208,8 +213,11 @@ extension MultiEditorViewController: MediaClipsEditorDelegate {
208
213
frames. remove ( at: index)
209
214
}
210
215
211
- migratedIndex = shift ( index: selected ?? 0 , indices: [ index] , edits: frames)
212
- selected = newIndex ( indices: [ index] , selected: selected, edits: frames)
216
+ let newSelection = newIndex ( indices: [ index] , selected: selected, edits: frames)
217
+ if newSelection == selected {
218
+ selected = nil
219
+ }
220
+ selected = newSelection
213
221
if selected == nil {
214
222
dismissButtonPressed ( )
215
223
}
@@ -224,12 +232,12 @@ extension MultiEditorViewController: MediaClipsEditorDelegate {
224
232
225
233
let sortedindices = indices. sorted ( )
226
234
227
- if let selected = selected, sortedindices. contains ( selected) {
228
- if let index = indices. first, edits. indices. contains ( index) {
235
+ if let selected = selected, sortedindices. contains ( selected) { // If the selection is contained in the set
236
+ if let index = indices. first, edits. indices. contains ( index) { // Keep the same selection if it still exists.
229
237
return index
230
- } else if let firstIndex = indices. first, firstIndex > edits. startIndex {
238
+ } else if let firstIndex = indices. first, firstIndex > edits. startIndex { // Item before if it does not.
231
239
nextIndex = edits. index ( before: firstIndex)
232
- } else if let lastIndex = sortedindices. last, lastIndex < edits. endIndex {
240
+ } else if let lastIndex = sortedindices. last, lastIndex < edits. endIndex { // Item after if prior item doesn't exist.
233
241
nextIndex = edits. index ( after: lastIndex)
234
242
}
235
243
} else {
@@ -248,23 +256,40 @@ extension MultiEditorViewController: MediaClipsEditorDelegate {
248
256
}
249
257
}
250
258
259
+ func shift( index: Int , moves: [ ( origin: Int , destination: Int ) ] , edits: [ Any ] ) -> Int {
260
+ let indexMoves : [ Int ] = moves. map { origin, destination -> Int in
261
+ if ( index < origin && index < destination) || ( index > origin && index > destination) {
262
+ return 0
263
+ } else {
264
+ if destination >= index && origin < index {
265
+ return - 1
266
+ } else if destination <= index && origin > index {
267
+ return 1
268
+ } else {
269
+ return 0
270
+ }
271
+ }
272
+ }
273
+ return index + indexMoves. reduce ( 0 , { $0 + $1 } )
274
+ }
275
+
251
276
func mediaClipWasMoved( from originIndex: Int , to destinationIndex: Int ) {
252
277
if let selected = selected {
253
278
archive ( index: selected)
254
279
}
255
280
frames. move ( from: originIndex, to: destinationIndex)
256
281
257
- let newIndex : Int
282
+ let selectedIndex : Int
258
283
if selected == originIndex {
259
284
// When moving the selected frame just move it to the destination index
260
- newIndex = destinationIndex
285
+ selectedIndex = destinationIndex
261
286
} else {
262
287
// Otherwise calculate the shifted index value
263
- newIndex = shift ( index: selected ?? 0 , indices : [ originIndex] , edits: frames)
288
+ selectedIndex = shift ( index: selected ?? 0 , moves : [ ( originIndex, destinationIndex ) ] , edits: frames)
264
289
}
265
290
266
- migratedIndex = newIndex
267
- selected = newIndex
291
+ migratedIndex = selectedIndex
292
+ selected = selectedIndex
268
293
}
269
294
270
295
func mediaClipWasSelected( at: Int ) {
0 commit comments