How to sign executable for sandbox

Issue #401 Find identity security find-identity Sign with entitlements and identity. For macOS, use 3rd Party Mac Developer Application codesign -f -s "3rd Party Mac Developer Application: Khoa Pham (123DK123F2)" --entitlements "MyApp.entitlements" "tool/mytool" To enable harden runtime codesign --verbose --force --deep -o runtime --sign

September 5, 2019 · 1 min · 44 words · Khoa

How to make simple traffic simulation in Javascript

Issue #400 Use prototype.js for inheritance Use enchant.js for 2d game logic Code https://github.com/onmyway133/traffic_simulator

September 5, 2019 · 1 min · 14 words · Khoa

How to make collaborative drawing canvas with socketio and node

Issue #399 Client Use https://github.com/facebook/create-react-app App.js import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; import Main from './Main' class App extends Component { render() { return <Main /> } } export default App; Main.js // @flow import React from 'react'; import PropTypes from 'prop-types'; import { withStyles } from '@material-ui/core/styles'; import AppBar from '@material-ui/core/AppBar'; import Toolbar from '@material-ui/core/Toolbar'; import Typography from '@material-ui/core/Typography'; import Button from '@material-ui/core/Button'; import IconButton from '@material-ui/core/IconButton'; import MenuIcon from '@material-ui/icons/Menu'; import Manager from '....

September 5, 2019 · 2 min · 334 words · Khoa

How to generate changelog for GitHub releases with rxjs and node

Issue #398 How to Use https://github.com/onmyway133/github-changelogs-maker Generate personal access token https://github.com/settings/tokens Technical Dependencies const Rx = require('rxjs/Rx') const Fetch = require('node-fetch') const Minimist = require('minimist') const Fs = require('fs') Use GraphQL makeOptions(query, token) { return { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `bearer ${token}` }, body: JSON.stringify({ query: ` query { repository(owner: "${this.owner}", name: "${this.repo}") { ${query} } } ` }) } } Use orderBy fetchPRsAndIssues(dates) { const query = ` pullRequests(last: 100, orderBy: {field: UPDATED_AT, direction: ASC}) { edges { node { title merged mergedAt url author { login url } } } } issues(last: 100, orderBy: {field: UPDATED_AT, direction: ASC}) { edges { node { title closed updatedAt url } } } } }

September 5, 2019 · 1 min · 117 words · Khoa

How to do launch screen in Android

Issue #397 We recommend that, rather than disabling the preview window, you follow the common Material Design patterns. You can use the activity’s windowBackground theme attribute to provide a simple custom drawable for the starting activity. styles.xml <style name="LaunchTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowBackground">@drawable/launch_background</item> </style> Set android:theme="@style/LaunchTheme" to activity element AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.onmyway133.whatsupintech"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".features.main.MainActivity" android:label="@string/app_name" android:theme="@style/LaunchTheme"> <intent-filter> <action android:name="android....

September 4, 2019 · 1 min · 106 words · Khoa

How to add header to NavigationView in Android

Issue #396 Use app:headerLayout <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/navigationView" android:fitsSystemWindows="true" android:layout_gravity="start" app:menu="@menu/drawer_menu" app:itemIconTint="@color/title" app:itemTextColor="@color/title" app:headerLayout="@layout/navigation_header" /> navigation_header.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.appcompat.widget.AppCompatImageView android:layout_width="0dp" android:layout_height="wrap_content" android:src="@drawable/icon" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>

September 4, 2019 · 1 min · 33 words · Khoa

How to do simple analytics in iOS

Issue #395 Prefer static enum to avoid repetition and error. The Log should have methods with all required fields so the call site is as simple as possible. How to format and assign parameters is encapsulated in this Analytics. import Foundation import Firebase import FirebaseAnalytics struct Analytics { enum Parameter: String { case studentId = "student_id" case classId = "class_id" case url = "url" } enum Property: String { case grantLocation = "grant_location" } enum Name: String { case login case logOut = "log_out" case enroll } struct Log { private func log(_ name: Name, parameters: [Parameter: String] = [:]) { let mapped: [String: String] = Dictionary(uniqueKeysWithValues: parameters....

