Skip to content

Commit

Permalink
fix: handle http calls with try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
l00pinfinity committed Oct 27, 2022
1 parent 5f2e9a1 commit b58c942
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 54 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.silkwebhq.devvscapecode"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 2
versionName "1.1.0"
versionCode 3
versionName "1.1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
Expand Down
4 changes: 2 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class AppComponent {

async initializeApp() {
this.platform.ready().then(async () => {
await SplashScreen.show({
fadeOutDuration:2000
await SplashScreen.hide({
fadeOutDuration:1000
});
})
}
Expand Down
157 changes: 107 additions & 50 deletions src/app/home/home.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,11 @@ export class HomePage implements OnInit {
}, 2000);
}

getImages() {
this.data.getImages().subscribe((response: any) => {
if (response) {

}
}, async (error: Error | HttpErrorResponse) => {
const toast = this.toastCtrl.create({
message: `${error}`,
duration: 10000,
position: 'bottom',
color: 'danger'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);
})
}

getImageById(id: number) {
this.data.getImageById(id).subscribe(
(response: any) => {
async getImages() {
try {
this.data.getImages().subscribe((response: any) => {
if (response) {
// console.log(response);
//save the id

}
}, async (error: Error | HttpErrorResponse) => {
const toast = this.toastCtrl.create({
Expand All @@ -105,35 +85,111 @@ export class HomePage implements OnInit {
(await toast).dismiss();
}, 1000);
})
} catch (error) {
const toast = this.toastCtrl.create({
message: error,
duration: 10000,
position: 'bottom',
color: 'danger'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);
}
}

public getPaginatedImages(isFirstLoad, event) {
this.data.getPaginatedImages(this.page).subscribe(
(response: any) => {
if (response) {
// console.log(response);
this.images$ = response;
}
if (isFirstLoad) {
event.target.complete();
}
this.page++;
}, async (error: Error | HttpErrorResponse) => {
const toast = this.toastCtrl.create({
message: `${error.message}`,
duration: 10000,
position: 'bottom',
color: 'danger',
icon: 'sad'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);
async getImageById(id: number) {
try {
this.data.getImageById(id).subscribe(
(response: any) => {
if (response) {
// console.log(response);
//save the id
}
}, async (error: Error | HttpErrorResponse) => {
const toast = this.toastCtrl.create({
message: `${error}`,
duration: 10000,
position: 'bottom',
color: 'danger'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);
})
} catch (error) {
const toast = this.toastCtrl.create({
message: error,
duration: 10000,
position: 'bottom',
color: 'danger'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);
}
}

//redirect to login to update token
this.router.navigateByUrl('/login')
})
public async getPaginatedImages(isFirstLoad, event) {
try {
this.data.getPaginatedImages(this.page).subscribe(
async (response: any) => {
if (response) {
// console.log(response);
this.images$ = response;
}else{
const toast = this.toastCtrl.create({
message: "Something went wrong! Try again",
duration: 10000,
position: 'bottom',
color: 'danger',
icon: 'sad'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);
}

if (isFirstLoad) {
event.target.complete();
}
this.page++;
}, async (error: Error | HttpErrorResponse) => {
const toast = this.toastCtrl.create({
message: `${error.message}`,
duration: 10000,
position: 'bottom',
color: 'danger',
icon: 'sad'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);

//redirect to login to update token
this.router.navigateByUrl('/login')
})
} catch (error) {
const toast = this.toastCtrl.create({
message: error,
duration: 10000,
position: 'bottom',
color: 'danger',
icon: 'sad'
});
(await toast).present();
setTimeout(async () => {
(await toast).dismiss();
}, 1000);

//redirect to login to update token
this.router.navigateByUrl('/login')
}
}

onSelect(image: Images) {
Expand Down Expand Up @@ -189,6 +245,7 @@ export class HomePage implements OnInit {
loading.present();
this.authService.logout();
this.router.navigateByUrl('/login');
loading.dismiss();
} catch (error) {
const toast = this.toastCtrl.create({
message: error,
Expand Down

0 comments on commit b58c942

Please sign in to comment.