Merge pull request #234 from cmgustavo/ref/design-16

Fix addressbook. Modal can be opened from anywhere
This commit is contained in:
Matias Alejo Garcia 2016-08-25 17:47:04 -03:00 committed by GitHub
commit 9c6ccd3ede
7 changed files with 172 additions and 117 deletions

View File

@ -1,4 +1,4 @@
<ion-modal-view>
<ion-modal-view ng-controller="addressbookModalController" ng-init="initAddressbook()">
<ion-header-bar align-title="center" class="bar-stable">
<button class="button button-clear button-positive"
ng-click="closeAddressbookModal()">
@ -22,14 +22,17 @@
<i class="icon ion-ios-search placeholder-icon"></i>
<input type="search"
placeholder="Search"
ng-model="search"
ng-change="findContact(search, {onlyContacts: true})" ng-model-onblur>
ng-model="addrSearch"
ng-change="findAddressbook(addrSearch)" ng-model-onblur>
</label>
</div>
<div ng-show="!addAddressbookEntry">
<ion-list>
<ion-item ng-repeat="addrEntry in list" class="item-icon-left" ng-show="!addrEntry.isWallet" ng-click="goToAmount(addrEntry)">
<ion-item ng-repeat="addrEntry in addressbook"
class="item-icon-left"
ng-show="!addrEntry.isWallet"
ng-click="sendTo(addrEntry)">
<i class="icon ion-ios-person-outline"></i>
<h2>{{addrEntry.label}}</h2>
<p>{{addrEntry.address}}</p>
@ -50,8 +53,6 @@
<form name="addressbookForm" ng-show="addAddressbookEntry" no-validate>
<div class="padding-vertical assertive" ng-show="error">{{error|translate}}</div>
<div class="list">
<label class="item item-input item-stacked-label">
@ -63,7 +64,7 @@
ng-show="addressbookForm.address.$invalid && addressbookEntry.address"></i>
</div>
<div class="qr-scan-icon">
<qr-scanner on-scan="onQrCodeScanned(data)"></qr-scanner>
<qr-scanner on-scan="onQrCodeScanned(data, addressbookForm)"></qr-scanner>
<input type="text"
id="address"
name="address"

View File

@ -20,7 +20,7 @@
<h3>Contacts & Wallets</h3>
</div>
<div class="col col-20 text-right">
<a class="button button-light" ng-click="openAddressbookModal(list)">
<a class="button button-light" ng-click="openAddressbookModal()">
<i class="size-36 icon ion-person-add"></i>
</a>
</div>

View File

@ -0,0 +1,124 @@
'use strict';
angular.module('copayApp.controllers').controller('addressbookModalController', function($scope, $log, $state, $timeout, $ionicPopup, addressbookService, lodash) {
var contacts;
// An alert dialog
var showAlert = function(title, msg, cb) {
$log.warn(title + ": " + msg);
var alertPopup = $ionicPopup.alert({
title: title,
template: msg
});
if (!cb) cb = function() {};
alertPopup.then(cb);
};
$scope.initAddressbook = function() {
addressbookService.list(function(err, ab) {
if (err) $log.error(err);
$scope.isEmptyList = lodash.isEmpty(ab);
contacts = [];
lodash.each(ab, function(v, k) {
contacts.push({
label: v,
address: k
});
});
$scope.addressbook = lodash.clone(contacts);
});
};
$scope.findAddressbook = function(search) {
if (!search || search.length < 2) {
$scope.addressbook = contacts;
$timeout(function() {
$scope.$apply();
}, 10);
return;
}
var result = lodash.filter(contacts, function(item) {
var val = item.label;
return lodash.includes(val.toLowerCase(), search.toLowerCase());
});
$scope.addressbook = result;
};
$scope.sendTo = function(item) {
$scope.closeAddressbookModal();
$timeout(function() {
$state.transitionTo('send.amount', { toAddress: item.address, toName: item.label})
}, 100);
};
$scope.closeAddressbookModal = function() {
$scope.cleanAddressbookEntry();
$scope.addAddressbookEntry = false;
$scope.addressbookModal.hide();
};
$scope.onQrCodeScanned = function(data, addressbookForm) {
$timeout(function() {
var form = addressbookForm;
if (data && form) {
data = data.replace('bitcoin:', '');
form.address.$setViewValue(data);
form.address.$isValid = true;
form.address.$render();
}
$scope.$digest();
}, 100);
};
$scope.cleanAddressbookEntry = function() {
$scope.addressbookEntry = {
'address': '',
'label': ''
};
};
$scope.toggleAddAddressbookEntry = function() {
$scope.cleanAddressbookEntry();
$scope.addAddressbookEntry = !$scope.addAddressbookEntry;
};
$scope.add = function(addressbook) {
$timeout(function() {
addressbookService.add(addressbook, function(err, ab) {
if (err) {
showAlert(err);
return;
}
$scope.initAddressbook();
$scope.toggleAddAddressbookEntry();
$scope.$digest();
});
}, 100);
};
$scope.remove = function(addr) {
$timeout(function() {
addressbookService.remove(addr, function(err, ab) {
if (err) {
showAlert(err);
return;
}
$scope.initAddressbook();
$scope.$digest();
});
}, 100);
};
$scope.$on('$destroy', function() {
$scope.addressbookModal.remove();
});
});

