From f79821182d7abfe1fc7a63c1dfc541e63b53fa01 Mon Sep 17 00:00:00 2001 From: nganhkhoa Date: Thu, 15 Apr 2021 15:52:54 +0700 Subject: [PATCH] working app --- .buckconfig | 6 + .editorconfig | 3 + .eslintrc.js | 4 + .flowconfig | 66 + .gitattributes | 3 + .gitignore | 59 + .prettierrc.js | 7 + .watchmanconfig | 1 + App.js | 161 + Farm.js | 18 + Pi.js | 49 + Utils.js | 21 + __tests__/App-test.js | 14 + android/app/BUCK | 55 + android/app/build.gradle | 222 + android/app/build_defs.bzl | 19 + android/app/debug.keystore | Bin 0 -> 2257 bytes android/app/proguard-rules.pro | 10 + android/app/src/debug/AndroidManifest.xml | 13 + .../ReactNativeFlipper.java | 72 + android/app/src/main/AndroidManifest.xml | 25 + .../com/piminerreactnative/MainActivity.java | 15 + .../piminerreactnative/MainApplication.java | 80 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3056 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5024 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2096 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2858 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4569 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7098 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6464 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10676 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9250 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15523 bytes android/app/src/main/res/values/strings.xml | 3 + android/app/src/main/res/values/styles.xml | 9 + android/build.gradle | 38 + android/gradle.properties | 28 + android/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 59203 bytes .../gradle/wrapper/gradle-wrapper.properties | 5 + android/gradlew | 185 + android/gradlew.bat | 89 + android/settings.gradle | 3 + app.json | 4 + babel.config.js | 3 + index.js | 9 + .../project.pbxproj | 709 ++ .../xcschemes/PiMinerReactNative.xcscheme | 88 + .../contents.xcworkspacedata | 10 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + ios/PiMinerReactNative/AppDelegate.h | 8 + ios/PiMinerReactNative/AppDelegate.m | 62 + .../AppIcon.appiconset/Contents.json | 38 + .../Images.xcassets/Contents.json | 6 + ios/PiMinerReactNative/Info.plist | 55 + .../LaunchScreen.storyboard | 47 + ios/PiMinerReactNative/main.m | 9 + ios/PiMinerReactNativeTests/Info.plist | 24 + .../PiMinerReactNativeTests.m | 65 + ios/Podfile | 29 + ios/Podfile.lock | 497 ++ ios/bcell_config.json | 11 + metro.config.js | 17 + package.json | 32 + views/LoginView.js | 35 + views/StatusView.js | 57 + yarn.lock | 6819 +++++++++++++++++ 66 files changed, 9925 insertions(+) create mode 100644 .buckconfig create mode 100644 .editorconfig create mode 100644 .eslintrc.js create mode 100644 .flowconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .prettierrc.js create mode 100644 .watchmanconfig create mode 100644 App.js create mode 100644 Farm.js create mode 100644 Pi.js create mode 100644 Utils.js create mode 100644 __tests__/App-test.js create mode 100644 android/app/BUCK create mode 100644 android/app/build.gradle create mode 100644 android/app/build_defs.bzl create mode 100644 android/app/debug.keystore create mode 100644 android/app/proguard-rules.pro create mode 100644 android/app/src/debug/AndroidManifest.xml create mode 100644 android/app/src/debug/java/com/piminerreactnative/ReactNativeFlipper.java create mode 100644 android/app/src/main/AndroidManifest.xml create mode 100644 android/app/src/main/java/com/piminerreactnative/MainActivity.java create mode 100644 android/app/src/main/java/com/piminerreactnative/MainApplication.java create mode 100644 android/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 android/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 android/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 android/app/src/main/res/values/strings.xml create mode 100644 android/app/src/main/res/values/styles.xml create mode 100644 android/build.gradle create mode 100644 android/gradle.properties create mode 100644 android/gradle/wrapper/gradle-wrapper.jar create mode 100644 android/gradle/wrapper/gradle-wrapper.properties create mode 100755 android/gradlew create mode 100644 android/gradlew.bat create mode 100644 android/settings.gradle create mode 100644 app.json create mode 100644 babel.config.js create mode 100644 index.js create mode 100644 ios/PiMinerReactNative.xcodeproj/project.pbxproj create mode 100644 ios/PiMinerReactNative.xcodeproj/xcshareddata/xcschemes/PiMinerReactNative.xcscheme create mode 100644 ios/PiMinerReactNative.xcworkspace/contents.xcworkspacedata create mode 100644 ios/PiMinerReactNative.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 ios/PiMinerReactNative/AppDelegate.h create mode 100644 ios/PiMinerReactNative/AppDelegate.m create mode 100644 ios/PiMinerReactNative/Images.xcassets/AppIcon.appiconset/Contents.json create mode 100644 ios/PiMinerReactNative/Images.xcassets/Contents.json create mode 100644 ios/PiMinerReactNative/Info.plist create mode 100644 ios/PiMinerReactNative/LaunchScreen.storyboard create mode 100644 ios/PiMinerReactNative/main.m create mode 100644 ios/PiMinerReactNativeTests/Info.plist create mode 100644 ios/PiMinerReactNativeTests/PiMinerReactNativeTests.m create mode 100644 ios/Podfile create mode 100644 ios/Podfile.lock create mode 100644 ios/bcell_config.json create mode 100644 metro.config.js create mode 100644 package.json create mode 100644 views/LoginView.js create mode 100644 views/StatusView.js create mode 100644 yarn.lock diff --git a/.buckconfig b/.buckconfig new file mode 100644 index 0000000..934256c --- /dev/null +++ b/.buckconfig @@ -0,0 +1,6 @@ + +[android] + target = Google Inc.:Google APIs:23 + +[maven_repositories] + central = https://repo1.maven.org/maven2 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7c28613 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,3 @@ +# Windows files +[*.bat] +end_of_line = crlf diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..40c6dcd --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,4 @@ +module.exports = { + root: true, + extends: '@react-native-community', +}; diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 0000000..315f274 --- /dev/null +++ b/.flowconfig @@ -0,0 +1,66 @@ +[ignore] +; We fork some components by platform +.*/*[.]android.js + +; Ignore "BUCK" generated dirs +/\.buckd/ + +; Ignore polyfills +node_modules/react-native/Libraries/polyfills/.* + +; Flow doesn't support platforms +.*/Libraries/Utilities/LoadingView.js + +[untyped] +.*/node_modules/@react-native-community/cli/.*/.* + +[include] + +[libs] +node_modules/react-native/interface.js +node_modules/react-native/flow/ + +[options] +emoji=true + +esproposal.optional_chaining=enable +esproposal.nullish_coalescing=enable + +exact_by_default=true + +module.file_ext=.js +module.file_ext=.json +module.file_ext=.ios.js + +munge_underscores=true + +module.name_mapper='^react-native/\(.*\)$' -> '/node_modules/react-native/\1' +module.name_mapper='^@?[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '/node_modules/react-native/Libraries/Image/RelativeImageStub' + +suppress_type=$FlowIssue +suppress_type=$FlowFixMe +suppress_type=$FlowFixMeProps +suppress_type=$FlowFixMeState + +[lints] +sketchy-null-number=warn +sketchy-null-mixed=warn +sketchy-number=warn +untyped-type-import=warn +nonstrict-import=warn +deprecated-type=warn +unsafe-getters-setters=warn +unnecessary-invariant=warn +signature-verification-failure=warn + +[strict] +deprecated-type +nonstrict-import +sketchy-null +unclear-type +unsafe-getters-setters +untyped-import +untyped-type-import + +[version] +^0.137.0 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..45a3dcb --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +# Windows files should use crlf line endings +# https://help.github.com/articles/dealing-with-line-endings/ +*.bat text eol=crlf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad572e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,59 @@ +# OSX +# +.DS_Store + +# Xcode +# +build/ +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata +*.xccheckout +*.moved-aside +DerivedData +*.hmap +*.ipa +*.xcuserstate + +# Android/IntelliJ +# +build/ +.idea +.gradle +local.properties +*.iml + +# node.js +# +node_modules/ +npm-debug.log +yarn-error.log + +# BUCK +buck-out/ +\.buckd/ +*.keystore +!debug.keystore + +# fastlane +# +# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the +# screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://docs.fastlane.tools/best-practices/source-control/ + +*/fastlane/report.xml +*/fastlane/Preview.html +*/fastlane/screenshots + +# Bundle artifact +*.jsbundle + +# CocoaPods +/ios/Pods/ diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..84196d9 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,7 @@ +module.exports = { + bracketSpacing: false, + jsxBracketSameLine: true, + singleQuote: true, + trailingComma: 'all', + arrowParens: 'avoid', +}; diff --git a/.watchmanconfig b/.watchmanconfig new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.watchmanconfig @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/App.js b/App.js new file mode 100644 index 0000000..ac55a32 --- /dev/null +++ b/App.js @@ -0,0 +1,161 @@ +import React, { useEffect, useState } from 'react'; +import type {Node} from 'react'; +import { + SafeAreaView, + Text, + TextInput, + View, + Button, +} from 'react-native'; + +import axios from 'axios'; + +import AsyncStorage from '@react-native-async-storage/async-storage'; + +import LoginView from "./views/LoginView"; +import StatusView, { appState } from "./views/StatusView"; + +import { decryptor } from "./Utils"; +import pi from "./Pi"; +import farm from "./Farm"; + +const App: () => Node = () => { + const [errorMessage, setErrorMessage] = useState(""); + const [username, setUsername] = useState(""); + const [userInfo, setUserInfo] = useState({ + username: "", + subscribers: [], + }); + + const [activeCount, setActiveCount] = useState(0); + const [totalCount, setTotalCount] = useState(0); + + // ["loginScreen", "statusFetch", "waiting", "waking", "fullyAwake"] + const [appStatus, setAppStatus] = useState(appState.default); + + const fetchUserData = async () => { + const userInfo = await farm.GetUserInfo(username); + const statusChecker = userInfo.subscribers.map(sub => { + return pi.StatusChecker(sub); + }); + const botStatuses = await Promise.all(statusChecker); + const botActive = botStatuses.reduce((acc, s) => s ? acc + 1 : acc, 0); + + setUserInfo(userInfo); + setTotalCount(botStatuses.length); + setActiveCount(botActive); + + return botActive == botStatuses.length; + }; + + const wakeup = () => { + setAppStatus(appState.waking) + const waker = userInfo.subscribers.map(sub => { + return pi.Waker(sub); + }); + Promise.all(waker) + .then((botStatuses) => { + setErrorMessage("") + const botActive = botStatuses.reduce((acc, s) => s ? acc + 1 : acc, 0) + setActiveCount(botActive); + if (botActive == botStatuses.length) { + setAppStatus(appState.fullyAwake) + } else { + setAppStatus(appState.waiting) + } + }) + .catch((err) => { + setErrorMessage("Cannot Wake Up") + }) + } + + const login = () => { + setAppStatus(appState.statusFetch) + fetchUserData() + .then((awaken) => { + setErrorMessage("") + if (awaken) { + setAppStatus(appState.fullyAwake) + } else { + setAppStatus(appState.waiting) + } + AsyncStorage.setItem("username", username) + .catch((_) => {}) + }) + .catch((err) => { + setErrorMessage("Cannot fetch user information") + }) + } + + const logout = () => { + setUsername(""); + setUserInfo({ + username: "", + subscribers: [], + }); + setAppStatus(appState.loginScreen) + AsyncStorage.removeItem("username", null); + } + + const tryGetUser = () => { + AsyncStorage.getItem("username") + .then((localUser) => { + if (localUser !== null) { + setUsername(localUser); + } + }) + .catch((_) => {}); + } + + useEffect(tryGetUser, []); + + return ( + + + { + (appStatus == appState.loginScreen) + ? + + + : + +