Socialify

Folder ..

Viewing dokidoki.dart
192 lines (180 loc) • 5.4 KB

  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
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'theme.dart';
import 'views/views.dart';
import 'github/profile.dart';
import 'package:github/github.dart';

// DokiDoki is a Github Client for MacOS
class DokiDoki extends StatelessWidget {
  const DokiDoki({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => AppTheme(),
      builder: (context, _) {
        final appTheme = context.watch<AppTheme>();
        return MacosApp(
          title: 'DokiDoki',
          theme: MacosThemeData.light(),
          darkTheme: MacosThemeData.dark(),
          themeMode: appTheme.mode,
          debugShowCheckedModeBanner: false,
          home: const StartDokiDoki(),
        );
      },
    );
  }
}

class StartDokiDoki extends StatefulWidget {
  const StartDokiDoki({super.key});

  @override
  State<StartDokiDoki> createState() => _StartDokiDokiState();
}

class _StartDokiDokiState extends State<StartDokiDoki> {
  int pageIndex = 0;
  SharedPreferences? prefs;
  String? token;

  @override
  void initState() {
    super.initState();
    _loadToken();
  }

  // Load the token from the shared preferences
  Future<void> _loadToken() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      this.prefs = prefs;
      token = prefs.getString('token');
    });
  }

  // Get profile from Github
  Future _getProfile(token) async {
    if (token != null) {
      try {
        final github = GithubProfile(token);
        final user = await github.getProfile();
        return user;
      } catch (e) {
        prefs!.remove('token');
        exit(0);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _getProfile(token),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return MainApp(
            token: token!,
            user: snapshot.data,
            pageIndex: pageIndex,
            onChanged: (page) => setState(() => pageIndex = page),
          );
        } else {
          return const Center(
            child: ProgressCircle(),
          );
        }
      },
    );
  }
}

class MainApp extends StatelessWidget {
  const MainApp({
    super.key,
    required this.token,
    required this.user,
    required this.pageIndex,
    required this.onChanged,
  });

  final String token;
  final User user;
  final int pageIndex;
  final ValueChanged<int> onChanged;

  @override
  Widget build(BuildContext context) {
    var name = user.name ?? user.login;
    var email = user.email ?? '@${user.login}';
    var avatarUrl = user.avatarUrl ?? '';
    return PlatformMenuBar(
      menus: const [
        PlatformMenu(
          label: 'DokiDoki',
          menus: [
            PlatformProvidedMenuItem(
              type: PlatformProvidedMenuItemType.about,
            ),
            PlatformProvidedMenuItem(
              type: PlatformProvidedMenuItemType.quit,
            ),
          ],
        )
      ],
      child: MacosWindow(
          sidebar: Sidebar(
            minWidth: 200,
            // isResizable: false,
            startWidth: 200,
            topOffset: 40,
            builder: (context, scrollController) {
              return SidebarItems(
                currentIndex: pageIndex,
                onChanged: onChanged,
                items: const [
                  SidebarItem(label: Text('Repositories')),
                  SidebarItem(label: Text('Pull Requests')),
                  SidebarItem(label: Text('Issues')),
                  SidebarItem(label: Text('Gists')),
                ],
              );
            },
            bottom: MacosListTile(
              leading: avatarUrl.isNotEmpty
                  ? CircleAvatar(
                      backgroundImage: NetworkImage(avatarUrl),
                      maxRadius: 16,
                    )
                  : const MacosIcon(
                      CupertinoIcons.profile_circled,
                      size: 32,
                    ),
              title: Text('$name'),
              subtitle: Text(email),
              onClick: () => showMacosAlertDialog(
                context: context,
                builder: (context) => MacosAlertDialog(
                  appIcon: avatarUrl.isNotEmpty
                      ? CircleAvatar(
                          backgroundImage: NetworkImage(avatarUrl),
                          maxRadius: 32,
                        )
                      : const MacosIcon(
                          CupertinoIcons.profile_circled,
                          size: 64,
                        ),
                  title: const Text(
                    'Not Implemented Yet',
                  ),
                  message: const Text(
                    'This is a placeholder for the user profile page',
                  ),
                  //horizontalActions: false,
                  primaryButton: PushButton(
                    buttonSize: ButtonSize.large,
                    onPressed: Navigator.of(context).pop,
                    child: const Text('Okie!'),
                  ),
                ),
              ),
            ),
          ),
          child: IndexedStack(index: pageIndex, children: views)),
    );
  }
}