Skip to content

Commit

Permalink
Merge pull request #178 from humhub/f-22-installation-non-detected
Browse files Browse the repository at this point in the history
Mobile app to display the path to the manifest instead of the start_url from the manifest.
  • Loading branch information
luke- committed May 6, 2024
2 parents 89eeb74 + 4633e13 commit 5c0c02d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
28 changes: 12 additions & 16 deletions lib/models/hum_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ enum RedirectAction { opener, webView }

class HumHub {
Manifest? manifest;
String? manifestUrl;
bool isHideOpener;
String? randomHash;
String? appVersion;
String? pushToken;

HumHub({
this.manifest,
this.isHideOpener = false,
this.randomHash,
this.appVersion,
this.pushToken
});
HumHub(
{this.manifest, this.manifestUrl, this.isHideOpener = false, this.randomHash, this.appVersion, this.pushToken});

Map<String, dynamic> toJson() => {
'manifest': manifest?.toJson(),
'manifestUri': manifestUrl,
'isHideDialog': isHideOpener,
'randomHash': randomHash,
'appVersion': appVersion,
Expand All @@ -31,8 +28,8 @@ class HumHub {

factory HumHub.fromJson(Map<String, dynamic> json) {
return HumHub(
manifest:
json['manifest'] != null ? Manifest.fromJson(json['manifest']) : null,
manifest: json['manifest'] != null ? Manifest.fromJson(json['manifest']) : null,
manifestUrl: json['manifestUri'],
isHideOpener: json['isHideDialog'] as bool,
randomHash: json['randomHash'],
appVersion: json['appVersion'],
Expand Down Expand Up @@ -61,13 +58,12 @@ class HumHub {
static String generateHash(int length) {
final random = Random.secure();
const characters = '0123456789abcdef';
return List.generate(
length, (_) => characters[random.nextInt(characters.length)]).join();
return List.generate(length, (_) => characters[random.nextInt(characters.length)]).join();
}

Map<String, String> get customHeaders =>{
'x-humhub-app-token': randomHash!,
'x-humhub-app': appVersion ?? '1.0.0',
'x-humhub-app-ostate': isHideOpener ? '1' : '0'
};
Map<String, String> get customHeaders => {
'x-humhub-app-token': randomHash!,
'x-humhub-app': appVersion ?? '1.0.0',
'x-humhub-app-ostate': isHideOpener ? '1' : '0'
};
}
10 changes: 10 additions & 0 deletions lib/models/manifest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class Manifest {
return Manifest.fromJson(res.data);
};

static String getUriWithoutExtension(String url) {
int lastSlashIndex = url.lastIndexOf('/');
// If there is no slash or only one character after the last slash, return the original URL
if (lastSlashIndex < 0 || lastSlashIndex == url.length - 1) {
return url;
}
// Remove everything after the last slash, including the slash itself
return url.substring(0, lastSlashIndex);
}

static String defineUrl(String url, {bool isUriPretty = true}) {
return !isUriPretty ? '$url/index.php?r=web%2Fpwa-manifest%2Findex' : '$url/manifest.json';
}
Expand Down
2 changes: 2 additions & 0 deletions lib/util/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extension MyWebViewController on InAppWebViewController {
final exitConfirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
title: Text(AppLocalizations.of(context)!.web_view_exit_popup_title),
content: Text(AppLocalizations.of(context)!.web_view_exit_popup_content),
actions: <Widget>[
Expand Down
15 changes: 8 additions & 7 deletions lib/util/opener_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class OpenerController {
/// obtained from the manifest data.
///
/// @throws Exception if an error occurs during the search process.
Future<bool> findManifest(String url) async {
Future<String?> findManifest(String url) async {
List<String> possibleUrls = generatePossibleManifestsUrls(url);
String? manifestUrl;
for (var url in possibleUrls) {
asyncData = await APIProvider.of(ref).request(Manifest.get(url));
manifestUrl = Manifest.getUriWithoutExtension(url);
if (!asyncData!.hasError) break;
}
return asyncData!.hasError;
return manifestUrl;
}

checkHumHubModuleView(String url) async {
Expand All @@ -68,16 +70,15 @@ class OpenerController {
return;
}
// Get the manifest.json for given url.
await findManifest(helper.model[formUrlKey]!);
logDebug("Here");
if (asyncData!.hasValue) {
String? manifestUrl = await findManifest(helper.model[formUrlKey]!);
if (asyncData!.hasValue && manifestUrl != null) {
await checkHumHubModuleView(asyncData!.value!.startUrl);
}
// If manifest.json does not exist the url is incorrect.
// This is a temp. fix the validator expect sync function this is established workaround.
// In the future we could define our own TextFormField that would also validate the API responses.
// But it this is not acceptable I can suggest simple popup or tempPopup.
if (asyncData!.hasError || !doesViewExist) {
if (asyncData!.hasError || !doesViewExist || manifestUrl == null) {
logError("Open URL error: $asyncData");
String value = urlTextController.text;
urlTextController.text = error404;
Expand All @@ -92,7 +93,7 @@ class OpenerController {
String currentUrl = urlTextController.text;
String hash = HumHub.generateHash(32);
if (lastUrl == currentUrl) hash = ref.read(humHubProvider).randomHash ?? hash;
await ref.read(humHubProvider).setInstance(HumHub(manifest: manifest, randomHash: hash));
await ref.read(humHubProvider).setInstance(HumHub(manifest: manifest, randomHash: hash, manifestUrl: manifestUrl));
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/util/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class HumHubNotifier extends ChangeNotifier {
_humHubInstance.isHideOpener = instance.isHideOpener;
_humHubInstance.randomHash = instance.randomHash;
_humHubInstance.appVersion = packageInfo.version;
_humHubInstance.manifestUrl = instance.manifestUrl;
_updateSafeStorage();
notifyListeners();
}
Expand All @@ -60,7 +61,7 @@ class HumHubNotifier extends ChangeNotifier {

_updateSafeStorage() async {
final jsonString = json.encode(_humHubInstance.toJson());
String lastUrl = _humHubInstance.manifest != null ? _humHubInstance.manifest!.startUrl : await getLastUrl();
String lastUrl = _humHubInstance.manifestUrl != null ? _humHubInstance.manifestUrl! : await getLastUrl();
await _storage.write(key: StorageKeys.humhubInstance, value: jsonString);
await _storage.write(key: StorageKeys.lastInstanceUrl, value: lastUrl);
}
Expand Down
14 changes: 8 additions & 6 deletions lib/util/universal_opener_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class UniversalOpenerController {

UniversalOpenerController({required this.url});

Future<bool> findManifest(String url) async {
Future<String?> findManifest(String url) async {
List<String> possibleUrls = generatePossibleManifestsUrls(url);
String? manifestUrl;
for (var url in possibleUrls) {
asyncData = await APIProvider.requestBasic(Manifest.get(url));
manifestUrl = url;
if (!asyncData!.hasError) break;
}
return asyncData!.hasError;
return manifestUrl;
}

static List<String> generatePossibleManifestsUrls(String url) {
Expand Down Expand Up @@ -53,17 +55,17 @@ class UniversalOpenerController {
asyncData = null;
return null;
}
await findManifest(url);
if (asyncData!.hasValue) {
String? manifestUrl = await findManifest(url);
if (asyncData!.hasValue && manifestUrl != null) {
await checkHumHubModuleView(asyncData!.value!.startUrl);
}
if (asyncData!.hasError || !doesViewExist) {
if (asyncData!.hasError || !doesViewExist || manifestUrl == null) {
asyncData = null;
return null;
} else {
Manifest manifest = asyncData!.value!;
String hash = HumHub.generateHash(32);
HumHub instance = HumHub(manifest: manifest, randomHash: hash);
HumHub instance = HumHub(manifest: manifest, randomHash: hash, manifestUrl: manifestUrl);
humhub = instance;
return instance;
}
Expand Down

0 comments on commit 5c0c02d

Please sign in to comment.