|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:saber/data/file_manager/file_manager.dart'; |
| 5 | +import 'package:saber/data/prefs.dart'; |
| 6 | +import 'package:saber/i18n/strings.g.dart'; |
| 7 | +import 'package:saber/pages/editor/editor.dart'; |
| 8 | +import 'package:stow_plain/stow_plain.dart'; |
| 9 | + |
| 10 | +class SortNotes { |
| 11 | + SortNotes._(); |
| 12 | + |
| 13 | + static final List<void Function(List<String>, bool)> _sortFunctions = [ |
| 14 | + _sortNotesAlpha, |
| 15 | + _sortNotesLastModified, |
| 16 | + _sortNotesSize, |
| 17 | + ]; |
| 18 | + static final PlainStow<int> _sortFunctionIdx = stows.sortFunctionIdx; |
| 19 | + static final PlainStow<bool> _isIncreasingOrder = stows.isSortIncreasing; |
| 20 | + |
| 21 | + static bool _isNeeded = true; |
| 22 | + static bool get isNeeded => _isNeeded; |
| 23 | + |
| 24 | + static int get sortFunctionIdx => _sortFunctionIdx.value; |
| 25 | + static set sortFunctionIdx(int value) { |
| 26 | + _sortFunctionIdx.value = value; |
| 27 | + _isNeeded = true; |
| 28 | + } |
| 29 | + |
| 30 | + static bool get isIncreasingOrder => _isIncreasingOrder.value; |
| 31 | + static set isIncreasingOrder(bool value) { |
| 32 | + _isIncreasingOrder.value = value; |
| 33 | + _isNeeded = true; |
| 34 | + } |
| 35 | + |
| 36 | + static void _reverse(List<String> list) { |
| 37 | + final n = list.length; |
| 38 | + for (int i = 0; i < n / 2; i++) { |
| 39 | + final tmp = list[i]; |
| 40 | + list[i] = list[n - i - 1]; |
| 41 | + list[n - i - 1] = tmp; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + static void sortNotes(List<String> filePaths, {bool forced = false}) { |
| 46 | + if (_isNeeded || forced) { |
| 47 | + _sortFunctions[sortFunctionIdx].call(filePaths, isIncreasingOrder); |
| 48 | + _isNeeded = false; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + static void _sortNotesAlpha(List<String> filePaths, bool isIncreasing) { |
| 53 | + filePaths.sort((a, b) => a.split('/').last.compareTo(b.split('/').last)); |
| 54 | + if (!isIncreasing) _reverse(filePaths); |
| 55 | + } |
| 56 | + |
| 57 | + static DateTime _getDirLastModified(Directory dir) { |
| 58 | + assert(dir.existsSync()); |
| 59 | + DateTime out = dir.statSync().modified; |
| 60 | + for (FileSystemEntity entity |
| 61 | + in dir.listSync(recursive: true, followLinks: false)) { |
| 62 | + if (entity is File && entity.absolute.path.endsWith(Editor.extension)) { |
| 63 | + final DateTime curFileModified = entity.lastModifiedSync(); |
| 64 | + if (curFileModified.isAfter(out)) out = curFileModified; |
| 65 | + } |
| 66 | + } |
| 67 | + return out; |
| 68 | + } |
| 69 | + |
| 70 | + static void _sortNotesLastModified( |
| 71 | + List<String> filePaths, bool isIncreasing) { |
| 72 | + filePaths.sort((a, b) { |
| 73 | + final Directory firstDir = Directory(FileManager.documentsDirectory + a); |
| 74 | + final Directory secondDir = Directory(FileManager.documentsDirectory + b); |
| 75 | + final DateTime firstTime = firstDir.existsSync() |
| 76 | + ? _getDirLastModified(firstDir) |
| 77 | + : FileManager.lastModified(a + Editor.extension); |
| 78 | + final DateTime secondTime = secondDir.existsSync() |
| 79 | + ? _getDirLastModified(secondDir) |
| 80 | + : FileManager.lastModified(b + Editor.extension); |
| 81 | + return firstTime.compareTo(secondTime); |
| 82 | + }); |
| 83 | + if (!isIncreasing) _reverse(filePaths); |
| 84 | + } |
| 85 | + |
| 86 | + static int _getDirSize(Directory dir) { |
| 87 | + assert(dir.existsSync()); |
| 88 | + int out = 0; |
| 89 | + for (FileSystemEntity entity |
| 90 | + in dir.listSync(recursive: true, followLinks: false)) { |
| 91 | + if (entity is File && entity.absolute.path.endsWith(Editor.extension)) { |
| 92 | + final int curFileSize = entity.lengthSync(); |
| 93 | + out += curFileSize; |
| 94 | + } |
| 95 | + } |
| 96 | + return out; |
| 97 | + } |
| 98 | + |
| 99 | + static void _sortNotesSize(List<String> filePaths, bool isIncreasing) { |
| 100 | + filePaths.sort((a, b) { |
| 101 | + final Directory firstDir = Directory(FileManager.documentsDirectory + a); |
| 102 | + final Directory secondDir = Directory(FileManager.documentsDirectory + b); |
| 103 | + final int firstSize = firstDir.existsSync() |
| 104 | + ? _getDirSize(firstDir) |
| 105 | + : FileManager.getFile('$a${Editor.extension}').statSync().size; |
| 106 | + final int secondSize = secondDir.existsSync() |
| 107 | + ? _getDirSize(secondDir) |
| 108 | + : FileManager.getFile('$b${Editor.extension}').statSync().size; |
| 109 | + return firstSize.compareTo(secondSize); |
| 110 | + }); |
| 111 | + if (!isIncreasing) _reverse(filePaths); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +class SortButton extends StatelessWidget { |
| 116 | + const SortButton({ |
| 117 | + super.key, |
| 118 | + required this.callback, |
| 119 | + }); |
| 120 | + |
| 121 | + final void Function() callback; |
| 122 | + |
| 123 | + @override |
| 124 | + Widget build(BuildContext context) { |
| 125 | + return IconButton( |
| 126 | + icon: Icon(Icons.sort), |
| 127 | + onPressed: () async { |
| 128 | + showDialog( |
| 129 | + context: context, |
| 130 | + builder: (BuildContext context) { |
| 131 | + return _SortButtonDialog(); |
| 132 | + }, |
| 133 | + ).then((_) => callback()); |
| 134 | + }, |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +class _SortButtonDialog extends StatefulWidget { |
| 140 | + @override |
| 141 | + State<_SortButtonDialog> createState() => _SortButtonDialogState(); |
| 142 | +} |
| 143 | + |
| 144 | +class _SortButtonDialogState extends State<_SortButtonDialog> { |
| 145 | + @override |
| 146 | + Widget build(BuildContext context) { |
| 147 | + // Needs to match the order of _sortFunctions |
| 148 | + final List<String> sortNames = [ |
| 149 | + t.home.sortNames.alphabetical, |
| 150 | + t.home.sortNames.lastModified, |
| 151 | + t.home.sortNames.sizeOnDisk, |
| 152 | + ]; |
| 153 | + |
| 154 | + return Align( |
| 155 | + alignment: Alignment.topRight, |
| 156 | + child: Container( |
| 157 | + width: 220, |
| 158 | + decoration: BoxDecoration(borderRadius: BorderRadius.circular(5)), |
| 159 | + clipBehavior: Clip.antiAlias, |
| 160 | + child: Material( |
| 161 | + child: Column( |
| 162 | + mainAxisSize: MainAxisSize.min, |
| 163 | + children: [ |
| 164 | + for (int idx = 0; idx < sortNames.length; idx++) |
| 165 | + RadioListTile<int>( |
| 166 | + title: Text(sortNames[idx]), |
| 167 | + onChanged: (int? newValue) => { |
| 168 | + SortNotes.sortFunctionIdx = newValue!, |
| 169 | + setState(() {}), |
| 170 | + // Navigator.pop(context), |
| 171 | + }, |
| 172 | + groupValue: SortNotes.sortFunctionIdx, |
| 173 | + value: idx, |
| 174 | + ), |
| 175 | + CheckboxListTile( |
| 176 | + controlAffinity: ListTileControlAffinity.leading, |
| 177 | + title: Text(t.home.sortNames.increasing), |
| 178 | + value: SortNotes.isIncreasingOrder, |
| 179 | + onChanged: (bool? v) => { |
| 180 | + SortNotes.isIncreasingOrder = v!, |
| 181 | + setState(() {}), |
| 182 | + }), |
| 183 | + ], |
| 184 | + ), |
| 185 | + ), |
| 186 | + ), |
| 187 | + ); |
| 188 | + } |
| 189 | +} |
0 commit comments