View File

@ -4,15 +4,6 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
var originalList;
// An alert dialog
var showAlert = function(title, msg) {
$log.warn(title + ": " + msg);
var alertPopup = $ionicPopup.alert({
title: title,
});
};
$scope.init = function() {
originalList = [];
@ -32,8 +23,6 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
addressbookService.list(function(err, ab) {
if (err) $log.error(err);
$scope.isEmptyList = lodash.isEmpty(ab);
var contacts = [];
lodash.each(ab, function(v, k) {
contacts.push({
@ -50,10 +39,7 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
});
};
$scope.findContact = function(search, opts) {
opts = opts || {};
$scope.findContact = function(search) {
if (incomingData.redir(search)) {
return;
@ -67,8 +53,7 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
return;
}
var result = lodash.filter($scope.list, function(item) {
if (opts && opts.onlyContacts && item.isWallet) return;
var result = lodash.filter(originalList, function(item) {
var val = item.label || item.alias || item.name;
return lodash.includes(val.toLowerCase(), search.toLowerCase());
});
@ -87,82 +72,17 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
});
};
/*
* Modal Addressbook
*/
$ionicModal.fromTemplateUrl('views/modals/addressbook.html', {
scope: $scope
}).then(function(modal) {
$scope.addressbookModal = modal;
});
$scope.openAddressbookModal = function() {
$scope.addressbookModal.show();
$ionicModal.fromTemplateUrl('views/modals/addressbook.html', {
scope: $scope
}).then(function(modal) {
$scope.addressbookModal = modal;
$scope.addressbookModal.show();
});
};
$scope.closeAddressbookModal = function() {
$scope.cleanAddressbookEntry();
$scope.addAddressbookEntry = false;
$scope.addressbookModal.hide();
};
$scope.onQrCodeScanned = function(data, addressbookForm) {
$timeout(function() {
var form = addressbookForm;
if (data && form) {
data = data.replace('bitcoin:', '');
form.address.$setViewValue(data);
form.address.$isValid = true;
form.address.$render();
}
$scope.$digest();
}, 100);
};
$scope.cleanAddressbookEntry = function() {
$scope.addressbookEntry = {
'address': '',
'label': ''
};
};
$scope.toggleAddAddressbookEntry = function() {
$scope.cleanAddressbookEntry();
$scope.addAddressbookEntry = !$scope.addAddressbookEntry;
};
$scope.add = function(addressbook) {
$timeout(function() {
addressbookService.add(addressbook, function(err, ab) {
if (err) {
$log.error(err);
return;
}
$scope.init();
$scope.toggleAddAddressbookEntry();
$scope.$digest();
});
}, 100);
};
$scope.remove = function(addr) {
$timeout(function() {
addressbookService.remove(addr, function(err, ab) {
if (err) {
$scope.error = err;
return;
}
$scope.init();
$scope.$digest();
});
}, 100);
};
$scope.$on('$destroy', function() {
$scope.addressbookModal.remove();
$scope.$on('modal.hidden', function() {
$scope.init();
});
$scope.$watch('')
});

View File

@ -72,6 +72,6 @@ angular.module('copayApp.directives')
},
controller: controller,
replace: true,
template: '<a ng-click="openScanner()"><i class="icon ion-qr-scanner"></i></a>'
template: '<a on-tap="openScanner()"><i class="icon ion-qr-scanner"></i></a>'
}
});

View File

@ -1,8 +1,18 @@
'use strict';
angular.module('copayApp.services').factory('addressbookService', function($stateParams, storageService, profileService, lodash) {
angular.module('copayApp.services').factory('addressbookService', function(bitcore, storageService, lodash) {
var root = {};
var getNetwork = function(addr) {
var Address = bitcore.Address;
if (Address.isValid(addr, 'livenet')) {
return 'livenet';
}
if (Address.isValid(addr, 'testnet')) {
return 'testnet';
}
};
root.getLabel = function(addr, cb) {
storageService.getAddressbook('testnet', function(err, ab) {
if (ab && ab[addr]) return cb(ab[addr]);
@ -31,13 +41,14 @@ angular.module('copayApp.services').factory('addressbookService', function($stat
};
root.add = function(entry, cb) {
var wallet = profileService.getWallet($stateParams.walletId);
root.list(function(err, ab) {
var network = getNetwork(entry.address);
storageService.getAddressbook(network, function(err, ab) {
if (err) return cb(err);
if (!ab) ab = {};
if (ab) ab = JSON.parse(ab);
ab = ab || {};
if (ab[entry.address]) return cb('Entry already exist');
ab[entry.address] = entry.label;
storageService.setAddressbook(wallet.credentials.network, JSON.stringify(ab), function(err, ab) {
storageService.setAddressbook(network, JSON.stringify(ab), function(err, ab) {
if (err) return cb('Error adding new entry');
root.list(function(err, ab) {
return cb(err, ab);
@ -47,13 +58,15 @@ angular.module('copayApp.services').factory('addressbookService', function($stat
};
root.remove = function(addr, cb) {
var wallet = profileService.getWallet($stateParams.walletId);
root.list(function(err, ab) {
var network = getNetwork(addr);
storageService.getAddressbook(network, function(err, ab) {
if (err) return cb(err);
if (!ab) return;
if (ab) ab = JSON.parse(ab);
ab = ab || {};
if (lodash.isEmpty(ab)) return cb('Addressbook is empty');
if (!ab[addr]) return cb('Entry does not exist');
delete ab[addr];
storageService.setAddressbook(wallet.credentials.network, JSON.stringify(ab), function(err) {
storageService.setAddressbook(network, JSON.stringify(ab), function(err) {
if (err) return cb('Error deleting entry');
root.list(function(err, ab) {
return cb(err, ab);
@ -63,10 +76,11 @@ angular.module('copayApp.services').factory('addressbookService', function($stat
};
root.removeAll = function() {
var wallet = profileService.getWallet($stateParams.walletId);
storageService.removeAddressbook(wallet.credentials.network, function(err) {
if (err) return cb('Error deleting addressbook');
return cb();
storageService.removeAddressbook('livenet', function(err) {
storageService.removeAddressbook('testnet', function(err) {
if (err) return cb('Error deleting addressbook');
return cb();
});
});
};

View File

@ -450,14 +450,10 @@ ul.wallet-selection.wallets {
margin-right: 10px;
}
.qr-scan-icon {
position: relative;
}
.qr-scan-icon a {
display: block;
position: absolute;
top: 0;
top: 25px;
right: 0;
padding: 0 10px;
font-size: 24px;