Skip to content

Commit

Permalink
Merge pull request #159 from lamarios/feautre/no_need_to_restart_for_…
Browse files Browse the repository at this point in the history
…settings_changes

make settings not needing restart when changed
  • Loading branch information
lamarios authored Apr 30, 2023
2 parents fafd690 + 93701b7 commit 98a50f7
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 104 deletions.
3 changes: 3 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/264.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add Dislike count to vidoes (opt-in in options)
Fix search for languages like russion
No need to restart the app after changing theme settings anymore
Binary file modified fastlane/metadata/android/en-US/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions lib/controllers/appController.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:get/get.dart';
import 'package:invidious/utils.dart';

class AppController extends GetxController {
static AppController? to() => safeGet();
}
4 changes: 2 additions & 2 deletions lib/controllers/playerController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ class PlayerController extends GetxController {

bool isHls = video.hlsUrl != null;
String videoUrl = isHls
? '${video.hlsUrl!}${service.useProxy ? '?local=true' : ''}'
? '${video.hlsUrl!}${service.useProxy() ? '?local=true' : ''}'
: useDash
? '${video.dashUrl}${service.useProxy ? '?local=true' : ''}'
? '${video.dashUrl}${service.useProxy() ? '?local=true' : ''}'
: video.formatStreams[video.formatStreams.length - 1].url;

log.info('Playing url (dash ${useDash}, hasHls ? ${video.hlsUrl != null}) ${videoUrl}');
Expand Down
9 changes: 9 additions & 0 deletions lib/controllers/settingsController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../models/country.dart';
import '../models/db/server.dart';
import '../models/db/settings.dart';
import '../utils.dart';
import 'appController.dart';

const String subtitleDefaultSize = '14';

Expand Down Expand Up @@ -50,6 +51,7 @@ class SettingsController extends GetxController {
db.saveSetting(SettingsValue(DYNAMIC_THEME, value.toString()));
useDynamicTheme = value;
update();
updateApp();
}

toggleDash(bool value) {
Expand Down Expand Up @@ -106,6 +108,7 @@ class SettingsController extends GetxController {
db.saveSetting(SettingsValue(BLACK_BACKGROUND, value.toString()));
blackBackground = value;
update();
AppController.to()?.update();
}

changeSubtitleSize({required bool increase}) {
Expand Down Expand Up @@ -137,6 +140,7 @@ class SettingsController extends GetxController {
db.saveSetting(SettingsValue(THEME_MODE, theme.name));
}
update();
updateApp();
}

setLocale(List<Locale> locals, List<String> localStrings, String? locale) {
Expand All @@ -157,6 +161,11 @@ class SettingsController extends GetxController {
db.saveSetting(SettingsValue(LOCALE, toSave));
}
update();
updateApp();
}

updateApp(){
AppController.to()?.update();
}

String? getLocaleDisplayName() {
Expand Down
6 changes: 3 additions & 3 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
"@useDynamicTheme": {
"description": ""
},
"useDynamicThemeDescription": "Use Material You colors (only available on Android 12+). Requires app restart",
"useDynamicThemeDescription": "Use Material You colors (only available on Android 12+)",
"@useDynamicThemeDescription": {
"description": ""
},
Expand Down Expand Up @@ -473,7 +473,7 @@
"@useProxy": {
"description": "label for settings switch to proxy videos from server"
},
"useProxyDescription": "By proxying video streams from the server, you can bypass regional blocks or ISP blocking YouTube. Requires app restart",
"useProxyDescription": "By proxying video streams from the server, you can bypass regional blocks or ISP blocking YouTube",
"@useProxyDescription": {
"description": "Description for the use proxy settings"
},
Expand Down Expand Up @@ -501,7 +501,7 @@
"@blackBackground": {
"description": "Settings name for black background"
},
"blackBackgroundDescription": "For dark theme on OLED screen. Requires app restart.",
"blackBackgroundDescription": "For dark theme on OLED screen",
"@blackBackgroundDescription": {
"description": "Description for dark background setting"
},
Expand Down
153 changes: 80 additions & 73 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:invidious/views/tv/tvWelcomeWizard.dart';
import 'package:invidious/views/welcomeWizard.dart';
import 'package:logging/logging.dart';

import 'controllers/appController.dart';
import 'database.dart';
import 'myRouteObserver.dart';

Expand Down Expand Up @@ -56,92 +57,98 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
bool showWizard = false;
bool useDynamicTheme = db.getSettings(DYNAMIC_THEME)?.value == 'true';
try {
db.getCurrentlySelectedServer();
} catch (err) {
showWizard = true;
}

// TODO: implement build
return DynamicColorBuilder(builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
ColorScheme lightColorScheme;
ColorScheme darkColorScheme;
return GetBuilder<AppController>(
init: AppController(),
builder: (_) {

if (useDynamicTheme && lightDynamic != null && darkDynamic != null) {
// On Android S+ devices, use the provided dynamic color scheme.
// (Recommended) Harmonize the dynamic color scheme' built-in semantic colors.
lightColorScheme = lightDynamic.harmonized();
// (Optional) Customize the scheme as desired. For example, one might
// want to use a brand color to override the dynamic [ColorScheme.secondary].
// lightColorScheme = lightColorScheme.copyWith(secondary: brandColor);
// (Optional) If applicable, harmonize custom colors.
// lightCustomColors = lightCustomColors.harmonized(lightColorScheme);
bool useDynamicTheme = db.getSettings(DYNAMIC_THEME)?.value == 'true';

// Repeat for the dark color scheme.
darkColorScheme = darkDynamic.harmonized();
// darkColorScheme = darkColorScheme.copyWith(secondary: brandColor);
// darkCustomColors = darkCustomColors.harmonized(darkColorScheme);
return DynamicColorBuilder(builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
ColorScheme lightColorScheme;
ColorScheme darkColorScheme;

// _isDemoUsingDynamicColors = true; // ignore, only for demo purposes
} else {
// Otherwise, use fallback schemes.
lightColorScheme = ColorScheme.fromSeed(
seedColor: brandColor,
);
darkColorScheme = ColorScheme.fromSeed(
seedColor: brandColor,
brightness: Brightness.dark,
);
}
if (useDynamicTheme && lightDynamic != null && darkDynamic != null) {
// On Android S+ devices, use the provided dynamic color scheme.
// (Recommended) Harmonize the dynamic color scheme' built-in semantic colors.
lightColorScheme = lightDynamic.harmonized();
// (Optional) Customize the scheme as desired. For example, one might
// want to use a brand color to override the dynamic [ColorScheme.secondary].
// lightColorScheme = lightColorScheme.copyWith(secondary: brandColor);
// (Optional) If applicable, harmonize custom colors.
// lightCustomColors = lightCustomColors.harmonized(lightColorScheme);

if (db.getSettings(BLACK_BACKGROUND)?.value == 'true') {
darkColorScheme = darkColorScheme.copyWith(background: Colors.black);
}
// Repeat for the dark color scheme.
darkColorScheme = darkDynamic.harmonized();
// darkColorScheme = darkColorScheme.copyWith(secondary: brandColor);
// darkCustomColors = darkCustomColors.harmonized(darkColorScheme);

List<String>? localeString = db.getSettings(LOCALE)?.value.split('_');
Locale? locale = localeString != null ? Locale.fromSubtags(languageCode: localeString[0], scriptCode: localeString.length >= 2 ? localeString[1] : null) : null;
// _isDemoUsingDynamicColors = true; // ignore, only for demo purposes
} else {
// Otherwise, use fallback schemes.
lightColorScheme = ColorScheme.fromSeed(
seedColor: brandColor,
);
darkColorScheme = ColorScheme.fromSeed(
seedColor: brandColor,
brightness: Brightness.dark,
);
}

return GetMaterialApp(
locale: locale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
scaffoldMessengerKey: scaffoldKey,
navigatorKey: globalNavigator,
debugShowCheckedModeBanner: false,
themeMode: ThemeMode.values.firstWhere((element) => element.name == db.getSettings(THEME_MODE)?.value, orElse: () => ThemeMode.system),
title: 'Clipious',
theme: ThemeData(
useMaterial3: true,
colorScheme: lightColorScheme,
),
darkTheme: ThemeData(useMaterial3: true, colorScheme: darkColorScheme),
home: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
child: isTv
? showWizard
? const TvWelcomeWizard()
: const TvHome()
: Stack(
children: [
MiniPlayerAware(
child: Navigator(
observers: [MyRouteObserver()],
key: navigatorKey,
initialRoute: '/',
onGenerateRoute: (settings) {
if (settings.name == '/') {
return GetPageRoute(page: () => showWizard ? const WelcomeWizard() : const Home());
}
}),
),
const MiniPlayer()
],
),
));
});
if (db.getSettings(BLACK_BACKGROUND)?.value == 'true') {
darkColorScheme = darkColorScheme.copyWith(background: Colors.black);
}

List<String>? localeString = db.getSettings(LOCALE)?.value.split('_');
Locale? locale = localeString != null ? Locale.fromSubtags(languageCode: localeString[0], scriptCode: localeString.length >= 2 ? localeString[1] : null) : null;

return GetMaterialApp(
locale: locale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
scaffoldMessengerKey: scaffoldKey,
navigatorKey: globalNavigator,
debugShowCheckedModeBanner: false,
themeMode: ThemeMode.values.firstWhere((element) => element.name == db.getSettings(THEME_MODE)?.value, orElse: () => ThemeMode.system),
title: 'Clipious',
theme: ThemeData(
useMaterial3: true,
colorScheme: lightColorScheme,
),
darkTheme: ThemeData(useMaterial3: true, colorScheme: darkColorScheme),
home: Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
child: isTv
? showWizard
? const TvWelcomeWizard()
: const TvHome()
: Stack(
children: [
MiniPlayerAware(
child: Navigator(
observers: [MyRouteObserver()],
key: navigatorKey,
initialRoute: '/',
onGenerateRoute: (settings) {
if (settings.name == '/') {
return GetPageRoute(page: () => showWizard ? const WelcomeWizard() : const Home());
}
}),
),
const MiniPlayer()
],
),
));
});
});
}
}

