Skip to content

Commit 5165924

Browse files
committed
Finishing my second pass through chapter 4
1 parent c191b1a commit 5165924

File tree

6 files changed

+58
-16
lines changed

6 files changed

+58
-16
lines changed

images/chapter4/android/7.png

35.7 KB
Loading

images/chapter4/android/8.gif

477 KB
Loading

images/chapter4/ios/7.png

20.9 KB
Loading

images/chapter4/ios/8.gif

417 KB
Loading

index.html

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,18 +1445,19 @@ <h4 class="exercise-start">
14451445
<p>At this point you can add a grocery item and it will appear immediately in your list—and, all of this is completely driven by a backend service. Pretty cool, huh?</p>
14461446
<p>Let&#39;s look at how you can polish this page with a NativeScript module for showing activity indicators.</p>
14471447
<h3 id="activityindicator">ActivityIndicator</h3>
1448-
<p>Introduce the activity indicator.</p>
1448+
<p>Currently there&#39;s a bit of a delay when you first visit the list page before groceries appear. This delay could confuse a new user, who might think the app is stuck rather than retrieving data from a backend.</p>
1449+
<p>In NativeScript apps you can use the <a href="http://docs.nativescript.org/ApiReference/ui/activity-indicator/README">ActivityIndicator</a> module to show a spinner icon in your UI while your app is busy performing actions. The ActivityIndicator is a relatively simple UI element as it primarily uses one attribute—<code>busy</code>. When an ActivityIndicator&#39;s <code>busy</code> attribute is set to <code>true</code> the ActivityIndicator shows, and when its <code>busy</code> attribute is set to <code>false</code> it doesn&#39;t. Let&#39;s see how the module works by adding an ActivityIndicator to the list page.</p>
14491450
<h4 class="exercise-start">
1450-
<b>Exercise</b>: ???
1451+
<b>Exercise</b>: Add an ActivityIndicator
14511452
</h4>
14521453