September 4, 2019 · 1 min · 198 words · Khoa

Back to static site

Issue #394 It’s been a while since I wrote Hello world, again, the ease of GitHub issue indeed motivates me to write more. In the mean time I also wrote on https://medium.com/@onmyway133 and https://dev.to/onmyway133 and got some traction. Then I started using GitHub pages again, with Jekyll and remote theme, it works great. But then I needed to manually link the GitHub issues to my page, that’s just labor work....

September 3, 2019 · 1 min · 169 words · Khoa

How to get all GitHub issues using GraphQL

Issue #393 https://developer.github.com/v4/explorer/ query { repository(owner: "onmyway133", name: "blog") { issues(orderBy: {field: UPDATED_AT, direction: ASC}, last: 100) { edges { cursor node { title createdAt updatedAt labels(first: 10) { edges { node { name } } } } } } } } In node.js const GraphQL = require('graphql-request') const GraphQLClient = GraphQL.GraphQLClient const client = new GraphQLClient('https://api.github.com/graphql', { headers: { Authorization: 'Bearer 123456730712334152e6e1232c53987654312', }, }) const query = `{ repository(owner: "onmyway133", name: "blog") { issues(first: 100) { edges { node { title createdAt updatedAt labels(first: 10) { edges { node { name } } } } } } } }` client....

September 3, 2019 · 1 min · 115 words · Khoa

How to use Hexo to deploy static site

Issue #392 It’s been a long journey since https://github.com/onmyway133/blog/issues/1, next step is to keep GitHub issues as source, and mirror those to a static site. Use 2 repos https://github.com/onmyway133/web for source https://github.com/onmyway133/onmyway133.github.io for generated content npm install -g hexo-cli echo $PATH=$PATH:/Users/khoa/.nodenv/versions/10.15.2/bin/hexo hexo init blog npm install hexo-deployer-git --save Update _config.yml deploy: type: git repo: https://github.com/onmyway133/onmyway133.github.io.git branch: master hexo clean hexo deploy Read more https://hexo.io/docs/deployment.html https://gist.github.com/btfak/18938572f5df000ebe06fbd1872e4e39

September 3, 2019 · 1 min · 65 words · Khoa

How to use type coersion in Javascript

Issue #391 People who make fun of Javascript probably don’t understand implicit type coersion and when to use triple equal. Javascript is very unexpected, but when we work with this language, we need to be aware. Coercion–Automatically changing a value from one type to another. If x is Number and y is String, return x == ToNumber(y) If x is String or Number and y is Object, return x == ToPrimitive(y) Empty array becomes empty string Read more https://codeburst....

September 3, 2019 · 1 min · 81 words · Khoa

How to safely access deeply nested object in Javascript

Issue #390 An object ’s property can be null or undefined. Accessing step by step is tedious props.user && props.user.posts && props.user.posts[0] && props.user.posts[0].comments Dynamic parsing path is too clever and involves string in the end, which is a no no const get = (p, o) => p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o) const getUserComments = get(['user', 'posts', 0, 'comments']) Instead let’s use function and catch errors explicitly, and defaults with a fallback...

September 3, 2019 · 1 min · 120 words · Khoa

How to use flow type in Javascript

Issue #389 Prefer flow over TypeScript for simplicity Javascript primitive types number and string are too general and do not express the domain objects. Because lack of type alias in Javascript, we can use flow export type Centimeter = number export type Color = string export type ImageSource = number export type Kilogram = number export type Kilocalorie = number // 1 cal = 4.1840 J export type Second = number export type SecondsSince1970 = number export type Minute = number export type DateString = string // 2018-11-20 export type DateTimeString = string // 2018-11-20T00:00:00 export type YearWeekString = string // 201838 export type FunctionWithVoid = () => void export type FunctionWithString = (string) => void export type FunctionWithBoolean = (boolean) => void export type FunctionWithNumber = (number) => void export type JSONObject = any export type JSONString = string export type StringToString = any export type Degree = number export type Radian = number export type Animal = 'cat' | 'dog' | 'cow'