Expand Down
31 changes: 10 additions & 21 deletions lib/service.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:convert';

import 'package:fbroadcast/fbroadcast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_web_auth/flutter_web_auth.dart';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
Expand All @@ -16,7 +14,6 @@ import 'package:invidious/models/sponsorSegment.dart';
import 'package:invidious/models/userFeed.dart';
import 'package:invidious/models/video.dart';
import 'package:invidious/models/videoInList.dart';
import 'package:invidious/views/subscriptions.dart';
import 'package:logging/logging.dart';

import 'models/channel.dart';
Expand Down Expand Up @@ -57,7 +54,6 @@ const MAX_PING = 9007199254740991;

class Service {
final log = Logger('Service');
final bool useProxy = db.getSettings(USE_PROXY)?.value == 'true';

handleResponse(Response response) {
var body = utf8.decode(response.bodyBytes);
Expand All @@ -83,17 +79,19 @@ class Service {
}
}

bool useProxy() {
return db.getSettings(USE_PROXY)?.value == 'true';
}

Uri buildUrl(String baseUrl, {Map<String, String>? pathParams, Map<String, String?>? query}) {
try {
String url = '${db
.getCurrentlySelectedServer()
.url}$baseUrl';
String url = '${db.getCurrentlySelectedServer().url}$baseUrl';

pathParams?.forEach((key, value) {
url = url.replaceAll(key, value);
});

if (useProxy) {
if (useProxy()) {
query ??= {};
query['local'] = 'true';
}
Expand Down Expand Up @@ -152,9 +150,7 @@ class Service {
String url = '$serverUrl/authorize_token?scopes=:feed,:subscriptions*,:playlists*&callback_url=clipious-auth://';
final result = await FlutterWebAuth.authenticate(url: url, callbackUrlScheme: 'clipious-auth');

final token = Uri
.parse(result)
.queryParameters['token'];
final token = Uri.parse(result).queryParameters['token'];

Server? server = db.getServer(serverUrl);

Expand Down Expand Up @@ -183,9 +179,7 @@ class Service {
}

Future<List<VideoInList>> getTrending({String? type}) async {
String countryCode = db
.getSettings(BROWSING_COUNTRY)
?.value ?? 'US';
String countryCode = db.getSettings(BROWSING_COUNTRY)?.value ?? 'US';
// parse.queryParameters['region'] = countryCode;
Map<String, String>? query = {'region': countryCode};

Expand Down Expand Up @@ -251,7 +245,6 @@ class Service {
url += '&categories=[${categories.map((e) => '"${e.name}"').join(",")}]';
}


log.info('Calling $url');
final response = await http.get(Uri.parse(url));
Iterable i = handleResponse(response);
Expand Down Expand Up @@ -430,18 +423,14 @@ class Service {
}

Future<Duration?> pingServer(String url) async {
int start = DateTime
.now()
.millisecondsSinceEpoch;
int start = DateTime.now().millisecondsSinceEpoch;
String fullUri = '$url${STATS}';
log.info('calling ${fullUri}');
final response = await http.get(Uri.parse(fullUri), headers: {'Content-Type': 'application/json; charset=utf-16'});

try {
handleResponse(response);
var diff = DateTime
.now()
.millisecondsSinceEpoch - start;
var diff = DateTime.now().millisecondsSinceEpoch - start;
return Duration(milliseconds: diff);
} catch (err) {
log.info(err);
Expand Down
4 changes: 2 additions & 2 deletions lib/views/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Settings extends StatelessWidget {
),
SettingsTile(
title: Text(locals.appLanguage),
value: Text('${_.getLocaleDisplayName() ?? locals.followSystem}. ${locals.requiresRestart}'),
value: Text(_.getLocaleDisplayName() ?? locals.followSystem),
onPressed: (context) => showSelectLanguage(context, _),
),
SettingsTile.switchTile(
Expand Down Expand Up @@ -218,7 +218,7 @@ class Settings extends StatelessWidget {
),
SettingsTile(
title: Text(locals.themeBrightness),
value: Text('${_.getThemeLabel(locals, _.themeMode)}. ${locals.requiresRestart}'),
value: Text(_.getThemeLabel(locals, _.themeMode)),
onPressed: (context) => selectTheme(context, _),
),
SettingsTile.switchTile(
Expand Down
Loading

0 comments on commit 98a50f7

Please sign in to comment.