ð±Mini Workshop 4: Tinder颚ã®ã¢ãã€ã«ã¢ããªãTindogsããäœãã!
ãããžã§ã¯ãã®ãŽãŒã« | NativeScriptãšVueã䜿ã£ãŠã«ãŒãã¹ã¯ã€ãã¹ã¿ã€ã«ã®ã¢ãã€ã«ã¢ããªãäœã |
---|---|
ãã®ã¯ãŒã¯ã·ã§ããã§åŠã¶ããš | ã¬ã€ã¢ãŠãããã©ã°ã€ã³ã®ç®¡çãå«ããVueãšNativeScriptã䜿ã£ãŠã¯ãã¹ãã©ãããã©ãŒã ã®ãã€ãã£ãã¢ãã€ã«ã¢ããªãæ§ç¯ããæ¹æ³ |
å¿ èŠãªããŒã« | Chromeã®ãããªææ°ã®ãã©ãŠã¶ã NativeScript Playground ãžã®ã¢ã¯ã»ã¹- Playgroundã«ã¢ã«ãŠã³ããäœæããŠãèªåã®äœåã®ããŒãžã§ã³ããã®ãŸãŸã«ããŠããããšãæ€èšããŠãã ããã NativeScript PlaygroundãšPreviewã¢ããªãã€ã³ã¹ããŒã«ãããæºåž¯é»è©±ïŒiOSãŸãã¯AndroidïŒã ãã¬ã€ã°ã©ãŠã³ãçšã®2ã€ã®NativeScriptã³ã³ãããªã³ã¢ããªã¯ãNativeScript ViewerãšNativeScript Playgroundã§ãã Androidã®å Žå: NativeScript Playground ãš NativeScript Preview iOSã®å Žå: NativeScript Playground ãš NativeScript Preview |
ãããæé | 1æé |
ãµã³ãã«ã¢ããªãè©Šãããå Žå | Playgroundã§ãã®ã¢ããªãéã |
ä»åæ§ç¯ãããã®
æé
ãã®ç« ã§ã¯ããããã®é芪æ¢ãäœéšãäœãããã®ãŠã§ãã¢ããªã®æ§ç¯ããããã®ããŒãã®ããªãšãŒã·ã§ã³ãšããŠã®ã¢ãã€ã«ã¢ããªã®æ§ç¯ã«ç§»ããŸããDog CEO APIãåã³äœ¿çšããŠã ãŠãŒã¶ãŒãäžåºŠã«15å¹ã®ç¬ã®ç»åã®ããããèªã¿èŸŒãã§ãå·Šå³ã«ã¹ã¯ã€ãããŠæ§ã ãªç¬ã "ãããïŒ"ããããšãã§ãã "Tinder for Dogs" - "Tindogs" ãäœãããšããŠããŸãã楜ãã¿ã§ä»æ¹ããããŸããïŒã§ã¯ããã£ããå§ããŠã¿ãŸãããã
NativeScript-Vue ã¢ããªãscaffoldããŠããã€ã¹ãæ¥ç¶ãã
ä»åã¯NativeScript Playgroundã§äœæ¥ããŠããã®ã¢ããªãæ§ç¯ããŠãããŸããNativeScript Playground ãéããŠèŠãŠã¿ãŸããããæåã®èšªåæã«ã¯ãäž»èŠãªæ©èœãä¿åãããŠããå Žæã瀺ããã³ãŒãããŒã¯ããããã€ãèŠããŸãã
ãPlay Nowããã¯ãªãã¯ããŠãã¡ã€ã³ãšãã£ã¿ãéããŸããQRã³ãŒãã衚瀺ãããŸãã®ã§ãNativeScript Playgroundã¢ããªã§ã¹ãã£ã³ããŠãã ãããQRã³ãŒããã¹ãã£ã³ãããšãæºåž¯é»è©±ãèªåçã«æŽæ°ãããŸããããã§ãNativeScript-Vueã¢ããªãè¶³å Žã«ããæºåãã§ããŸããã
ããã©ã«ãã§ã¯ãæåã®Playgroundã¢ããªã¯Angularã§äœæãããŠããŸããäžéšã®ãNewããã¯ãªãã¯ãããã³ãã¬ãŒããšããŠãNS-VueããéžæããŸããå¥ã®QRã³ãŒãã衚瀺ãããã®ã§ããã¡ããã¹ãã£ã³ããŠãã ãããã¹ãããæŽæ°ãããåºæ¬çãªã¢ããªã衚瀺ãããŸãã
ããã€ãã®ã¹ã¿ã€ã«ãè¿œå
ããã§ã¯ãã¢ããªã®ã€ã³ã¿ãŒãã§ã€ã¹ã®ã¹ã¿ã€ã«ãäœæããUIãæ§ç¯ããŸããã¢ã¯ã·ã§ã³ããŒãã¿ã€ãã«ã2ã€ã®é ããã¿ã³ãã«ãŒãã®ã¹ã¿ãã¯ããããŸããå³ã«ã¹ã¯ã€ããããå·Šã«ã¹ã¯ã€ããããã«å¿ããŠããããã®ãã¿ã³ã¯çã衚瀺ããããæ¶ãããããŸãã
ã¢ããªã®ã«ãŒã㧠app.css ãã¡ã€ã«ãéãããã¡ã€ã«ã以äžã®ã¹ã¿ã€ã«ã§äžæžãããŸãã
@import 'nativescript-theme-core/css/core.light.css';
.card {
margin: 10;
z-index: 0;
border-radius: 5;
}
.action-bar {
background-color: white;
color: red;
}
.btn {
z-index: 1;
padding: 5;
border-width: 5;
border-radius: 5;
background-color: white;
text-align: center;
font-size: 40px;
opacity: 0;
}
.h1 {
text-align: center;
padding-top: 40;
}
.yes {
color: green;
border-color: green;
transform: rotate(15deg);
}
.no {
color: red;
border-color: red;
transform: rotate(-15deg);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
ãã®ãã¡ã€ã«ã®äžèº«ãèŠãŠã¿ãŸããããæšæºçãªCSSã®ãµãã»ããã䜿ã£ãŠãAndroidãiOSã®ã¢ãã€ã«ã¢ããªã®æ§ã ãªèŠçŽ ãã¹ã¿ã€ã«åããããã®CSSãæžãããšãã§ããŸããz-indexãšopacityïŒäžéæ床ïŒã®èšå®ã«æ³šæããŠãã ããã
ä»ã®ãšãããã¹ã¿ã€ãªã³ã°ã¯ActionBarã«è²ãã€ãã以å€ã«ã¯äœãããŠããŸããããã©ã°ã€ã³ãè¿œå ããŠUIãæ§ç¯ããæºåãå¿ èŠã§ãã
ã«ãŒãã¬ã€ã¢ãŠããäœæãããã©ã°ã€ã³ãè¿œå ãã
ä»åã¯NativeScriptãã©ã°ã€ã³ã䜿ã£ãŠãDog CEO APIããååŸããç»åããã¹ã¿ãã«ãã«ã«ãŒãã®ã¬ã€ã¢ãŠããäœæããŠã¿ãŸãã
ð¡
NativeScript Marketplaceã§ã¯ãã³ãŒããµã³ãã«ããã³ãã¬ãŒããšãšãã«ãããããçš®é¡ã®ã¯ãŒã«ãªæ€èšŒæžã¿ãã©ã°ã€ã³ãã³ãã¥ããã£æ§ç¯ãã©ã°ã€ã³ãèŠã€ããããšãã§ããŸãã
ã¿ã€ãã« Explorer
ã®æšªã«ããå°ã㪠+
ãã¿ã³ãã¯ãªãã¯ããŠããã©ã°ã€ã³ããã¬ã€ã°ã©ãŠã³ãã«ã€ã³ããŒãããŸããã¢ãŒãã«ã§ nativescript-swipe-layout
ãæ€çŽ¢ãããã©ã°ã€ã³ã®ææ°ããŒãžã§ã³ãã€ã³ããŒãããŸãã
次ã«ããã©ã°ã€ã³ã䜿çšã§ããããã«ããããã«ã€ã³ããŒãããå¿
èŠããããŸããapp.js
ãã¡ã€ã«ã®æåã®è¡ã§ Vue
ãã€ã³ããŒããããŠãããšããã§ããã©ã°ã€ã³ãã€ã³ããŒãããŸãããã
Vue.registerElement('SwipeLayout', () => require('./nativescript-swipe-layout').SwipeLayout);
Vue.config.silent = false;
2
ð¡
ãããã°ç®ç㧠Vue.config.silent
ã false
ã«èšå®ããPlayground ã€ã³ã¿ãŒãã§ãŒã¹ã®äžéšã«ãã Device Logs ããã«ã§ãšã©ãŒã確èªãããšäŸ¿å©ã§ãã
ããŒã¿ã®è¿œå
ãŠãŒã¶ãŒãã¹ã¯ã€ãããããã®ç¬ã®ç»åã15åã©ã³ãã ã«Dog CEO APIããååŸããŠã¿ãŸããããHelloWorld.vue
ãã¡ã€ã«ãéããããŒã¿é
åã®ãã¬ãŒã¹ãã«ããšã㊠data
ãªããžã§ã¯ããè¿œå ããswipeLayoutAnimated
ã®å€ã ON_EVENTS
ã«ããŸããåŸè
ã®å€ãèšå®ããããšã§ããŠãŒã¶ãŒã¯å·Šå³ã®ã¹ã¯ã€ãã®ã¿ã匷å¶çã«è¡ãããšãã§ããŸã (äžãäžã«ã¯ã¹ã¯ã€ãããŸãã)ã
çŸåšã®ããŒã¿ãããã¯ã眮ãæãã:
data() {
return {
dogArray: [],
dogs: [],
swipeLayoutAnimated: 'ON_EVENTS'
}
},
2
3
4
5
6
7
泚æ: ãã¡ã€ã«ã®
<script>
é åã®ã³ãŒãããã©ãŒããããããŠããªãå Žåã¯ãcmd-Z
ãšæ°åã¿ã€ãããŠã³ãŒããåãã©ãŒãããããŠã¿ãŠãã ããã
ããã§APIãåŒã³åºãæºåãã§ããŸããããŸãã<script>
ã¿ã°ã®ããäžã« http
ã¢ãžã¥ãŒã«ãã€ã³ããŒãããŸãã
const http = require("http");
次ã«ãmethods
ãããã¯ãäœæããŸããdata() {...},
ãããã¯ã®æåŸã®ã«ã³ãã®åŸã«APIãžã®åŒã³åºããè¿œå ããŸãã
methods: {
getMultiDogs() {
http.request({
url: "https://dog.ceo/api/breeds/image/random/15", method: "GET"
}).then((response) => {
this.dogArray = JSON.parse(response.content);
for (let i = 0; i < 15; i++) {
this.dogs.push(this.dogArray.message[i])
}
console.log(JSON.stringify(this.dogs))
}).catch( (e) => {
alert("error")
});
}
//handle the swipes here
},
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ãã®APIã³ãŒã«ãèŠãŠãã ãããäžåºŠã«å€ãã®ã«ãŒããèªã¿èŸŒãã§ã¢ããªã®åäœãé ããªããªãããã«ã15æã®ã©ã³ãã ãªç»åãAPIã«èŠæ±ããŠããŸããã¬ã¹ãã³ã¹ãé åã«ããŒã¹ãããããã«ãŒãããŠç¬ã®ç»åã®URLã®é åãäœæããŸããããã€ã¹ãã°ã«åºåãããããã«ãªãã¯ãã§ãã
次ã«ãã¢ããªã®äœææã«ãã® getMultiDogs
ã¡ãœãããåŒã³åºããŸãããã®ãããã¯ã methods: {},
ãããã¯ã®æåŸã®ã«ã³ãã®çŽåŸã«è¿œå ããŸãã
created() {
this.getMultiDogs();
},
2
3
ð¡
泚æããŠãã ããããã® created()
ã©ã€ããµã€ã¯ã«ããã¯ã methods
ã®äžã§å²ã¿ãããªããŸãããå®éã«ã¯ã¡ãœãããããã¯ã®å€åŽã«ãªããã°ãªããŸããã
æåŸã«ãUIã«ç»åã衚瀺ããŸãããã³ãã¬ãŒããããã¯å
ã® <ScrollView...
ã¿ã°ã以äžã®ããŒã¯ã¢ããã«çœ®ãæããŠãã ããã
<StackLayout>
<GridLayout rows="*" columns="*">
<SwipeLayout v-for="dog in dogs" :key="dog.id" row="0" col="0" :animationState="swipeLayoutAnimated">
<image class="card" :src="dog" stretch="aspectFill"></image>
</SwipeLayout>
</GridLayout>
</StackLayout>
2
3
4
5
6
7
ð¡
ãŠã§ãçšã®éçºãšã¢ãã€ã«çšã®éçºã®æ倧ã®éãã¯ãã¬ã€ã¢ãŠãã§ããNativeScript ã®ã¬ã€ã¢ãŠãã¯ããã€ãã£ãã®ã¢ãã€ã«ã¬ã€ã¢ãŠããåŠçããããã«èšèšãããŠããããŠã§ãéçºè ã«éŠŽæã¿ã®ãã DOM æ§ç¯ã®ãããªãã¯ããã¯ã¯ãããŸãããNativeScript ã®ã¬ã€ã¢ãŠãã«ã€ããŠã®è©³çŽ°ã¯ ãã¡ã
ãã®é ã«ãªããšãã«ãŒãã®äžã«ç¬ãåºãŠããã¯ãã§ãããã«ãŒããã¹ã¯ã€ãã§ããŸããããããçŽããŸãããã
ã«ãŒããã¹ã¯ã€ãå¯èœã«ãã
ãŠãŒã¶ãŒã®ãžã§ã¹ãã£ãŒãå·Šå³ã¹ã¯ã€ãã管çããã¡ãœãããããã€ãè¿œå ããå¿ èŠããããŸãã
ãŸããã¡ãœãããããã¯ã«2ã€ã®æ°ããã¡ãœãããè¿œå ããgetMultiDogs
ã¡ãœããã®éãæ¬åŒ§ã®åŸã«ã«ã³ããè¿œå ããŸãã
next() {
this.dogs.pop();
},
swipeRightCallback(e) {
this.next();
},
swipeLeftCallback(e) {
this.next();
}
2
3
4
5
6
7
8
9
次ã«ãã¹ã¯ã€ãæã«ã³ãŒã«ããã¯ãåŒã³åºãããããã« <SwipeLayout
ããŒã¯ã¢ãããç·šéããŸãã
ãã®è¡ãïŒ
<SwipeLayout v-for="dog in dogs" :key="dog.id" row="0" col="0" :animationState="swipeLayoutAnimated">
ãã®ããã«å€æŽããŸããïŒ
<SwipeLayout v-for="dog in dogs" :key="dog.id" row="0" col="0" :animationState="swipeLayoutAnimated" @swipeLeft="swipeLeftCallback($event)" @swipeRight="swipeRightCallback($event)">
ãšããããããã®ç·šéã§ã¯ããŠãŒã¶ãŒãã«ãŒããå»æ£ããéã« dogs é åããèŠçŽ ãåé€ããŠãå·Šå³ã®ã¹ã¯ã€ããžã§ã¹ãã£ãŒã®ã³ãŒã«ããã¯ãè¿œå ããŸããããã§ã«ãŒãã¯å·Šå³ã«ã¹ã¯ã€ãããããã«ãªãã¯ãã§ãã
ãããã15å以äžã¹ã¯ã€ãããå Žåã¯ã©ããªãã®ã§ããããïŒå€ãããŒã¿ãç Žæ£ãããåŸãæ°ããããŒã¿ã»ããã®ããã«å床APIãåŒã³åºãããã« next()
ã¡ãœããã®åŒã³åºããè¿œå ããããšãã§ããŸããnext()
ãç·šéããŸããããïŒ
next() {
this.dogs.pop();
console.log(this.dogs.length);
if (this.dogs.length == 0) {
alert("Loading some more!");
this.getMultiDogs();
}
},
2
3
4
5
6
7
8
å·Šå³ã«ã¹ã¯ã€ãããŠã¿ãŠãã ããã15çªãŸã§ã¹ã¯ã€ããããšã©ããªããŸããïŒããã€ã¹ãã°ãèŠãŠãé åã®é·ãã確èªããŠã¿ãŸãããã
ã¢ãã¡ãŒã·ã§ã³ãã¿ã³ãè¿œå ãã
ããªãã®ã¢ããªã¯15å¹ã®ç¬ãããŒãããå·Šå³ã«ã¹ã¯ã€ãããããšãã§ããç¬ã®é åã空ã®ãšãã«ããã«15å¹ã®ç¬ãããŒãããããšãã§ããŸãã
å·Šã«ã¹ã¯ã€ããããšèµ€ããã¿ã³ãç¹æ» ããå³ã«ã¹ã¯ã€ããããšç·ã®ãã¿ã³ãç¹æ» ãããããªNativeScriptã®ã¢ãã¡ãŒã·ã§ã³ãè©ŠããŠã¿ãŸãããã
ãŸããäžéšã® <script>
ã¿ã°ã®äžã§ã¢ãã¡ãŒã·ã§ã³ã¢ãžã¥ãŒã«ãrequireããŸããïŒ
const Animation = require('ui/animation');
ãããŠãUIã®äžéšããªãŒããã³ã°ã® <StackLayout
ã¿ã°ã®çŽäžã«ã»ã¯ã·ã§ã³ãè¿œå ããŸãã
<AbsoluteLayout>
<GridLayout style="z-index:1" columns="*,*" width="100%" paddingTop="20">
<label ref="no" col="0" text="ð€" class="btn no" />
<label ref="yes" col="1" text="ð" class="btn yes" />
</GridLayout>
<label class="h1" width="100%" text="Fetch a new friend!" />
</AbsoluteLayout>
2
3
4
5
6
7
ãã®ããŒã¯ã¢ããã§ã¯ãã¬ã€ã¢ãŠãã®çŽ æµãªçµã¿åããã玹ä»ããŠããŸããAbsoluteLayoutã¯ãèŠããªããã¿ã³ãã¢ããªã®äžéšã«åºå®ããã¿ã€ãã«ãéããŠããŸããAbsoluteLayoutã¿ã°ã®äžã«ã¯ããã¿ã³ãæ°Žå¹³ã«é 眮ããGridLayoutããããŸãã
æåŸã«ãã³ãŒã«ããã¯ã³ãŒãã以äžã®2ã€ã®ã¡ãœããã«çœ®ãæããŸããåãã¿ã³ã®opacityãäžå¯èŠããå¯èŠã«çŽ æ©ãå€æŽããããã«ã¢ãã¡ãŒã·ã§ã³ã¢ãžã¥ãŒã«ã䜿çšããŠããããšã«æ³šæããŠãã ããã
swipeRightCallback(e) {
this.$refs.yes.nativeView.animate({ opacity: 1 })
.then(() => {
this.$refs.yes.nativeView.animate({ opacity: 0 })
.then(() => {
this.next();
})
})
},
swipeLeftCallback(e) {
this.$refs.no.nativeView.animate({ opacity: 1 })
.then(() => {
this.$refs.no.nativeView.animate({ opacity: 0 })
.then(() => {
this.next();
})
})
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ð¡
ãã€ãã£ããã¥ãŒå
ã§ã¢ãã¡ãŒã·ã§ã³ãããå¿
èŠãããèŠçŽ ãžã®åç
§ã§ãã $refs
ã䜿çšããŠããããšã«æ³šæããŠãã ãããã©ã®èŠçŽ ãåç
§ãããŠãããã¯ãããŒã¯ã¢ããã®äžã§ ref
ãæ¢ãããšã§èå¥ã§ããŸããref="no"
ãæ¢ãããšã§ãã©ã®èŠçŽ ãåç
§ãããŠããããç¹å®ã§ããŸããåç
§ã¯ãWebéçºã«ãããdivã®'id'ã®ãããªåœ¹å²ãæãããŸãã
ã§ã¯ãã¹ã¯ã€ãããŠã¿ãŠãã ãããã¹ã¯ã€ããããšãèµ€ãšç·ã®ã©ãã«ã衚瀺ãããŸãã
æåŸã®åŸ®èª¿æŽã¯ãActionBarã®ã¿ã€ãã«ãå€æŽããããšã§ã! ã¢ããªã«ååãã€ããŸãããã
ðã¢ãã€ã«ã¢ããªãå®æããŸãããïŒããã§ãšãããããŸãïŒïŒïŒð