Skip to content

Commit

Permalink
Add torrent downloading to user selected location
Browse files Browse the repository at this point in the history
add settings page
  • Loading branch information
Leapward-Koex committed Apr 28, 2022
1 parent 875c821 commit 96a5e9b
Show file tree
Hide file tree
Showing 18 changed files with 889 additions and 165 deletions.
114 changes: 114 additions & 0 deletions HelperFunctions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {Platform, PermissionsAndroid} from 'react-native';
import {NativeModules} from 'react-native';

export async function promiseEach<T>(
promiseArray: Promise<T>[],
thenCallback: (item: T) => any,
) {
for (const item of promiseArray) {
item.then(data => thenCallback(data));
}
}

export const weekday = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];

export function getDayOfWeek(dateString: string) {
const date = new Date(dateString);
return weekday[date.getDay()];
}

export function humanFileSize(bytes: number, si = false, dp = 1) {
const thresh = si ? 1000 : 1024;

if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}

const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10 ** dp;

do {
bytes /= thresh;
++u;
} while (
Math.round(Math.abs(bytes) * r) / r >= thresh &&
u < units.length - 1
);

return bytes.toFixed(dp) + ' ' + units[u];
}

interface FilePathModuleInterface {
getFolderPathFromUri(
contentUri: string,
callback: (directoryPath: string) => void,
): void;
verifyManageFilesPermission(callback: (granted: boolean) => void): void;
}

const {FilePathModule} = NativeModules;
const FilePathModuleTyped = FilePathModule as FilePathModuleInterface;
export const getRealPathFromContentUri = (contentUri: string) => {
return new Promise<string>((resolve, reject) => {
FilePathModuleTyped.getFolderPathFromUri(contentUri, directoryPath => {
if (directoryPath) {
resolve(directoryPath);
} else {
reject(`Content Uri ${contentUri} could not be mapped to directory`);
}
});
});
};

export async function requestStoragePermission() {
if (Platform.OS !== 'android') {
return true;
}

const checkManageFilesPermission = () => {
return new Promise<boolean>(resolve => {
FilePathModuleTyped.verifyManageFilesPermission(resolve);
});
};

const pm0 = await checkManageFilesPermission();
if (!pm0) {
return false;
}

const pm1 = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
);
const pm2 = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
);

if (pm1 && pm2) {
return true;
}

const userResponse = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
]);

if (
userResponse['android.permission.READ_EXTERNAL_STORAGE'] === 'granted' &&
userResponse['android.permission.WRITE_EXTERNAL_STORAGE'] === 'granted'
) {
return true;
} else {
return false;
}
}
47 changes: 0 additions & 47 deletions HelperFunctions/index.tsx

This file was deleted.

5 changes: 3 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
implementation "androidx.activity:activity:1.4.0"
implementation "androidx.fragment:fragment:1.4.1"
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation project(':react-native-fs')
implementation project(':react-native-svg')

Expand Down
12 changes: 9 additions & 3 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yorha">
xmlns:tools="http://schemas.android.com/tools"
package="com.yorha">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

<application
android:name=".MainApplication"
Expand All @@ -16,8 +21,9 @@
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
Loading

0 comments on commit 96a5e9b

Please sign in to comment.