Skip to content

Commit

Permalink
Add notification for not connected to internet, fix video screen and …
Browse files Browse the repository at this point in the history
…channel widget
  • Loading branch information
prateekmedia committed Sep 13, 2021
1 parent 67b05e5 commit 9a9062e
Show file tree
Hide file tree
Showing 18 changed files with 572 additions and 284 deletions.
38 changes: 38 additions & 0 deletions lib/controller/internet_connectivity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';

enum NetworkStatus { online, offline, restored }

class InternetConnectivity {
static final StreamController _networkController = StreamController<NetworkStatus>.broadcast();

static Stream<NetworkStatus> get networkStream => _networkController.stream as Stream<NetworkStatus>;

static NetworkStatus _status = NetworkStatus.online;
static NetworkStatus get status => _status;

static void networkStatusService() async {
Connectivity().onConnectivityChanged.listen((status) async {
if (status != ConnectivityResult.none) {
_networkController.add(NetworkStatus.restored);
await Future.delayed(const Duration(seconds: 5));
if (status != ConnectivityResult.none) {
_networkController.add(NetworkStatus.online);
}
} else {
_networkController.add(NetworkStatus.offline);
}
});
_networkController.stream.listen((event) {
_status = event;
});
if (await Connectivity().checkConnectivity() == ConnectivityResult.none) {
_networkController.add(NetworkStatus.offline);
}
}

void dispose() {
_networkController.close();
}
}
21 changes: 16 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutube/controller/internet_connectivity.dart';
import 'package:ionicons/ionicons.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:hive_flutter/hive_flutter.dart';
Expand All @@ -9,17 +10,25 @@ import 'package:salomon_bottom_bar/salomon_bottom_bar.dart';
import 'package:flutube/utils/utils.dart';
import 'package:flutube/models/models.dart';
import 'package:flutube/screens/screens.dart';
import 'package:flutube/widgets/widgets.dart';
import 'package:flutube/providers/providers.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Connectivity check stream initialised.
InternetConnectivity.networkStatusService();

// Initialize SharedPreferences
await MyPrefs().init();

// Initialize Hive database
Hive.registerAdapter(LikedCommentAdapter());
Hive.registerAdapter(QueryVideoAdapter());
await Hive.initFlutter();
await Hive.openBox('playlist');
await Hive.openBox('likedList');
await Hive.openBox('downloadList');

runApp(const ProviderScope(child: MyApp()));
}

Expand Down Expand Up @@ -142,11 +151,13 @@ class MyHomePage extends HookWidget {
duration: const Duration(milliseconds: 300), curve: Curves.fastOutSlowIn),
),
Flexible(
child: PageView.builder(
controller: _controller,
itemCount: mainScreens.length,
itemBuilder: (context, index) => mainScreens[index],
onPageChanged: (index) => _currentIndex.value = index,
child: FtBody(
child: PageView.builder(
controller: _controller,
itemCount: mainScreens.length,
itemBuilder: (context, index) => mainScreens[index],
onPageChanged: (index) => _currentIndex.value = index,
),
),
),
],
Expand Down
21 changes: 8 additions & 13 deletions lib/screens/channel_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ class ChannelScreen extends HookWidget {
)
: null,
),
if (channel.hasData && channel.data != null) ...[
SliverToBoxAdapter(
SliverToBoxAdapter(
child: FtBody(
expanded: false,
child: FutureBuilder<ChannelUploadsList>(
future: YoutubeExplode().channels.getUploadsFromPage(channel.data!.id.value),
future: channel.hasData && channel.data != null
? YoutubeExplode().channels.getUploadsFromPage(channel.data!.id.value)
: null,
builder: (ctx, snapshot) {
return snapshot.hasData
? ListView.builder(
Expand All @@ -55,18 +58,10 @@ class ChannelScreen extends HookWidget {
: getCircularProgressIndicator();
},
),
)
] else
SliverToBoxAdapter(child: getCircularProgressIndicator()),
),
),
],
),
);
}

Widget getCircularProgressIndicator() {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 25),
child: const CircularProgressIndicator().center(),
);
}
}
33 changes: 9 additions & 24 deletions lib/screens/downloads_screen.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutube/screens/screens.dart';
import 'package:flutube/widgets/widgets.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'package:flutube/utils/utils.dart';
import 'package:flutube/models/models.dart';
import 'package:flutube/screens/screens.dart';
import 'package:flutube/widgets/widgets.dart';
import 'package:flutube/providers/providers.dart';
import 'package:open_file/open_file.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

class DownloadsScreen extends ConsumerWidget {
const DownloadsScreen({Key? key}) : super(key: key);
Expand Down Expand Up @@ -46,7 +45,6 @@ class DownloadItemBuilder extends StatelessWidget {

@override
Widget build(BuildContext context) {
var yt = YoutubeExplode();
return GestureDetector(
onTap: item.total != 0 && item.total == item.downloaded
? () => OpenFile.open(item.queryVideo.path + item.queryVideo.name)
Expand All @@ -57,23 +55,10 @@ class DownloadItemBuilder extends StatelessWidget {
children: [
GestureDetector(
onTap: () {
context.pushPage(FutureBuilder<Video>(
future: yt.videos.get(item.queryVideo.id).whenComplete(() => yt.close()),
builder: (context, snapshot) {
return snapshot.hasData
? VideoScreen(
video: snapshot.data!,
loadData: true,
)
: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.chevron_left),
onPressed: context.back,
),
),
body: const CircularProgressIndicator().center());
}));
context.pushPage(VideoScreen(
video: null,
videoId: item.queryVideo.id,
));
},
child: ClipRRect(
borderRadius: BorderRadius.circular(15),
Expand Down
13 changes: 9 additions & 4 deletions lib/screens/playlist_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@ class PlaylistSubScreen extends StatelessWidget {
onPressed: context.back,
),
),
body: ListView(
children: [
for (var videoUrl in currentPlaylist.value) FTVideo(videoUrl: videoUrl),
],
body: FtBody(
expanded: false,
child: currentPlaylist.value.isNotEmpty
? ListView(
children: [
for (var videoUrl in currentPlaylist.value) FTVideo(videoUrl: videoUrl),
],
)
: const Center(child: Text("No videos found!")),
),
);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/screens/search_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutube/widgets/ft_video.dart';
import 'package:flutube/widgets/widgets.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

import '../utils/utils.dart';
Expand Down Expand Up @@ -75,7 +76,7 @@ class SearchResult extends HookWidget {
isRow: context.width >= mobileWidth,
),
)
: const CircularProgressIndicator().center();
: getCircularProgressIndicator();
});
}
}
Expand Down Expand Up @@ -108,7 +109,7 @@ class SuggestionList extends HookWidget {
title: Text(snapshot.data![idx]),
),
)
: const CircularProgressIndicator().center(),
: getCircularProgressIndicator(),
),
),
);
Expand Down
5 changes: 5 additions & 0 deletions lib/screens/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> with AutomaticK
value: context.isDark,
onChanged: (bool value) => ref.read(themeTypeProvider.notifier).themeType = value ? 2 : 1,
),
SwitchListTile(
title: const Text('Thumbnail downloader'),
value: context.isDark,
onChanged: (bool value) => ref.read(themeTypeProvider.notifier).themeType = value ? 2 : 1,
),
],
);
}
Expand Down
Loading

0 comments on commit 9a9062e

Please sign in to comment.