1
0
mirror of https://github.com/go-vikunja/app synced 2024-06-01 02:06:51 +00:00

moved bucket position and task kanbanPosition calculation to helper function

This commit is contained in:
Paul Nettleton 2022-08-05 02:15:49 -05:00
parent b697944a6e
commit 71a836b806
3 changed files with 60 additions and 34 deletions

View File

@ -20,6 +20,7 @@ import 'package:vikunja_app/models/bucket.dart';
import 'package:vikunja_app/pages/list/list_edit.dart';
import 'package:vikunja_app/pages/list/task_edit.dart';
import 'package:vikunja_app/stores/list_store.dart';
import 'package:vikunja_app/utils/calculate_item_position.dart';
enum BucketMenu {limit, done, delete}
@ -260,25 +261,38 @@ class _ListPageState extends State<ListPage> {
newIndex -= 1;
indexUpdated = true;
}
taskState.buckets.insert(newIndex, taskState.buckets.removeAt(_draggedBucketIndex));
if (newIndex == 0) {
taskState.buckets[0].position = 0;
await _updateBucket(context, taskState.buckets[0]);
newIndex = 1;
final movedBucket = taskState.buckets.removeAt(_draggedBucketIndex);
if (newIndex >= taskState.buckets.length) {
taskState.buckets.add(movedBucket);
} else {
taskState.buckets.insert(newIndex, movedBucket);
}
if (taskState.buckets[newIndex].position <= taskState.buckets[newIndex - 1].position
|| taskState.buckets[newIndex].position >= taskState.buckets[newIndex + 1].position) {
taskState.buckets[newIndex].position = newIndex == taskState.buckets.length - 1
? taskState.buckets[newIndex - 1].position + pow(2.0, 16.0)
: (taskState.buckets[newIndex - 1].position
+ taskState.buckets[newIndex + 1].position) / 2.0;
_updateBucket(context, taskState.buckets[newIndex]);
taskState.buckets[newIndex].position = calculateItemPosition(
positionBefore: newIndex != 0
? taskState.buckets[newIndex - 1].position : null,
positionAfter: newIndex < taskState.buckets.length - 1
? taskState.buckets[newIndex + 1].position : null,
);
await _updateBucket(context, taskState.buckets[newIndex]);
// make sure the first 2 buckets don't have 0 position
if (newIndex == 0 && taskState.buckets.length > 1 && taskState.buckets[1].position == 0) {
taskState.buckets[1].position = calculateItemPosition(
positionBefore: taskState.buckets[0].position,
positionAfter: 1 < taskState.buckets.length - 1
? taskState.buckets[2].position : null,
);
_updateBucket(context, taskState.buckets[1]);
}
if (indexUpdated && portrait) _pageController.animateToPage(
newIndex - 1,
duration: Duration(milliseconds: 100),
curve: Curves.easeInOut,
);
setState(() => _draggedBucketIndex = null);
},
);

View File

@ -1,8 +1,7 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:vikunja_app/models/task.dart';
import 'package:vikunja_app/models/bucket.dart';
import 'package:vikunja_app/utils/calculate_item_position.dart';
import 'package:vikunja_app/global.dart';
class ListProvider with ChangeNotifier {
@ -183,36 +182,28 @@ class ListProvider with ChangeNotifier {
else
_buckets[newBucketIndex].tasks.insert(index, task);
double kanbanPosition;
if (_buckets[newBucketIndex].tasks.length == 1) // only task
kanbanPosition = 0.0;
else if (index == 0) // first task
kanbanPosition = _buckets[newBucketIndex].tasks[1].kanbanPosition / 2.0;
else if (index == _buckets[newBucketIndex].tasks.length - 1) // last task
kanbanPosition = _buckets[newBucketIndex].tasks[index - 1].kanbanPosition + pow(2.0, 16.0);
else // in the middle
kanbanPosition = (_buckets[newBucketIndex].tasks[index - 1].kanbanPosition
+ _buckets[newBucketIndex].tasks[index + 1].kanbanPosition) / 2.0;
task = await VikunjaGlobal.of(context).taskService.update(task.copyWith(
bucketId: newBucketId ?? task.bucketId,
kanbanPosition: kanbanPosition,
kanbanPosition: calculateItemPosition(
positionBefore: index != 0
? _buckets[newBucketIndex].tasks[index - 1].kanbanPosition : null,
positionAfter: index < _buckets[newBucketIndex].tasks.length - 1
? _buckets[newBucketIndex].tasks[index + 1].kanbanPosition : null,
),
));
_buckets[newBucketIndex].tasks[index] = task;
// make sure first 2 tasks don't have 0 kanbanPosition
// make sure the first 2 tasks don't have 0 kanbanPosition
Task secondTask;
if (index == 0 && _buckets[newBucketIndex].tasks.length > 1
&& _buckets[newBucketIndex].tasks[1].kanbanPosition == 0) {
if (_buckets[newBucketIndex].tasks.length == 2) // last task
kanbanPosition = _buckets[newBucketIndex].tasks[0].kanbanPosition + pow(2.0, 16.0);
else // in the middle
kanbanPosition = (_buckets[newBucketIndex].tasks[0].kanbanPosition
+ _buckets[newBucketIndex].tasks[2].kanbanPosition) / 2.0;
secondTask = await VikunjaGlobal.of(context).taskService.update(
_buckets[newBucketIndex].tasks[1].copyWith(
kanbanPosition: kanbanPosition,
kanbanPosition: calculateItemPosition(
positionBefore: task.kanbanPosition,
positionAfter: 1 < _buckets[newBucketIndex].tasks.length - 1
? _buckets[newBucketIndex].tasks[2].kanbanPosition : null,
),
));
_buckets[newBucketIndex].tasks[1] = secondTask;
}

View File

@ -0,0 +1,21 @@
import 'dart:math';
double calculateItemPosition({double positionBefore, double positionAfter}) {
// only
if (positionBefore == null && positionAfter == null) {
return 0;
}
// first
if (positionBefore == null && positionAfter != null) {
return positionAfter / 2;
}
// last
if (positionBefore != null && positionAfter == null) {
return positionBefore + pow(2.0, 16.0);
}
// in the middle (positionBefore != null && positionAfter != null)
return (positionBefore + positionAfter) / 2;
}