14531454
<p>Open up <code>app/pages/list/list.html</code> and paste the following line immediately before the final <code>&lt;/GridLayout&gt;</code>:</p>
14541455
<pre><code class="lang-XML">&lt;ActivityIndicator [busy]=&quot;isLoading&quot; row=&quot;1&quot;&gt;&lt;/ActivityIndicator&gt;
14551456
</code></pre>
1456-
<p>Next, open <code>app/pages/list/list.component.ts</code> and add the following property to the <code>ListPage</code> class (immediately under <code>grocery: string</code>):</p>
1457+
<p>This binds the ActivityIndicator’s <code>busy</code> attribute to an <code>isLoading</code> property in the <code>ListPage</code> component. To define that property, open <code>app/pages/list/list.component.ts</code> and add the following line of code immediately under <code>grocery: string = &quot;&quot;</code>:</p>
14571458
<pre><code class="lang-TypeScript">isLoading = false;
14581459
</code></pre>
1459-
<p>Then, change the existing <code>ngOnInit()</code> function to use the code below:</p>
1460+
<p>Now that the property exists, your final step is to set this flag to to <code>true</code> when the grocery data is loading. To do that, change the existing <code>ngOnInit()</code> function to use the code below:</p>
14601461
<pre><code class="lang-TypeScript">ngOnInit() {
14611462
this.isLoading = true;
14621463
this._groceryListService.load()
@@ -1470,12 +1471,21 @@ <h4 class="exercise-start">
14701471
</code></pre>
14711472
<div class="exercise-end"></div>
14721473

1473-
<p>Talk about how that works, then introduce how an animation can add a little polish.</p>
1474+
<p>When you first visit the list page, you should now see the following loading indicators:</p>
1475+
<p><img src="images/chapter4/android/7.png" alt="ActivityIndicator on Android">
1476+
<img src="images/chapter4/ios/7.png" alt="ActivityIndicator on iOS"></p>
1477+
<blockquote>
1478+
<p><strong>TIP</strong>: You can apply the same <code>row</code> or <code>column</code> attribute to multiple UI controls to have them take up the same space on the screen. The UI control that is defined last will appear on top, which is why the <code>&lt;ActivityIndicator&gt;</code> appears on top of the <code>&lt;ListView&gt;</code> in the previous example.</p>
1479+
<pre><code class="lang-XML">&lt;ListView row=&quot;1&quot;&gt;...&lt;/ListView&gt;
1480+
&lt;ActivityIndicator row=&quot;1&quot;&gt;&lt;/ActivityIndicator&gt;
1481+
</code></pre>
1482+
</blockquote>
1483+
<p>To finish off this chapter, let’s look at how you can use the animation module to add a final bit of polish to how the list page loads.</p>
14741484
<h4 class="exercise-start">
1475-
<b>Exercise</b>: ???
1485+
<b>Exercise</b>: Add a fade-in animation
14761486
</h4>
14771487

1478-
<p>Open <code>app/pages/list/list-common.css</code> and paste the following CSS at the top of the file:</p>
1488+
<p>In this exercise, you’ll use the animation module to animate the <code>opacity</code> of the list page’s <code>&lt;ListView&gt;</code> to add a fade-in effect. Start by opening <code>app/pages/list/list-common.css</code> and pasting in the following CSS at the top of the file:</p>
14791489
<pre><code class="lang-CSS">ListView {
14801490
opacity: 0;
14811491
}
@@ -1499,7 +1509,15 @@ <h4 class="exercise-start">
14991509
</code></pre>
15001510
<div class="exercise-end"></div>
15011511

1502-
<p>Talk about what happened. Include a gif. Transition to talking about npm modules and {N} plugins.</p>
1512+
<p>A few things are happening in the code above.</p>
1513+
<p>First, in CSS, you assign an <code>opacity</code> of <code>0</code> to the list page’s <code>&lt;ListView&gt;</code>. This hides the grocery list completely when the page loads. Next, in TypeScript, after the <code>GroceryListService</code>’s <code>load()</code> call completes, you call the list view’s <code>animate()</code> function. This changes the element&#39;s <code>opacity</code> from <code>0</code> (completely hidden) to <code>1</code> (completely visible) over one full second.</p>
1514+
<p>The result of this code is a nice fade-in animation:</p>
1515+
<p><img src="images/chapter4/android/8.gif" alt="Loading animation on Android">
1516+
<img src="images/chapter4/ios/8.gif" alt="Loading animation on iOS"></p>
1517+
<p>Now that you have functional login and list pages, let’s enhance the app’s functionality as a grocery list management tool. In the next chapters you&#39;ll add functionality such as email validation, social sharing, and more. And you’ll use one of NativeScript&#39;s most useful features to do so: npm modules.</p>
1518+
<blockquote>
1519+
<p><strong>TIP</strong>: There are several modules that come out of the box with your NativeScript installation that we did not have time to cover in this guide—including a <a href="{{site.baseurl}}/ApiReference/location/HOW-TO">location service</a>, a <a href="{{site.baseurl}}/ApiReference/file-system/HOW-TO">file-system helper</a>, a <a href="{{site.baseurl}}/ApiReference/timer/HOW-TO">timer module</a>, a <a href="{{site.baseurl}}/ApiReference/camera/HOW-TO">camera module</a>, and a whole lot more. Make sure to peruse the “Modules API” of the docs, or just look around <code>node_modules/tns-core-modules</code> to see all of what’s available.</p>
1520+
</blockquote>
15031521

15041522
</div>
15051523
<div class="chapter">

src/chapters/chapter4.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,12 @@ Let's look at how you can polish this page with a NativeScript module for showin
563563
564564
### ActivityIndicator
565565
566-
Introduce the activity indicator.
566+
Currently there's a bit of a delay when you first visit the list page before groceries appear. This delay could confuse a new user, who might think the app is stuck rather than retrieving data from a backend.
567+
568+
In NativeScript apps you can use the [ActivityIndicator](http://docs.nativescript.org/ApiReference/ui/activity-indicator/README) module to show a spinner icon in your UI while your app is busy performing actions. The ActivityIndicator is a relatively simple UI element as it primarily uses one attribute—`busy`. When an ActivityIndicator's `busy` attribute is set to `true` the ActivityIndicator shows, and when its `busy` attribute is set to `false` it doesn't. Let's see how the module works by adding an ActivityIndicator to the list page.
567569
568570
<h4 class="exercise-start">
569-
<b>Exercise</b>: ???
571+
<b>Exercise</b>: Add an ActivityIndicator
570572
</h4>
571573
572574
Open up `app/pages/list/list.html` and paste the following line immediately before the final `</GridLayout>`:
@@ -575,13 +577,13 @@ Open up `app/pages/list/list.html` and paste the following line immediately befo
575577
<ActivityIndicator [busy]="isLoading" row="1"></ActivityIndicator>
576578
```
577579
578-
Next, open `app/pages/list/list.component.ts` and add the following property to the `ListPage` class (immediately under `grocery: string`):
580+
This binds the ActivityIndicator’s `busy` attribute to an `isLoading` property in the `ListPage` component. To define that property, open `app/pages/list/list.component.ts` and add the following line of code immediately under `grocery: string = ""`:
579581
580582
``` TypeScript
581583
isLoading = false;
582584
```
583585
584-
Then, change the existing `ngOnInit()` function to use the code below:
586+
Now that the property exists, your final step is to set this flag to to `true` when the grocery data is loading. To do that, change the existing `ngOnInit()` function to use the code below:
585587

586588
``` TypeScript
587589
ngOnInit() {
@@ -598,13 +600,24 @@ ngOnInit() {
598600

599601
<div class="exercise-end"></div>
600602

601-
Talk about how that works, then introduce how an animation can add a little polish.
603+
When you first visit the list page, you should now see the following loading indicators:
604+
605+
![ActivityIndicator on Android](images/chapter4/android/7.png)
606+
![ActivityIndicator on iOS](images/chapter4/ios/7.png)
607+
608+
> **TIP**: You can apply the same `row` or `column` attribute to multiple UI controls to have them take up the same space on the screen. The UI control that is defined last will appear on top, which is why the `<ActivityIndicator>` appears on top of the `<ListView>` in the previous example.
609+
> ``` XML
610+
> <ListView row="1">...</ListView>
611+
> <ActivityIndicator row="1"></ActivityIndicator>
612+
> ```
613+
614+
To finish off this chapter, lets look at how you can use the animation module to add a final bit of polish to how the list page loads.
602615

603616
<h4 class="exercise-start">
604-
<b>Exercise</b>: ???
617+
<b>Exercise</b>: Add a fade-in animation
605618
</h4>
606619

607-
Open `app/pages/list/list-common.css` and paste the following CSS at the top of the file:
620+
In this exercise, youll use the animation module to animate the `opacity` of the list pages `<ListView>` to add a fade-in effect. Start by opening `app/pages/list/list-common.css` and pasting in the following CSS at the top of the file:
608621

609622
``` CSS
610623
ListView {
@@ -634,4 +647,15 @@ ngOnInit() {
634647
635648
<div class="exercise-end"></div>
636649
637-
Talk about what happened. Include a gif. Transition to talking about npm modules and {N} plugins.
650+
A few things are happening in the code above.
651+
652+
First, in CSS, you assign an `opacity` of `0` to the list page’s `<ListView>`. This hides the grocery list completely when the page loads. Next, in TypeScript, after the `GroceryListService`’s `load()` call completes, you call the list views `animate()` function. This changes the element's `opacity` from `0` (completely hidden) to `1` (completely visible) over one full second.
653+
654+
The result of this code is a nice fade-in animation:
655+
656+
![Loading animation on Android](images/chapter4/android/8.gif)
657+
![Loading animation on iOS](images/chapter4/ios/8.gif)
658+
659+
Now that you have functional login and list pages, let’s enhance the app’s functionality as a grocery list management tool. In the next chapters you'll add functionality such as email validation, social sharing, and more. And you’ll use one of NativeScript's most useful features to do so: npm modules.
660+
661+
> **TIP**: There are several modules that come out of the box with your NativeScript installation that we did not have time to cover in this guide—including a [location service]({{site.baseurl}}/ApiReference/location/HOW-TO), a [file-system helper]({{site.baseurl}}/ApiReference/file-system/HOW-TO), a [timer module]({{site.baseurl}}/ApiReference/timer/HOW-TO), a [camera module]({{site.baseurl}}/ApiReference/camera/HOW-TO), and a whole lot more. Make sure to peruse the “Modules API” of the docs, or just look around `node_modules/tns-core-modules` to see all of what’s available.

0 commit comments

Comments
 (0)