Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
chore(Misc): Add examples/hacker_news_pwa :D
Browse files Browse the repository at this point in the history
For the very curious, this is something we've been iterating on internally for
our own reference. This is not hyper optimized, or used for anything other than
an "example" application to generate some reference numbers.

PiperOrigin-RevId: 185884499
  • Loading branch information
matanlurey committed Feb 16, 2018
1 parent 4b4f444 commit b73eb72
Show file tree
Hide file tree
Showing 33 changed files with 641 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/hacker_news_pwa/.mono_repo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# See https://github.com/dart-lang/mono_repo for details
dart:
- dev

stages:
- analyze:
- dartanalyzer: --fatal-warnings web
- build:
- command: pub run build_runner build --config=release --fail-on-severe
21 changes: 21 additions & 0 deletions examples/hacker_news_pwa/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Google Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions examples/hacker_news_pwa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# AngularDart Hacker News PWA

## Service Worker

### Updating Cached Assets

Run the following command to update the list of assets the service worker will
cache to be accessible offline.

```shell
$ pub run pwa --exclude "packages/**,*.ng_placeholder"
```
11 changes: 11 additions & 0 deletions examples/hacker_news_pwa/build.release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
targets:
$default:
builders:
build_web_compilers|entrypoint:
generate_for:
- web/**.dart
options:
compiler: dart2js
dart2js_args:
- --trust-type-annotations
- --trust-primitives
21 changes: 21 additions & 0 deletions examples/hacker_news_pwa/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"hosting": {
"public": "build/web",
"ignore": [
"*.ng_placeholder",
"packages/"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "/",
"headers": [{"key": "Link", "value": "</main.dart.js>;rel=preload;as=script"}]
}
]
}
}
45 changes: 45 additions & 0 deletions examples/hacker_news_pwa/lib/app_component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
a {
align-items: center;
color: #bbb;
display: flex;
padding: 0 8px;
text-decoration: none;
}

a:hover {
color: #fff;
}

header {
background: #212121;
box-shadow: 0 4px 8px rgba(0, 0, 0, .2);
display: flex;
height: 56px;
position: fixed;
top: 0;
width: 100%;
}

img {
width: 40px;
}

main {
margin: 56px auto 0 auto;
max-width: 800px;
}

nav {
display: flex;
height: 100%;
margin: 0 auto;
max-width: 800px;
width: 100%;
}

.active-route {
border-bottom: 2px solid #fff;
/* This keeps content vertically centered. */
border-top: 2px solid transparent;
color: #fff;
}
52 changes: 52 additions & 0 deletions examples/hacker_news_pwa/lib/app_component.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Ignore generated templates imported for route definitions.
// ignore_for_file: uri_has_not_been_generated

import 'package:angular/angular.dart';
import 'package:angular_router/angular_router.dart';

import 'src/feed_component.template.dart' as feed;
import 'src/item_detail_component.template.dart' deferred as item_detail;
import 'src/routes.dart';

@Component(
selector: 'app',
templateUrl: 'app_component.html',
styleUrls: const ['app_component.css'],
directives: const [routerDirectives],
)
class AppComponent {
final newsUrl = newsRoutePath.toUrl();
final newUrl = newRoutePath.toUrl();
final showUrl = showRoutePath.toUrl();
final askUrl = askRoutePath.toUrl();
final jobsUrl = jobsRoutePath.toUrl();
final routes = [
new RouteDefinition(
routePath: newsRoutePath,
component: feed.FeedComponentNgFactory,
),
new RouteDefinition(
routePath: newRoutePath,
component: feed.FeedComponentNgFactory,
),
new RouteDefinition(
routePath: showRoutePath,
component: feed.FeedComponentNgFactory,
),
new RouteDefinition(
routePath: askRoutePath,
component: feed.FeedComponentNgFactory,
),
new RouteDefinition(
routePath: jobsRoutePath,
component: feed.FeedComponentNgFactory,
),
new RouteDefinition.defer(
routePath: itemRoutePath,
loader: () async {
await item_detail.loadLibrary();
return item_detail.ItemDetailComponentNgFactory;
},
),
];
}
15 changes: 15 additions & 0 deletions examples/hacker_news_pwa/lib/app_component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<header>
<nav>
<a href="https://webdev.dartlang.org/angular">
<img src="/images/logo.svg" alt="AngularDart logo">
</a>
<a [routerLink]="newsUrl" routerLinkActive="active-route">top</a>
<a [routerLink]="newUrl" routerLinkActive="active-route">new</a>
<a [routerLink]="showUrl" routerLinkActive="active-route">show</a>
<a [routerLink]="askUrl" routerLinkActive="active-route">ask</a>
<a [routerLink]="jobsUrl" routerLinkActive="active-route">jobs</a>
</nav>
</header>
<main>
<router-outlet [routes]="routes"></router-outlet>
</main>
23 changes: 23 additions & 0 deletions examples/hacker_news_pwa/lib/hacker_news_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:async';
import 'dart:convert';

import 'package:http/browser_client.dart';

