Skip to content

Commit 9f9a334

Browse files
committed
Keep track of units that have been opened, the slide they're on last, and whether the end has been reached
Also, ensure that each unit starts on: - The first card if it has never been opened - The most recently-viewed card for that unit if it has been opened
1 parent 991162e commit 9f9a334

4 files changed

Lines changed: 100 additions & 14 deletions

File tree

platforms/android/assets/www/js/controllers.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ angular.module('starter.controllers', ['starter.services'])
7575
};
7676

7777
$scope.swiper = {};
78+
$scope.currentUnit = null;
7879
$scope.onReadySwiper = function (swiper) {
79-
8080
$scope.swiper = swiper;
81-
swiper.on('slideChangeStart', function () {
8281

83-
console.log('slideChangeStart');
82+
swiper.on('slideChangeEnd', function (sw) {
83+
DBService.logUnitAdvance($scope.currentUnit, sw.activeIndex, sw.isEnd);
8484
});
8585
};
8686

@@ -175,12 +175,16 @@ angular.module('starter.controllers', ['starter.services'])
175175
}
176176
});
177177
});
178+
$scope.currentUnit = unitSlug;
179+
DBService.logUnitStart($scope.currentUnit);
180+
181+
$scope.swiper.detachEvents();
178182
$scope.cards = cardList;
179-
if(!_.isEmpty($scope.swiper)) {
180-
$scope.swiper.update(true);
181-
$scope.swiper.slideTo(0,0,false);
182-
}
183+
$scope.swiper.update(true);
184+
var startSlide = DBService.getStartSlide(unitSlug);
185+
$scope.swiper.slideTo(startSlide,0,false);
183186
$scope.modal.show();
187+
$scope.swiper.attachEvents();
184188
}, function(err) {
185189
// Error
186190
console.log('error');

platforms/android/assets/www/js/services.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
1515
var _topics;
1616
var _topicIndex;
1717
var _downloadedTopics;
18+
var _viewedUnits;
1819
var _idx;
1920

2021
function convertCategoriesToProperties(flatIndex) {
@@ -105,6 +106,7 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
105106
_topics = _db.getCollection('topics');
106107
_downloadedTopics = _db.getCollection('downloadedTopics');
107108
_topicIndex = _db.getCollection('topicIndex');
109+
_viewedUnits = _db.getCollection('viewedUnits');
108110

109111
if(!_topics) {
110112
_topics = _db.addCollection('topics', { unique: ['slug'] });
@@ -114,6 +116,9 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
114116
_downloadedTopics = _db.addCollection('downloadedTopics', { unique: ['slug'] });
115117
}
116118

119+
if(!_viewedUnits) {
120+
_viewedUnits = _db.addCollection('viewedUnits', { unique: ['slug'] });
121+
}
117122

118123
if(!_topicIndex) {
119124
_topicIndex = _db.addCollection('topicIndex', { unique: ['slug'] });
@@ -179,6 +184,40 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
179184

180185
var retVal = _topics.chain().find();
181186
return retVal.data();
187+
},
188+
189+
getStartSlide: function(slug) {
190+
var retVal = 0;
191+
var viewLog = _viewedUnits.by('slug', slug);
192+
if(viewLog) {
193+
retVal = viewLog.currentCardIndex;
194+
}
195+
return retVal;
196+
},
197+
198+
logUnitStart: function(slug) {
199+
var viewLog = _viewedUnits.by('slug', slug);
200+
if(viewLog) {
201+
viewLog.isStarted = true;
202+
_viewedUnits.update(viewLog);
203+
} else {
204+
_viewedUnits.insert({
205+
slug: slug,
206+
isStarted: true,
207+
currentCardIndex: 0
208+
});
209+
}
210+
},
211+
212+
logUnitAdvance: function(slug, cardIndex, isEnd) {
213+
var viewLog = _viewedUnits.by('slug', slug);
214+
if(viewLog) {
215+
viewLog.currentCardIndex = cardIndex;
216+
if(isEnd) {
217+
viewLog.isFinished = true;
218+
}
219+
_viewedUnits.update(viewLog);
220+
}
182221
}
183222
};
184223
});

