Skip to content

Commit

Permalink
Add app preview hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedon-dev committed Dec 1, 2024
1 parent 1f3346e commit f8fa58e
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 44 deletions.
1 change: 1 addition & 0 deletions lib/backend/managers/shared_prefs_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Future<void> setDefaultSettings([forceReset = false]) async {
await sharedStorage.setBool("enable_watch_history", true);
await sharedStorage.setBool("enable_search_history", true);
await sharedStorage.setBool("keyboard_incognito_mode", true);
await sharedStorage.setBool("hide_app_preview", true);
await sharedStorage.setBool("auto_play", false);
await sharedStorage.setBool("show_progress_thumbnails", true);
await sharedStorage.setInt("preferred_video_quality", 2160); // 4K
Expand Down
129 changes: 91 additions & 38 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:io';

import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:secure_app_switcher/secure_app_switcher.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '/backend/custom_logger.dart';
Expand All @@ -24,6 +27,9 @@ final logger = Logger(
);
late PackageInfo packageInfo;

/// This stores the global setting of whether the preview should be hidden
bool hidePreview = true;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
logger.i("Initializing app");
Expand All @@ -46,7 +52,7 @@ class ViewerApp extends StatefulWidget {
context.findAncestorStateOfType<ViewerAppState>();
}

class ViewerAppState extends State<ViewerApp> {
class ViewerAppState extends State<ViewerApp> with WidgetsBindingObserver {
bool updateAvailable = false;
bool isDownloadingUpdate = false;
bool updateFailed = false;
Expand All @@ -55,6 +61,8 @@ class ViewerAppState extends State<ViewerApp> {
double downloadProgress = 0.0;
UpdateManager updateManager = UpdateManager();

/// This controls whether the preview should be currently blocked
bool blockPreview = false;
int _selectedIndex = 0;
static List<Widget> screenList = <Widget>[
const HomeScreen(),
Expand All @@ -66,6 +74,17 @@ class ViewerAppState extends State<ViewerApp> {
@override
void initState() {
super.initState();
// Hide app preview by default
SecureAppSwitcher.on();
sharedStorage.getBool("hide_app_preview").then((value) {
if (!value!) {
SecureAppSwitcher.off();
}
setState(() => hidePreview = value);
});

// For detecting app state
WidgetsBinding.instance.addObserver(this);
Future<List<String?>> updateResponseFuture = updateManager.checkForUpdate();
updateResponseFuture.whenComplete(() async {
List<String?> updateFuture = await updateResponseFuture;
Expand All @@ -84,10 +103,37 @@ class ViewerAppState extends State<ViewerApp> {

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
updateManager.removeListener(() {});
super.dispose();
}

// This is only necessary for desktops, as the mobile platforms have that feature built in
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (hidePreview) {
logger.i("Lifecycle state changed to $state");
if (state == AppLifecycleState.paused ||
state == AppLifecycleState.inactive) {
logger.i("Lifecycle state is paused or inactive");
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
logger.i("Blurring app");
setState(() {
blockPreview = true;
});
}
} else if (state == AppLifecycleState.resumed) {
logger.i("Lifecycle state is resumed");
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
logger.i("Unblurring app");
setState(() {
blockPreview = false;
});
}
}
}
}

@override
Widget build(BuildContext context) {
return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) {
Expand All @@ -112,43 +158,50 @@ class ViewerAppState extends State<ViewerApp> {
brightness: Brightness.dark),
),
themeMode: snapshot.data,
home: updateAvailable
? buildUpdateDialogue()
: Scaffold(
bottomNavigationBar: NavigationBar(
destinations: <Widget>[
NavigationDestination(
icon: _selectedIndex == 0
? const Icon(Icons.home)
: const Icon(Icons.home_outlined),
label: "Home",
),
NavigationDestination(
icon: _selectedIndex == 1
? const Icon(Icons.subscriptions)
: const Icon(Icons.subscriptions_outlined),
label: "Subscriptions",
),
NavigationDestination(
icon: _selectedIndex == 2
? const Icon(Icons.video_library)
: const Icon(Icons.video_library_outlined),
label: "Library",
),
NavigationDestination(
icon: _selectedIndex == 3
? const Icon(Icons.settings)
: const Icon(Icons.settings_outlined),
label: "Settings",
),
],
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() {
_selectedIndex = index;
});
}),
body: screenList.elementAt(_selectedIndex)),
home: Stack(children: [
updateAvailable
? buildUpdateDialogue()
: Scaffold(
bottomNavigationBar: NavigationBar(
destinations: <Widget>[
NavigationDestination(
icon: _selectedIndex == 0
? const Icon(Icons.home)
: const Icon(Icons.home_outlined),
label: "Home",
),
NavigationDestination(
icon: _selectedIndex == 1
? const Icon(Icons.subscriptions)
: const Icon(Icons.subscriptions_outlined),
label: "Subscriptions",
),
NavigationDestination(
icon: _selectedIndex == 2
? const Icon(Icons.video_library)
: const Icon(Icons.video_library_outlined),
label: "Library",
),
NavigationDestination(
icon: _selectedIndex == 3
? const Icon(Icons.settings)
: const Icon(Icons.settings_outlined),
label: "Settings",
),
],
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() {
_selectedIndex = index;
});
}),
body: screenList.elementAt(_selectedIndex)),
if (blockPreview) ...[
Positioned.fill(
child: Container(color: Colors.black),
),
]
]),
);
});
});
Expand Down
29 changes: 29 additions & 0 deletions lib/ui/screens/settings/settings_misc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:secure_app_switcher/secure_app_switcher.dart';