class HackerNewsService {
static const String baseUrl = 'https://node-hnapi.herokuapp.com';
final BrowserClient _client;

HackerNewsService() : _client = new BrowserClient();

Future<List<Map>> getFeed(String name, int page) async {
final url = '$baseUrl/$name?page=$page';
final response = await _client.get(url);
return JSON.decode(response.body);
}

Future<Map> getItem(String id) async {
final url = '$baseUrl/item/$id';
final response = await _client.get(url);
return JSON.decode(response.body);
}
}
13 changes: 13 additions & 0 deletions examples/hacker_news_pwa/lib/pwa/offline_urls.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// URLs for offline cache.
final List<String> offlineUrls = [
'./',
'./images/icons/icon-large.png',
'./images/icons/icon-small.png',
'./images/logo.svg',
'./main.dart.js',
'./main.dart.js_1.part.js',
'./manifest.json',
];

/// Last modified timestamp of the files
final String lastModified = '2017-10-12T23:40:50.000Z';
25 changes: 25 additions & 0 deletions examples/hacker_news_pwa/lib/src/comment_component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
:host {
display: block;
}

button {
background: inherit;
border: none;
padding: 0;
}

button:hover {
text-decoration: underline;
}

comment {
padding-left: 8px;
}

.content {
border-bottom: 1px solid #eee;
}

.header {
padding: 8px 0;
}
29 changes: 29 additions & 0 deletions examples/hacker_news_pwa/lib/src/comment_component.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:angular/angular.dart';
import 'package:angular/security.dart';

@Component(
selector: 'comment',
templateUrl: 'comment_component.html',
styleUrls: const ['comment_component.css'],
directives: const [CommentComponent, NgFor, NgIf],
changeDetection: ChangeDetectionStrategy.OnPush,
)
class CommentComponent {
final DomSanitizationService _sanitizer;

@Input()
Map comment;

bool hidden = false;

CommentComponent(this._sanitizer);

SafeHtml get content =>
_sanitizer.bypassSecurityTrustHtml(comment['content']);

String get showCommentText => '+${comment['comments_count']}';

void toggleVisibility() {
hidden = !hidden;
}
}
13 changes: 13 additions & 0 deletions examples/hacker_news_pwa/lib/src/comment_component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="header">
<button (click)="toggleVisibility()">
[{{hidden ? showCommentText : '-'}}]
</button>
<span>{{comment['user']}} {{comment['time_ago']}}</span>
</div>
<div [hidden]="hidden">
<div class="content" [innerHtml]="content"></div>
<comment
*ngFor="let comment of comment['comments']"
[comment]="comment">
</comment>
</div>
19 changes: 19 additions & 0 deletions examples/hacker_news_pwa/lib/src/feed_component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
article {
align-items: center;
background: #fff;
border-bottom: 1px solid #eee;
display: flex;
min-height: 56px;
padding: 12px 0;
}

item {
padding-right: 16px;
}

.rank {
color: #212121;
font-weight: 600;
min-width: 56px;
text-align: center;
}
36 changes: 36 additions & 0 deletions examples/hacker_news_pwa/lib/src/feed_component.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:async';

import 'package:angular/angular.dart';
import 'package:angular_router/angular_router.dart';

import '../hacker_news_service.dart';
import 'item_component.dart';

const itemsPerPage = 30;

@Component(
selector: 'feed',
templateUrl: 'feed_component.html',
styleUrls: const ['feed_component.css'],
directives: const [ItemComponent, NgFor, NgIf, routerDirectives],
)
class FeedComponent implements OnActivate {
final HackerNewsService _hackerNewsService;

List<Map> items;
String nextPageUrl;
int startingRank;

FeedComponent(this._hackerNewsService);

@override
Future onActivate(_, RouterState current) async {
final routePath = current.routePath;
final String feed = routePath.additionalData['feed'];
final page = current.queryParameters['p'];
final pageNumber = page != null ? int.parse(page, onError: (_) => 1) : 1;
nextPageUrl = routePath.toUrl(queryParameters: {'p': '${pageNumber + 1}'});
startingRank = itemsPerPage * (pageNumber - 1) + 1;
items = await _hackerNewsService.getFeed(feed, pageNumber);
}
}
20 changes: 20 additions & 0 deletions examples/hacker_news_pwa/lib/src/feed_component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Loading view -->
<template [ngIf]="items == null">
<div>Loading...</div>
</template>

<!-- Loaded empty view -->
<template [ngIf]="items != null && items.isEmpty">
<div>There are no more items.</div>
</template>

<!-- Loaded items view -->
<template [ngIf]="items != null && items.isNotEmpty">
<article *ngFor="let item of items; let i = index">
<span class="rank">{{i + startingRank}}</span>
<item [item]="item"></item>
</article>
<div>
<a [routerLink]="nextPageUrl">More</a>
</div>
</template>
17 changes: 17 additions & 0 deletions examples/hacker_news_pwa/lib/src/item_component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.primary {
margin-bottom: .2rem;
}

.primary a {
color: #000;
text-decoration: none;
}

.secondary {
color: #555;
font-size: .8rem;
}

.secondary a {
color: #555;
}
Loading

0 comments on commit b73eb72

Please sign in to comment.