Skip to content

Commit

Permalink
♻️ Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LNA-DEV committed Nov 1, 2024
1 parent 9074f8e commit 3f3ce37
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 117 deletions.
4 changes: 2 additions & 2 deletions lib/apis/auth_info_api.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:convert';
import 'package:open_media_server_app/apis/base_api.dart';
import 'package:http/http.dart' as http;
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/preference_globals.dart';
import 'package:open_media_server_app/models/auth/auth_info.dart';

class AuthInfoApi {
Future<AuthInfo> getAuthInfo() async {
String apiUrl = "${Globals.BaseUrl}/auth/info";
String apiUrl = "${PreferenceGlobals.BaseUrl}/auth/info";

var headers = BaseApi.getHeaders();

Expand Down
2 changes: 1 addition & 1 deletion lib/apis/base_api.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:open_media_server_app/helpers/Preferences.dart';
import 'package:open_media_server_app/helpers/preferences.dart';

class BaseApi {
static Map<String, String> getHeaders() {
Expand Down
12 changes: 6 additions & 6 deletions lib/apis/inventory_api.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:convert';
import 'package:open_media_server_app/apis/base_api.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/preference_globals.dart';
import 'package:open_media_server_app/models/inventory/episode.dart';
import 'package:open_media_server_app/models/inventory/inventory_item.dart';
import 'package:http/http.dart' as http;
Expand All @@ -10,7 +10,7 @@ import 'package:open_media_server_app/models/inventory/show.dart';

class InventoryApi {
Future<List<InventoryItem>> listItems(String category) async {
String apiUrl = "${Globals.BaseUrl}/api/inventory/items?";
String apiUrl = "${PreferenceGlobals.BaseUrl}/api/inventory/items?";

var headers = BaseApi.getHeaders();

Expand All @@ -26,7 +26,7 @@ class InventoryApi {
}

Future<Movie> getMovie(String id) async {
String apiUrl = "${Globals.BaseUrl}/api/inventory/movie?";
String apiUrl = "${PreferenceGlobals.BaseUrl}/api/inventory/movie?";

var headers = BaseApi.getHeaders();

Expand All @@ -42,7 +42,7 @@ class InventoryApi {
}

Future<Show> getShow(String id) async {
String apiUrl = "${Globals.BaseUrl}/api/inventory/show?";
String apiUrl = "${PreferenceGlobals.BaseUrl}/api/inventory/show?";

var headers = BaseApi.getHeaders();

Expand All @@ -58,7 +58,7 @@ class InventoryApi {
}

Future<Season> getSeason(String id) async {
String apiUrl = "${Globals.BaseUrl}/api/inventory/season?";
String apiUrl = "${PreferenceGlobals.BaseUrl}/api/inventory/season?";

var headers = BaseApi.getHeaders();

Expand All @@ -74,7 +74,7 @@ class InventoryApi {
}

Future<Episode> getEpisode(String id) async {
String apiUrl = "${Globals.BaseUrl}/api/inventory/episode?";
String apiUrl = "${PreferenceGlobals.BaseUrl}/api/inventory/episode?";

var headers = BaseApi.getHeaders();

Expand Down
4 changes: 2 additions & 2 deletions lib/apis/metadata_api.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:convert';
import 'package:open_media_server_app/apis/base_api.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/preference_globals.dart';
import 'package:open_media_server_app/models/metadata/metadata_model.dart';
import 'package:http/http.dart' as http;

class MetadataApi {
Future<MetadataModel> getMetadata(String id, String category) async {
String apiUrl = "${Globals.BaseUrl}/api/metadata?";
String apiUrl = "${PreferenceGlobals.BaseUrl}/api/metadata?";

var headers = BaseApi.getHeaders();

Expand Down
15 changes: 8 additions & 7 deletions lib/auth/login_manager.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:open_media_server_app/auth/auth_globals.dart';
import 'package:open_media_server_app/globals/auth_globals.dart';
import 'package:open_media_server_app/auth/oauth_handler/custom_web_base_dummy.dart'
if (dart.library.html) './oauth_handler/custom_web_base.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -7,8 +7,9 @@ import 'package:oauth2_client/access_token_response.dart';
import 'package:oauth2_client/interfaces.dart';
import 'package:oauth2_client/oauth2_client.dart';
import 'package:open_media_server_app/auth/device_code.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/helpers/Preferences.dart';
import 'package:open_media_server_app/globals/globals.dart';
import 'package:open_media_server_app/globals/platform_globals.dart';
import 'package:open_media_server_app/helpers/preferences.dart';
import 'package:open_media_server_app/models/auth/auth_info.dart';
import 'package:random_string/random_string.dart';

Expand All @@ -18,16 +19,16 @@ class LoginManager {
BaseWebAuth? baseWebAuth;

LoginManager(AuthInfo authInfo) {
if (Globals.isTv) {
if (PlatformGlobals.isTv) {
// Do nothing
} else if (!Globals.isWeb) {
} else if (!PlatformGlobals.isWeb) {
client = OAuth2Client(
authorizeUrl: authInfo.authorizeUrl,
tokenUrl: authInfo.tokenUrl,
redirectUri: "my.test.app:/oauth2redirect", // TODO
customUriScheme: "my.test.app",
);
} else if (Globals.isWeb) {
} else if (PlatformGlobals.isWeb) {
client = OAuth2Client(
authorizeUrl: authInfo.authorizeUrl,
tokenUrl: authInfo.tokenUrl,
Expand All @@ -43,7 +44,7 @@ class LoginManager {
}

Future<String?> login(AuthInfo authInfo, BuildContext context) async {
if (Globals.isTv) {
if (PlatformGlobals.isTv) {
DeviceCode deviceCode = DeviceCode();
var token = await deviceCode.authenticateUser(
authInfo, "offline_access", authInfo.deviceCodeUrl, context);
Expand Down
4 changes: 2 additions & 2 deletions lib/auth/oauth_handler/custom_web_base.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:oauth2_client/interfaces.dart';
import 'package:open_media_server_app/auth/auth_globals.dart';
import 'package:open_media_server_app/globals/auth_globals.dart';
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;

import 'package:open_media_server_app/helpers/Preferences.dart';
import 'package:open_media_server_app/helpers/preferences.dart';

class CustomWebBase implements BaseWebAuth {
@override
Expand Down
6 changes: 4 additions & 2 deletions lib/auth/auth_globals.dart → lib/globals/auth_globals.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/preference_globals.dart';

class AuthGlobals {
static String get redirectUriWeb {
if (kDebugMode) {
return "http://localhost:8000/redirect.html";
} else {
return "https://${Globals.BaseUrl}/redirect.html";
return "https://${PreferenceGlobals.BaseUrl}/redirect.html";
}
}

static String? appLoginCodeRoute;

}
7 changes: 0 additions & 7 deletions lib/globals.dart → lib/globals/globals.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
class Globals {
static String BaseUrl = "http://localhost:8080";

static String Title = "Open Media Station";
static String PictureNotFoundUrl =
"https://static.vecteezy.com/system/resources/previews/005/337/799/original/icon-image-not-found-free-vector.jpg";

static bool isTv = false;
static bool isAndroidTv = false;
static bool isMobile = false;
static bool isWeb = false;
}
34 changes: 34 additions & 0 deletions lib/globals/platform_globals.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';

class PlatformGlobals {
static bool isTv = false;
static bool isAndroidTv = false;
static bool isMobile = false;
static bool isWeb = false;

static Future setGlobals() async {
PlatformGlobals.isMobile = defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.android;

if (defaultTargetPlatform == TargetPlatform.android) {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
PlatformGlobals.isTv =
androidInfo.systemFeatures.contains('android.software.leanback');
}

if (PlatformGlobals.isTv) {
PlatformGlobals.isMobile = false;
}

if (PlatformGlobals.isTv &&
defaultTargetPlatform == TargetPlatform.android) {
PlatformGlobals.isAndroidTv = true;
}

if (kIsWeb) {
PlatformGlobals.isWeb = true;
}
}
}
5 changes: 5 additions & 0 deletions lib/globals/preference_globals.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:open_media_server_app/helpers/preferences.dart';

class PreferenceGlobals {
static String BaseUrl = Preferences.prefs!.getString("BaseUrl")!;
}
File renamed without changes.
76 changes: 9 additions & 67 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:open_media_server_app/apis/auth_info_api.dart';
import 'package:open_media_server_app/auth/auth_globals.dart';
import 'package:open_media_server_app/auth/login_manager.dart';
import 'package:open_media_server_app/gallery.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/helpers/Preferences.dart';
import 'package:open_media_server_app/globals/auth_globals.dart';
import 'package:open_media_server_app/globals/platform_globals.dart';
import 'package:open_media_server_app/views/gallery.dart';
import 'package:open_media_server_app/globals/globals.dart';
import 'package:open_media_server_app/helpers/preferences.dart';
import 'package:open_media_server_app/views/login.dart';
import 'package:shared_preferences/shared_preferences.dart';

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
MediaKit.ensureInitialized();
Globals.isMobile = defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.android;

if (defaultTargetPlatform == TargetPlatform.android) {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
Globals.isTv =
androidInfo.systemFeatures.contains('android.software.leanback');
}

if (Globals.isTv) {
Globals.isMobile = false;
}

if (Globals.isTv && defaultTargetPlatform == TargetPlatform.android) {
Globals.isAndroidTv = true;
}

if (kIsWeb) {
Globals.isWeb = true;
}
await PlatformGlobals.setGlobals();

var prefs = await SharedPreferences.getInstance();
Preferences.prefs = prefs;
Expand All @@ -48,6 +27,7 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: (settings) {
// For authentication in web
if (settings.name?.contains("code") ?? false) {
AuthGlobals.appLoginCodeRoute = settings.name;
}
Expand All @@ -62,45 +42,7 @@ class MyApp extends StatelessWidget {
),
useMaterial3: true,
),
home: HomePage(title: Globals.Title),
);
}
}

class HomePage extends StatelessWidget {
const HomePage({super.key, required this.title});

final String title;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
surfaceTintColor: Colors.transparent,
title: Text(title),
),
body: FutureBuilder(
future: authenticate(context),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}

return const Gallery();
},
),
home: const LoginView(widget: Gallery()),
);
}

Future authenticate(BuildContext context) async {
AuthInfoApi authInfoApi = AuthInfoApi();
var info = await authInfoApi.getAuthInfo();

LoginManager loginManager = LoginManager(info);

var token = await loginManager.login(info, context);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:flutter/material.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/globals.dart';
import 'package:open_media_server_app/globals/preference_globals.dart';
import 'package:open_media_server_app/models/internal/grid_item_model.dart';
import 'package:open_media_server_app/player.dart';
import 'package:open_media_server_app/views/player.dart';
import 'package:open_media_server_app/widgets/custom_image.dart';

class EpisodeDetailView extends StatelessWidget {
Expand Down Expand Up @@ -71,7 +72,7 @@ class EpisodeDetailView extends StatelessWidget {
MaterialPageRoute(
builder: (context) => PlayerView(
url:
"${Globals.BaseUrl}/stream/${itemModel.inventoryItem?.category}/${itemModel.inventoryItem?.id}"),
"${PreferenceGlobals.BaseUrl}/stream/${itemModel.inventoryItem?.category}/${itemModel.inventoryItem?.id}"),
),
);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:flutter/material.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/globals.dart';
import 'package:open_media_server_app/globals/preference_globals.dart';
import 'package:open_media_server_app/models/internal/grid_item_model.dart';
import 'package:open_media_server_app/player.dart';
import 'package:open_media_server_app/views/player.dart';
import 'package:open_media_server_app/widgets/custom_image.dart';

class MovieDetailView extends StatelessWidget {
Expand Down Expand Up @@ -70,7 +71,7 @@ class MovieDetailView extends StatelessWidget {
MaterialPageRoute(
builder: (context) => PlayerView(
url:
"${Globals.BaseUrl}/stream/${itemModel.inventoryItem?.category}/${itemModel.inventoryItem?.id}"),
"${PreferenceGlobals.BaseUrl}/stream/${itemModel.inventoryItem?.category}/${itemModel.inventoryItem?.id}"),
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'package:flutter/material.dart';
import 'package:open_media_server_app/apis/base_api.dart';
import 'package:open_media_server_app/apis/inventory_api.dart';
import 'package:open_media_server_app/apis/metadata_api.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/globals.dart';
import 'package:open_media_server_app/models/internal/grid_item_model.dart';
import 'package:open_media_server_app/models/metadata/metadata_model.dart';
import 'package:open_media_server_app/views/episode_detail.dart';
import 'package:open_media_server_app/views/detail_views/episode_detail.dart';
import 'package:open_media_server_app/widgets/custom_image.dart';
import 'package:open_media_server_app/widgets/title.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'package:flutter/material.dart';
import 'package:open_media_server_app/apis/base_api.dart';
import 'package:open_media_server_app/apis/inventory_api.dart';
import 'package:open_media_server_app/apis/metadata_api.dart';
import 'package:open_media_server_app/globals.dart';
import 'package:open_media_server_app/globals/globals.dart';
import 'package:open_media_server_app/models/internal/grid_item_model.dart';
import 'package:open_media_server_app/models/metadata/metadata_model.dart';
import 'package:open_media_server_app/views/season_detail.dart';
import 'package:open_media_server_app/views/detail_views/season_detail.dart';
import 'package:open_media_server_app/widgets/custom_image.dart';
import 'package:open_media_server_app/widgets/title.dart';

Expand Down
Loading

0 comments on commit 3f3ce37

Please sign in to comment.