import '/main.dart';
import 'custom_widgets/options_switch.dart';
Expand All @@ -24,6 +27,32 @@ class _MiscScreenState extends State<MiscScreen> {
padding: const EdgeInsets.all(8),
child: Column(
children: <Widget>[
FutureBuilder<bool?>(
future: sharedStorage.getBool("hide_app_preview"),
builder: (context, snapshot) {
// only build when data finished loading
if (snapshot.data == null) {
return const SizedBox();
}
return OptionsSwitch(
title: "Hide app preview",
subTitle: "Hide app preview in app switcher",
switchState: snapshot.data!,
onToggled: (value) async {
await sharedStorage.setBool(
"hide_app_preview", value);
// Force an immediate update
if (Platform.isAndroid || Platform.isIOS) {
if (!value) {
SecureAppSwitcher.off();
} else {
SecureAppSwitcher.on();
}
}
// the hidePreview var is from main.dart
setState(() => hidePreview = value);
});
}),
FutureBuilder<bool?>(
future:
sharedStorage.getBool("keyboard_incognito_mode"),
Expand Down
20 changes: 14 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,10 @@ packages:
dependency: transitive
description:
name: path_provider_android
sha256: c464428172cb986b758c6d1724c603097febb8fb855aa265aeecc9280c294d4a
sha256: "8c4967f8b7cb46dc914e178daa29813d83ae502e0529d7b0478330616a691ef7"
url: "https://pub.dev"
source: hosted
version: "2.2.12"
version: "2.2.14"
path_provider_foundation:
dependency: transitive
description:
Expand Down Expand Up @@ -554,6 +554,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.0"
secure_app_switcher:
dependency: "direct main"
description:
name: secure_app_switcher
sha256: "6f8a1755d15358291e697d212a2a5a28a302cc14798c0849e8630c12b4df4efd"
url: "https://pub.dev"
source: hosted
version: "0.1.0+1"
share_plus:
dependency: "direct main"
description:
Expand Down Expand Up @@ -675,10 +683,10 @@ packages:
dependency: transitive
description:
name: sqlite3
sha256: bb174b3ec2527f9c5f680f73a89af8149dd99782fbb56ea88ad0807c5638f2ed
sha256: cb7f4e9dc1b52b1fa350f7b3d41c662e75fc3d399555fa4e5efcf267e9a4fbb5
url: "https://pub.dev"
source: hosted
version: "2.4.7"
version: "2.5.0"
sqlite3_flutter_libs:
dependency: "direct main"
description:
Expand Down Expand Up @@ -915,10 +923,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: "84ba388638ed7a8cb3445a320c8273136ab2631cd5f2c57888335504ddab1bc2"
sha256: "8b338d4486ab3fbc0ba0db9f9b4f5239b6697fcee427939a40e720cbb9ee0a69"
url: "https://pub.dev"
source: hosted
version: "5.8.0"
version: "5.9.0"
window_manager:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
share_plus: ^10.0.2
html_unescape: ^2.0.0
linkify: ^5.0.0
secure_app_switcher: ^0.1.0+1


dev_dependencies:
Expand Down

0 comments on commit f8fa58e

Please sign in to comment.