Skip to content

Commit

Permalink
wip: refactor theme
Browse files Browse the repository at this point in the history
  • Loading branch information
brainwo committed Oct 26, 2022
1 parent 0fee0e0 commit b752ebb
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 70 deletions.
Empty file added lib/config/config.dart
Empty file.
29 changes: 16 additions & 13 deletions lib/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ void playVideo(YouTubeVideo item) async {
}
];

var file = File('data.json');
if (await file.exists()) {
List<Map<String, dynamic>> previousData =
jsonDecode(await file.readAsString());
previousData.addAll(entry);
await file
.writeAsString(jsonEncode(previousData))
.whenComplete(() => SystemNavigator.pop());
} else {
await file
.writeAsString(jsonEncode(entry))
.whenComplete(() => SystemNavigator.pop());
}
// TODO: load data.json
// var file = File('data.json');
// if (await file.exists()) {
// List<Map<String, dynamic>> previousData =
// jsonDecode(await file.readAsString());
// previousData.addAll(entry);
// await file
// .writeAsString(jsonEncode(previousData))
// .whenComplete(() => SystemNavigator.pop());
// } else {
// await file
// .writeAsString(jsonEncode(entry))
// .whenComplete(() => SystemNavigator.pop());
// }

SystemNavigator.pop();
}
129 changes: 92 additions & 37 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
// ignore: unused_import
import 'dart:convert';
// ignore: unused_import
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:ythacker/helper.dart';
import 'helper.dart';
import 'theme.dart';

import 'widget/list_item.dart';

import 'theme/arc_dark.dart';
import 'string/en_us.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:youtube_api/youtube_api.dart';
import 'const.dart';

List<YouTubeVideo> result = [];
AppTheme appTheme = AppTheme.archDark();

