Matthias Nott
2026-04-01 f6f948769e0aa78e086cbff48043c0fa624b8a19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import 'package:flutter/material.dart';
import '../models/session.dart';
import '../theme/app_theme.dart';
/// Side drawer showing session list with reordering, unread badges, and controls.
class SessionDrawer extends StatelessWidget {
  final List<Session> sessions;
  final String? activeSessionId;
  final Map<String, int> unreadCounts;
  final void Function(Session session) onSelect;
  final void Function(Session session) onRemove;
  final void Function(Session session, String newName) onRename;
  final void Function(int oldIndex, int newIndex) onReorder;
  final VoidCallback onNewSession;
  final VoidCallback onRefresh;
  const SessionDrawer({
    super.key,
    required this.sessions,
    required this.activeSessionId,
    required this.unreadCounts,
    required this.onSelect,
    required this.onRemove,
    required this.onRename,
    required this.onReorder,
    required this.onNewSession,
    required this.onRefresh,
  });
  @override
  Widget build(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    return Drawer(
      child: SafeArea(
        child: Column(
          children: [
            // Header
            Padding(
              padding:
                  const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    'Sessions',
                    style: Theme.of(context).textTheme.titleLarge,
                  ),
                  IconButton(
                    icon: const Icon(Icons.refresh, size: 22),
                    onPressed: onRefresh,
                    tooltip: 'Refresh sessions',
                  ),
                ],
              ),
            ),
            const Divider(height: 1),
            // Session list
            Expanded(
              child: sessions.isEmpty
                  ? const Center(
                      child: Text(
                        'No sessions',
                        style: TextStyle(color: AppColors.darkTextTertiary),
                      ),
                    )
                  : ReorderableListView.builder(
                      itemCount: sessions.length,
                      onReorder: onReorder,
                      buildDefaultDragHandles: false,
                      itemBuilder: (context, index) {
                        final session = sessions[index];
                        final isActive = session.id == activeSessionId;
                        final unread = unreadCounts[session.id] ?? 0;
                        return Dismissible(
                          key: ValueKey(session.id),
                          direction: DismissDirection.endToStart,
                          background: Container(
                            color: AppColors.error,
                            alignment: Alignment.centerRight,
                            padding: const EdgeInsets.only(right: 16),
                            child: const Icon(Icons.delete,
                                color: Colors.white),
                          ),
                          confirmDismiss: (_) async {
                            return await showDialog<bool>(
                              context: context,
                              builder: (ctx) => AlertDialog(
                                title: const Text('Remove session?'),
                                content: Text(
                                    'Remove "${session.name}" from the list?'),
                                actions: [
                                  TextButton(
                                    onPressed: () =>
                                        Navigator.pop(ctx, false),
                                    child: const Text('Cancel'),
                                  ),
                                  TextButton(
                                    onPressed: () =>
                                        Navigator.pop(ctx, true),
                                    child: const Text('Remove',
                                        style: TextStyle(
                                            color: AppColors.error)),
                                  ),
                                ],
                              ),
                            );
                          },
                          onDismissed: (_) => onRemove(session),
                          child: ListTile(
                            key: ValueKey('tile_${session.id}'),
                            leading: Text(
                              session.icon,
                              style: const TextStyle(fontSize: 20),
                            ),
                            title: GestureDetector(
                              onDoubleTap: () =>
                                  _showRenameDialog(context, session),
                              child: Text(
                                session.name,
                                style: TextStyle(
                                  fontWeight: isActive
                                      ? FontWeight.bold
                                      : FontWeight.normal,
                                  color: isActive ? AppColors.accent : null,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                            trailing: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                if (unread > 0)
                                  Container(
                                    padding: const EdgeInsets.symmetric(
                                        horizontal: 6, vertical: 2),
                                    decoration: BoxDecoration(
                                      color: AppColors.unreadBadge,
                                      borderRadius:
                                          BorderRadius.circular(10),
                                    ),
                                    child: Text(
                                      '$unread',
                                      style: const TextStyle(
                                        color: Colors.white,
                                        fontSize: 11,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ),
                                const SizedBox(width: 4),
                                ReorderableDragStartListener(
                                  index: index,
                                  child: Icon(
                                    Icons.drag_handle,
                                    color: isDark
                                        ? AppColors.darkTextTertiary
                                        : Colors.grey.shade400,
                                    size: 20,
                                  ),
                                ),
                              ],
                            ),
                            selected: isActive,
                            selectedTileColor: isDark
                                ? Colors.white.withAlpha(10)
                                : Colors.blue.withAlpha(15),
                            onTap: () {
                              onSelect(session);
                              Navigator.pop(context);
                            },
                          ),
                        );
                      },
                    ),
            ),
            // New session button
            const Divider(height: 1),
            Padding(
              padding: const EdgeInsets.all(12),
              child: SizedBox(
                width: double.infinity,
                child: ElevatedButton.icon(
                  onPressed: onNewSession,
                  icon: const Icon(Icons.add, size: 20),
                  label: const Text('New Session'),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.accent,
                    foregroundColor: Colors.white,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
  void _showRenameDialog(BuildContext context, Session session) {
    final controller = TextEditingController(text: session.name);
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        title: const Text('Rename session'),
        content: TextField(
          controller: controller,
          autofocus: true,
          decoration: const InputDecoration(hintText: 'Session name'),
          onSubmitted: (value) {
            if (value.isNotEmpty) {
              onRename(session, value);
            }
            Navigator.pop(ctx);
          },
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(ctx),
            child: const Text('Cancel'),
          ),
          TextButton(
            onPressed: () {
              if (controller.text.isNotEmpty) {
                onRename(session, controller.text);
              }
              Navigator.pop(ctx);
            },
            child: const Text('Save'),
          ),
        ],
      ),
    ).then((_) => controller.dispose());
  }
}