www/js/controllers.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ angular.module('starter.controllers', ['starter.services'])
7575
};
7676

7777
$scope.swiper = {};
78+
$scope.currentUnit = null;
7879
$scope.onReadySwiper = function (swiper) {
79-
8080
$scope.swiper = swiper;
81-
swiper.on('slideChangeStart', function () {
8281

83-
console.log('slideChangeStart');
82+
swiper.on('slideChangeEnd', function (sw) {
83+
DBService.logUnitAdvance($scope.currentUnit, sw.activeIndex, sw.isEnd);
8484
});
8585
};
8686

@@ -175,12 +175,16 @@ angular.module('starter.controllers', ['starter.services'])
175175
}
176176
});
177177
});
178+
$scope.currentUnit = unitSlug;
179+
DBService.logUnitStart($scope.currentUnit);
180+
181+
$scope.swiper.detachEvents();
178182
$scope.cards = cardList;
179-
if(!_.isEmpty($scope.swiper)) {
180-
$scope.swiper.update(true);
181-
$scope.swiper.slideTo(0,0,false);
182-
}
183+
$scope.swiper.update(true);
184+
var startSlide = DBService.getStartSlide(unitSlug);
185+
$scope.swiper.slideTo(startSlide,0,false);
183186
$scope.modal.show();
187+
$scope.swiper.attachEvents();
184188
}, function(err) {
185189
// Error
186190
console.log('error');

www/js/services.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
1515
var _topics;
1616
var _topicIndex;
1717
var _downloadedTopics;
18+
var _viewedUnits;
1819
var _idx;
1920

2021
function convertCategoriesToProperties(flatIndex) {
@@ -105,6 +106,7 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
105106
_topics = _db.getCollection('topics');
106107
_downloadedTopics = _db.getCollection('downloadedTopics');
107108
_topicIndex = _db.getCollection('topicIndex');
109+
_viewedUnits = _db.getCollection('viewedUnits');
108110

109111
if(!_topics) {
110112
_topics = _db.addCollection('topics', { unique: ['slug'] });
@@ -114,6 +116,9 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
114116
_downloadedTopics = _db.addCollection('downloadedTopics', { unique: ['slug'] });
115117
}
116118

119+
if(!_viewedUnits) {
120+
_viewedUnits = _db.addCollection('viewedUnits', { unique: ['slug'] });
121+
}
117122

118123
if(!_topicIndex) {
119124
_topicIndex = _db.addCollection('topicIndex', { unique: ['slug'] });
@@ -179,6 +184,40 @@ angular.module('starter.services', ['lodash','ionic','lokijs', 'lunr'])
179184

180185
var retVal = _topics.chain().find();
181186
return retVal.data();
187+
},
188+
189+
getStartSlide: function(slug) {
190+
var retVal = 0;
191+
var viewLog = _viewedUnits.by('slug', slug);
192+
if(viewLog) {
193+
retVal = viewLog.currentCardIndex;
194+
}
195+
return retVal;
196+
},
197+
198+
logUnitStart: function(slug) {
199+
var viewLog = _viewedUnits.by('slug', slug);
200+
if(viewLog) {
201+
viewLog.isStarted = true;
202+
_viewedUnits.update(viewLog);
203+
} else {
204+
_viewedUnits.insert({
205+
slug: slug,
206+
isStarted: true,
207+
currentCardIndex: 0
208+
});
209+
}
210+
},
211+
212+
logUnitAdvance: function(slug, cardIndex, isEnd) {
213+
var viewLog = _viewedUnits.by('slug', slug);
214+
if(viewLog) {
215+
viewLog.currentCardIndex = cardIndex;
216+
if(isEnd) {
217+
viewLog.isFinished = true;
218+
}
219+
_viewedUnits.update(viewLog);
220+
}
182221
}
183222
};
184223
});

0 commit comments

Comments
 (0)