void main() async {
ValueNotifier<List<YouTubeVideo>> notifier = ValueNotifier(result);

File file = File('data.json');
if (await file.exists()) {
String fileAsString = await file.readAsString();
List<dynamic> data = jsonDecode(fileAsString);
// result = data.map((item) => YouTubeVideo(item)).toList();
debugPrint('$result');
notifier.value = result;
} else {
debugPrint('awia');
}
// File config = File('~/.config/ythacker/config.json');

// if (await config.exists()) {
// config.readAsString();
// } else {
// config.create(recursive: true);
// }

// File file = File('data.json');
// if (await file.exists()) {
// String fileAsString = await file.readAsString();
// List<dynamic> data = jsonDecode(fileAsString);
// result = data.map((item) => YouTubeVideo(item)).toList();
// debugPrint('$result');
// notifier.value = result;
// } else {
// debugPrint('Create new data');
// }

runApp(AnimatedBuilder(
animation: notifier,
Expand Down Expand Up @@ -101,9 +112,9 @@ class App extends StatelessWidget {
title: 'YT Hacker',
theme: ThemeData(
brightness: Brightness.dark,
scaffoldBackgroundColor: AppTheme.background,
micaBackgroundColor: AppTheme.background,
activeColor: AppTheme.primary,
scaffoldBackgroundColor: appTheme.background,
micaBackgroundColor: appTheme.background,
activeColor: appTheme.primary,
),
home: HomePage(
searchBoxFocus: searchBoxFocus,
Expand All @@ -130,17 +141,42 @@ class HomePage extends StatefulWidget {
State<HomePage> createState() => _HomePageState();
}

enum SearchBoxMode { video, playlist, channel, all }

class _HomePageState extends State<HomePage> {
YoutubeAPI ytApi = YoutubeAPI(innerKey);
String noResultString = AppString.noSearchQuery;
bool loading = false;
SearchBoxMode searchBoxMode = SearchBoxMode.all;

@override
Widget build(BuildContext context) {
TextEditingController textBoxController = TextEditingController();

textBoxController.addListener(() {
if (searchBoxMode == SearchBoxMode.all) {
switch (textBoxController.text) {
case 'v ':
setState(() {
searchBoxMode = SearchBoxMode.video;
});
break;
case 'c ':
setState(() {
searchBoxMode = SearchBoxMode.channel;
});
break;
case 'p ':
setState(() {
searchBoxMode = SearchBoxMode.playlist;
});
break;
}
}
});

Widget searchResult = Container(
color: AppTheme.background,
color: appTheme.background,
child: (result.isEmpty)
? Center(child: Text(noResultString))
: ListItem(
Expand All @@ -150,28 +186,47 @@ class _HomePageState extends State<HomePage> {

return NavigationView(
appBar: NavigationAppBar(
backgroundColor: AppTheme.backgroundDarker,
backgroundColor: appTheme.backgroundDarker,
automaticallyImplyLeading: false,
title: TextBox(
placeholder: AppString.searchPlaceholder,
focusNode: widget.searchBoxFocus,
controller: textBoxController,
onSubmitted: (query) {
setState(() {
loading = true;
});
ytApi.search(query, type: 'playlist').then((value) {
setState(() {
result = value;
loading = false;
});
}, onError: (err) {
setState(() {
noResultString = '${AppString.errorInformation}$err';
loading = false;
});
});
},
title: Row(
children: [
if (searchBoxMode != SearchBoxMode.all)
Row(
children: [
Text(
'${searchBoxMode.name[0].toUpperCase()}${searchBoxMode.name.substring(1)}'),
const SizedBox(width: 8.0),
],
),
Expanded(
child: TextBox(
placeholder: searchBoxMode == SearchBoxMode.all ? AppString.searchPlaceholderAll : AppString.searchPlaceholder,
focusNode: widget.searchBoxFocus,
controller: textBoxController,
onSubmitted: (query) {
setState(() {
loading = true;
});
ytApi
.search(query,
type: searchBoxMode == SearchBoxMode.all
? 'video,channel,playlist'
: searchBoxMode.name)
.then((value) {
setState(() {
result = value;
loading = false;
});
}, onError: (err) {
setState(() {
noResultString = '${AppString.errorInformation}$err';
loading = false;
});
});
},
),
),
],
),
),
content: Stack(
Expand Down
3 changes: 2 additions & 1 deletion lib/string/en_us.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class AppString {
AppString._();

static const String noSearchQuery = 'No search query';
static const String searchPlaceholder = 'Search... (video, channel, playlist)';
static const String searchPlaceholderAll = 'Search... (video, channel, playlist)';
static const String searchPlaceholder = 'Search...';
static const String errorInformation = 'Error: ';

static const String video = 'Video';
Expand Down
33 changes: 33 additions & 0 deletions lib/theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:flutter/rendering.dart';

class AppTheme {
final Color background;
final Color backgroundDarker;
final Color text;
final Color primary;

AppTheme({
required this.background,
required this.backgroundDarker,
required this.text,
required this.primary,
});

static AppTheme archDark() {
return AppTheme(
background: const Color.fromRGBO(64, 69, 82, 1.0), // #404552
backgroundDarker: const Color.fromRGBO(47, 52, 63, 1.0), // #2F343F
text: const Color.fromRGBO(211, 218, 227, 1.0), // #D3DAE3
primary: const Color.fromRGBO(81, 144, 219, 1.0), // #5294E2
);
}

static AppTheme? from(String themeName) {
switch (themeName) {
case 'Arc-Dark':
return archDark();
default:
return null;
}
}
}
19 changes: 0 additions & 19 deletions lib/theme/arc_dark.dart

This file was deleted.

1 change: 1 addition & 0 deletions lib/widget/list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ListItem extends StatefulWidget {
final ValueNotifier<int?> selected;
final List<YouTubeVideo> result;


@override
State<ListItem> createState() => _ListItemState();
}
Expand Down

0 comments on commit b752ebb

Please sign in to comment.