Skip to content

Replace "Loading" template with progress bar #2571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/components/progress-bar.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div local-class="progress-bar" style={{this.progress.style}}></div>
6 changes: 6 additions & 0 deletions app/components/progress-bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { inject as service } from '@ember/service';
import Component from '@glimmer/component';

export default class extends Component {
@service progress;
}
8 changes: 8 additions & 0 deletions app/components/progress-bar.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.progress-bar {
position: fixed;
left: 0;
top: 0;
height: 3px;
box-shadow: 0 0 10px rgba(0, 13, 41, 0.6);
background: var(--yellow) !important;
}
1 change: 1 addition & 0 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { inject as service } from '@ember/service';
export default Controller.extend({
design: service(),
flashMessages: service(),
progress: service(),
});
5 changes: 5 additions & 0 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inject as service } from '@ember/service';
export default Route.extend({
flashMessages: service(),
googleCharts: service(),
progress: service(),
session: service(),

beforeModel() {
Expand All @@ -15,6 +16,10 @@ export default Route.extend({
},

actions: {
loading(transition) {
this.progress.handle(transition);
return true;
},
didTransition() {
this.flashMessages.step();
},
Expand Down
84 changes: 84 additions & 0 deletions app/services/progress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Service, { inject as service } from '@ember/service';
import { htmlSafe } from '@ember/string';
import { tracked } from '@glimmer/tracking';

import { rawTimeout, task } from 'ember-concurrency';
import { buildWaiter } from 'ember-test-waiters';

const SPEED = 200;

let waiter = buildWaiter('progress-bar');

export default class ProgressService extends Service {
@service router;

count = 0;
progress = 0;

@tracked _style = '';

get style() {
return htmlSafe(this._style);
}

handle(thenable) {
this.counterTask.perform(thenable);
}

increaseCounter() {
this.count += 1;
this.updateTask.perform();
}

decreaseCounter() {
this.count -= 1;
}

@task(function* (promise) {
try {
this.increaseCounter();
yield promise;
} finally {
this.decreaseCounter();
}
})
counterTask;

@(task(function* () {
let token = waiter.beginAsync();

this.progress = 0;
this._style = `width: 0%`;

while (this.count !== 0) {
yield rawTimeout(SPEED);

let currentAmount;
if (this.progress >= 0 && this.progress < 0.2) {
currentAmount = 0.1;
} else if (this.progress >= 0.2 && this.progress < 0.5) {
currentAmount = 0.04;
} else if (this.progress >= 0.5 && this.progress < 0.8) {
currentAmount = 0.02;
} else if (this.progress >= 0.8 && this.progress < 0.99) {
currentAmount = 0.005;
} else {
currentAmount = 0;
}

this.progress += currentAmount;
if (this.progress > 0.998) {
this.progress = 0.998;
}

this._style = `transition: width ${SPEED}ms linear; width: ${this.progress * 100}%`;
}

this._style = `transition: width ${SPEED}ms linear; width: 100%`;
yield rawTimeout(SPEED);
this._style = `transition: opacity ${SPEED * 2}ms linear; width: 100%; opacity: 0`;

waiter.endAsync(token);
}).drop())
updateTask;
}
1 change: 1 addition & 0 deletions app/styles/application.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--violet: #2e2359;
--dark-grey: #2a3439;
--dark-green: #3b6837;
--yellow: #ffc833;

--header-bg-color: var(--dark-green);
--footer-bg-color: var(--dark-green);
Expand Down
12 changes: 0 additions & 12 deletions app/styles/loading.module.css

This file was deleted.

2 changes: 2 additions & 0 deletions app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

{{page-title "crates.io: Rust Package Registry" separator=' - ' prepend=true}}

<ProgressBar/>

<Header/>

<main local-class="main">
Expand Down
3 changes: 0 additions & 3 deletions app/templates/loading.hbs

This file was deleted.