September 3, 2019 · 1 min · 164 words · Khoa

How to use flow in Kotlin

Issue #388 Asynchronous Flow https://kotlinlang.org/docs/reference/coroutines/flow.html Using List result type we can only return all the values at once. To represent the stream of values that are being asynchronously computed we can use Flow type similarly to the Sequence type for synchronously computed values:

September 3, 2019 · 1 min · 43 words · Khoa

How to choose Firebase vs Google Analytics

Issue #387 Google Analytics is shutting down. From Firebase Analytics console, we can choose to upgrade to Google Analytics, no code change is needed. https://support.google.com/firebase/answer/9167112?hl=en In October 2019, we will start to sunset Google Analytics mobile-apps reporting based on the Google Analytics Services SDKs for Android and iOS. https://firebase.googleblog.com/2019/07/firebase-google-analytics-upgrade.html Thanks to our continued partnership with Google Analytics, you can now upgrade your Firebase projects to the next generation of app analytics!...

September 3, 2019 · 1 min · 139 words · Khoa

How to use lessThan and greaterThan in Auto Layout in iOS

Issue #386 When it comes to right and bottom side, we should use negative values, and use lessThan, as it means less than a negative value

September 3, 2019 · 1 min · 26 words · Khoa

How to check app running on jailbreak iOS device

Issue #385 From https://github.com/OneSignal/OneSignal-iOS-SDK/blob/master/iOS_SDK/OneSignalSDK/Source/OneSignalJailbreakDetection.m + (BOOL)isJailbroken { #if !(TARGET_IPHONE_SIMULATOR) FILE *file = fopen("/Applications/Cydia.app", "r"); if (file) { fclose(file); return YES; } file = fopen("/Library/MobileSubstrate/MobileSubstrate.dylib", "r"); if (file) { fclose(file); return YES; } file = fopen("/bin/bash", "r"); if (file) { fclose(file); return YES; } file = fopen("/usr/sbin/sshd", "r"); if (file) { fclose(file); return YES; } file = fopen("/etc/apt", "r"); if (file) { fclose(file); return YES; } file = fopen("/usr/bin/ssh", "r"); if (file) { fclose(file); return YES; } NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:@"/Applications/Cydia....

September 2, 2019 · 1 min · 194 words · Khoa

How to create bounce animation programmatically in Android

Issue #383 Right click res -> New -> Android Resource Directory, select anim and name it anim Right click res/anim -> New -> Android Resource file, name it bounce <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="-100" android:repeatCount="infinite" /> </set> We have to set repeatCount in xml, setting in code does not work !! val bounce = AnimationUtils.loadAnimation(context, R.anim.bounce) bounce.repeatMode = Animation.REVERSE bounce.duration = (1000..2000).random().toLong() imageView.startAnimation(bounce)

August 28, 2019 · 1 min · 66 words · Khoa

How to use point in dp programmatically in Android

Issue #382 import android.content.Context fun Int.toDp(context: Context?): Int { if (context != null) { val scale = context.resources.displayMetrics.density return (this.toFloat() * scale + 0.5f).toInt() } else { return 0 } } val set = ConstraintSet() set.setMargin(imageView.id, ConstraintSet.RIGHT, rightMargin.toDp(150)) Read more https://stackoverflow.com/questions/5255184/android-and-setting-width-and-height-programmatically-in-dp-units

August 28, 2019 · 1 min · 41 words · Khoa

How to create constraints programmatically with ConstraintLayout in Android

Issue #381 From API < 17, there is ViewCompat.generateViewId() For API 17, there is View.generateViewId() Note that to use ConstraintSet, all views under ConstraintLayout inside xml must have unique id val imageView = ImageView(context) imageView.id = View.generateViewId() imageView.setImageResource(resId) constraintLayout.addView(imageView) val set = ConstraintSet() set.clone(constraintLayout) set.connect(imageView.id, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT) set.applyTo(constraintLayout)

August 28, 2019 · 1 min · 49 words · Khoa