Skip to content

Instantly share code, notes, and snippets.

View adrianhall's full-sized avatar

Adrian Hall adrianhall

View GitHub Profile
@adrianhall
adrianhall / ComponentExtensions.kt
Created April 10, 2018 15:21
An extension function for implementing a text change watcher on EditText
fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
afterTextChanged.invoke(s.toString())
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { }
})
@adrianhall
adrianhall / ComponentExtensions.kt
Created April 10, 2018 15:27
Extension function for a validator
fun EditText.validate(validator: (String) -> Boolean, message: String) {
this.afterTextChanged {
this.error = if (validator(it)) null else message
}
this.error = if (validator(this.text.toString())) null else message
}
@adrianhall
adrianhall / AppSync-Example.yaml
Created April 13, 2018 16:01
An example CloudFormation template for AWS AppSync
---
Description: AWSAppSync DynamoDB Example
Resources:
GraphQLApi:
Type: "AWS::AppSync::GraphQLApi"
Properties:
Name: AWSAppSync DynamoDB Example
AuthenticationType: AWS_IAM
PostDynamoDBTableDataSource:
@adrianhall
adrianhall / AppSyncAPI.yaml
Last active September 25, 2024 01:43
A CloudFormation template for DynamoDB + Cognito User Pool + AppSync API for the Notes tutorial
---
Description: AWS AppSync Notes API
Parameters:
APIName:
Type: String
Description: Name of the API - used to generate unique names for resources
MinLength: 3
MaxLength: 20
AllowedPattern: '^[a-zA-Z][a-zA-Z0-9_]*$'
@adrianhall
adrianhall / CognitoSNSRole.yaml
Last active April 13, 2018 21:56
Some resources in CloudFormation for creating the Cognito SNS Role
SNSRole:
Type: AWS::IAM::Role
Description: "An IAM Role to allow Cognito to send SNS messages"
Properties:
RoleName: !Sub ${APIName}-cognito-sns-role
ManagedPolicyArns:
- Ref: CognitoSNSPolicy
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
@adrianhall
adrianhall / CognitoUserPool.yaml
Created April 13, 2018 22:02
Some resources to generate a CloudFormation template for Amazon Cognito user pools
UserPool:
Type: "AWS::Cognito::UserPool"
Description: "A Cognito user pool for authenticating users"
Properties:
UserPoolName: !Sub ${APIName}-user-pool
AutoVerifiedAttributes:
- phone_number
MfaConfiguration: "ON"
SmsConfiguration:
ExternalId: !Sub ${APIName}-external
@adrianhall
adrianhall / DynamoDBTable.yaml
Created April 13, 2018 22:16
CloudFormation resource template for a DynamoDB table
DynamoDBNotesTable:
Type: "AWS::DynamoDB::Table"
Description: "Data store for AWS AppSync Notes Type"
Properties:
TableName: !Sub ${APIName}-notes-table
AttributeDefinitions:
- AttributeName: "NoteId"
AttributeType: "S"
- AttributeName: "UserId"
AttributeType: "S"
@adrianhall
adrianhall / AppsyncDynamoDBRole.yaml
Created April 13, 2018 22:20
A CloudFormation template for the AppSync DynamoDB access role.
DynamoDBRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${APIName}-appsync-dynamodb-role
ManagedPolicyArns:
- Ref: AppSyncDynamoDBPolicy
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
@adrianhall
adrianhall / AppSyncAPI.yaml
Created April 13, 2018 22:23
CloudFormation template for AWS AppSync (partial)
AppSyncApi:
Type: "AWS::AppSync::GraphQLApi"
Description: "The GraphQL API for the Notes App"
Properties:
AuthenticationType: "AMAZON_COGNITO_USER_POOLS"
Name: !Sub ${APIName}
UserPoolConfig:
UserPoolId: !Ref UserPoolClient
AwsRegion: !Sub ${AWS::Region}
DefaultAction: "ALLOW"
@adrianhall
adrianhall / AppSyncResolvers.yaml
Created April 13, 2018 22:27
The resolver resources in CloudFormation format.
AppSyncAllNotesQueryResolver:
Type: "AWS::AppSync::Resolver"
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Query
FieldName: allNotes
DataSourceName: !GetAtt AppSyncNotesTableDataSource.Name
RequestMappingTemplate: |
{