1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import { Component , ViewChild , ElementRef } from '@angular/core' ;
15+ import { Component , ViewChild , ElementRef , OnDestroy } from '@angular/core' ;
1616import { IonicPage , NavController , NavParams } from 'ionic-angular' ;
1717import { TranslateService } from '@ngx-translate/core' ;
1818import { CoreAppProvider } from '@providers/app' ;
@@ -25,6 +25,7 @@ import { CoreLoginHelperProvider } from '../../providers/helper';
2525import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
2626import { CoreConfigConstants } from '../../../../configconstants' ;
2727import { CoreCustomURLSchemes } from '@providers/urlschemes' ;
28+ import { Subscription } from 'rxjs' ;
2829
2930/**
3031 * Page to enter the user credentials.
@@ -34,7 +35,7 @@ import { CoreCustomURLSchemes } from '@providers/urlschemes';
3435 selector : 'page-core-login-credentials' ,
3536 templateUrl : 'credentials.html' ,
3637} )
37- export class CoreLoginCredentialsPage {
38+ export class CoreLoginCredentialsPage implements OnDestroy {
3839
3940 @ViewChild ( 'credentialsForm' ) formElement : ElementRef ;
4041
@@ -57,6 +58,7 @@ export class CoreLoginCredentialsPage {
5758 protected viewLeft = false ;
5859 protected siteId : string ;
5960 protected urlToOpen : string ;
61+ protected valueChangeSubscription : Subscription ;
6062
6163 constructor ( private navCtrl : NavController ,
6264 navParams : NavParams ,
@@ -89,6 +91,28 @@ export class CoreLoginCredentialsPage {
8991 } else {
9092 this . showScanQR = false ;
9193 }
94+
95+ if ( appProvider . isIOS ( ) ) {
96+ // Make iOS auto-fill work. The field that isn't focused doesn't get updated, do it manually.
97+ // Debounce it to prevent triggering this function too often when the user is typing.
98+ this . valueChangeSubscription = this . credForm . valueChanges . debounceTime ( 1000 ) . subscribe ( ( changes ) => {
99+ if ( ! this . formElement || ! this . formElement . nativeElement ) {
100+ return ;
101+ }
102+
103+ const usernameInput = this . formElement . nativeElement . querySelector ( 'input[name="username"]' ) ;
104+ const passwordInput = this . formElement . nativeElement . querySelector ( 'input[name="password"]' ) ;
105+ const usernameValue = usernameInput && usernameInput . value ;
106+ const passwordValue = passwordInput && passwordInput . value ;
107+
108+ if ( typeof usernameValue != 'undefined' && usernameValue != changes . username ) {
109+ this . credForm . get ( 'username' ) . setValue ( usernameValue ) ;
110+ }
111+ if ( typeof passwordValue != 'undefined' && passwordValue != changes . password ) {
112+ this . credForm . get ( 'password' ) . setValue ( passwordValue ) ;
113+ }
114+ } ) ;
115+ }
92116 }
93117
94118 /**
@@ -336,4 +360,11 @@ export class CoreLoginCredentialsPage {
336360 }
337361 }
338362 }
363+
364+ /**
365+ * Component destroyed.
366+ */
367+ ngOnDestroy ( ) : void {
368+ this . valueChangeSubscription && this . valueChangeSubscription . unsubscribe ( ) ;
369+ }
339370}
0 commit comments