Skip to content

Commit

Permalink
Refresher added
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoumpierre committed Mar 28, 2018
1 parent d3e810f commit 1fc9755
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/pages/customers/form/customer-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class CustomerFormPage {
*/
ionViewDidLoad() {
if (this.id) {
this.apiProvider.builder('customers/' + this.id).loader().get({'lists': 0}).subscribe((res) => {
this.apiProvider.builder('customers/' + this.id).loader().get().subscribe((res) => {
this.form.controls['name'].setValue(res.name);
this.form.controls['cnpj'].setValue(res.cnpj);
this.form.controls['phone'].setValue(res.phone);
Expand Down
9 changes: 9 additions & 0 deletions src/pages/customers/index/customers.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
</ion-header>

<ion-content>
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content
pullingIcon="arrow-dropdown"
pullingText="Puxe para atualizar"
refreshingSpinner="circles"
refreshingText="Atualizando...">
</ion-refresher-content>
</ion-refresher>

<ion-list class="single-item">
<ion-item *ngFor="let customer of customers; let i = index" class="list-item">
<div (click)="goToDetails(customer.id)">
Expand Down
13 changes: 13 additions & 0 deletions src/pages/customers/index/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ export class CustomersPage {
this.customers.splice(key, 1);
}
}

/**
* Updates the customer with the refresher
*
* @param refresher
*/
doRefresh(refresher) {
this.syncProvider
.verifySync('customers', true, false)
.then(customers => this.customers = customers)
.then(() => refresher.complete())
.catch((error) => console.log(error));
}
}
5 changes: 1 addition & 4 deletions src/pages/customers/view/customer-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export class CustomerViewPage {
* Gets one item by id
*/
ionViewDidLoad() {
this.apiProvider.builder('customers/' + this.navParams.get('id')).loader().get().subscribe((res) => {
this.customer = res;
console.log(res);
});
this.apiProvider.builder('customers/' + this.navParams.get('id')).loader().get({'lists': true}).subscribe((res) => this.customer = res);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/pages/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

<!-- Conteúdo -->
<ion-content>
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content
pullingIcon="arrow-dropdown"
pullingText="Puxe para atualizar"
refreshingSpinner="circles"
refreshingText="Atualizando...">
</ion-refresher-content>
</ion-refresher>

<ion-grid>
<ion-row>
<ion-col col-5>
Expand Down
12 changes: 12 additions & 0 deletions src/pages/home/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ export class HomePage {
goToUsers() {
this.navCtrl.push(UsersPage);
}

/**
* Updates all the categories with the refresher
*
* @param refresher
*/
doRefresh(refresher) {
this.syncProvider
.syncCategories(['all_customers', 'customers', 'orders', 'products', 'users'], true, false)
.then(() => refresher.complete())
.catch((error) => console.log(error));
}
}
9 changes: 9 additions & 0 deletions src/pages/orders/index/orders.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
</ion-header>

<ion-content>
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content
pullingIcon="arrow-dropdown"
pullingText="Puxe para atualizar"
refreshingSpinner="circles"
refreshingText="Atualizando...">
</ion-refresher-content>
</ion-refresher>

<ion-list class="single-item">
<ion-item *ngFor="let order of orders; let i = index" class="list-item" (click)="goToDetails(order.id)">
<div class="list-item__content">
Expand Down
13 changes: 13 additions & 0 deletions src/pages/orders/index/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ export class OrdersPage {
goToForm() {
this.navCtrl.push(OrderFormPage);
}

/**
* Updates the orders with the refresher
*
* @param refresher
*/
doRefresh(refresher) {
this.syncProvider
.verifySync('orders', true, false)
.then(orders => this.orders = orders)
.then(() => refresher.complete())
.catch((error) => console.log(error));
}
}
9 changes: 9 additions & 0 deletions src/pages/products/index/products.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
</ion-header>

<ion-content>
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content
pullingIcon="arrow-dropdown"
pullingText="Puxe para atualizar"
refreshingSpinner="circles"
refreshingText="Atualizando...">
</ion-refresher-content>
</ion-refresher>

<ion-list class="single-item">
<ion-item *ngFor="let product of products; let i = index" class="list-item">
<ion-avatar item-start>
Expand Down
13 changes: 13 additions & 0 deletions src/pages/products/index/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,17 @@ export class ProductsPage {
this.products.splice(key, 1);
}
}

/**
* Updates the products with the refresher
*
* @param refresher
*/
doRefresh(refresher) {
this.syncProvider
.verifySync('products', true, false)
.then(products => this.products = products)
.then(() => refresher.complete())
.catch((error) => console.log(error));
}
}
10 changes: 9 additions & 1 deletion src/pages/users/index/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@
</ion-navbar>
</ion-header>


<ion-content>
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content
pullingIcon="arrow-dropdown"
pullingText="Puxe para atualizar"
refreshingSpinner="circles"
refreshingText="Atualizando...">
</ion-refresher-content>
</ion-refresher>

<ion-list class="single-item">
<ion-item *ngFor="let item of users; let i = index" class="list-item">
<ion-avatar item-start>
Expand Down
13 changes: 13 additions & 0 deletions src/pages/users/index/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ export class UsersPage {
this.users.splice(key, 1);
}).catch(error => console.log(error));
}

/**
* Updates the user with the refresher
*
* @param refresher
*/
doRefresh(refresher) {
this.syncProvider
.verifySync('users', true, false)
.then(users => this.users = users)
.then(() => refresher.complete())
.catch((error) => console.log(error));
}
}
24 changes: 15 additions & 9 deletions src/providers/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ToastController } from 'ionic-angular';

@Injectable()
export class SyncProvider {
private categories = ['customers', 'orders', 'products', 'users'];
private categories = ['all_customers', 'customers', 'orders', 'products', 'users'];
private syncDelay = (3600 * 5 * 1000);

constructor(private storage: Storage, private apiProvider: ApiProvider, private toastCtrl: ToastController) {
Expand All @@ -18,9 +18,9 @@ export class SyncProvider {
* @param {boolean} force
* @returns {Promise<any>}
*/
syncCategories(categories: string[] = this.categories, force: boolean = false): Promise<any> {
syncCategories(categories: string[] = this.categories, force: boolean = false, toast: boolean = true): Promise<any> {
return new Promise((resolve, reject) => {
this.updateCategoriesData(categories, force).then(() => resolve())
this.updateCategoriesData(categories, force, toast).then(() => resolve())
.catch((error) => reject(error));
});
}
Expand All @@ -32,7 +32,7 @@ export class SyncProvider {
* @param {boolean} force
* @returns {Promise<any>}
*/
verifySync(category: string, force: boolean = false): Promise<any> {
verifySync(category: string, force: boolean = false, toast: boolean = true): Promise<any> {
return new Promise((resolve, reject) => {
this.storage.get('sync_' + category).then(sync => {
sync = sync || [];
Expand All @@ -43,9 +43,9 @@ export class SyncProvider {
let promise;

if (category == 'all_customers') {
promise = this.getCategoryData(category, true, 'customers', {all: true});
promise = this.getCategoryData(category, true && toast, 'customers', {all: true});
} else {
promise = this.getCategoryData(category);
promise = this.getCategoryData(category, toast);
}

promise.then((data) => resolve(data))
Expand All @@ -62,18 +62,24 @@ export class SyncProvider {
* @param {Boolean} force
* @returns {Promise<any>}
*/
private updateCategoriesData(categories: Array<any>, force: Boolean = false): Promise<any> {
private updateCategoriesData(categories: Array<any>, force: boolean = false, toast: boolean = true): Promise<any> {
let promiseChain: Promise<any> = Promise.resolve();
categories = categories || [];

categories.forEach((category, index) => {
this.storage.get('sync_' + category).then(sync => {
if ((sync && !this.isSyncTimeValid(sync['date'])) || !sync || force) {
if (index == 0) {
if (index == 0 && toast) {
this.toast('Atualizando dados da aplicação');
}

promiseChain = promiseChain.then(() => this.getCategoryData(category, (categories.length - 1 == index)));
promiseChain = promiseChain.then(() => {
if (category == 'all_customers') {
return this.getCategoryData(category, (categories.length - 1 == index) && toast, 'customers', {all: true});
}

return this.getCategoryData(category, (categories.length - 1 == index) && toast);
});
}
});
});
Expand Down

0 comments on commit 1fc9755

Please sign in to comment.