Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-25970] Android: 'postlayout' event not working for Picker with type Titanium.UI.PICKER_TYPE_DATE #10005

Merged
merged 13 commits into from
May 16, 2018
Merged
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
<ti.modules.titanium.ui.widget.picker.CustomDatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titanium_ui_date_picker_spinner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:datePickerMode ="spinner"/>
android:datePickerMode ="spinner"/>
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,13 @@ public void onDismiss(DialogInterface dialog)
if (settings.containsKey("title")) {
dialog.setTitle(TiConvert.toString(settings, "title"));
}
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog)
{
fireEvent(TiC.EVENT_POST_LAYOUT, null, false);
}
});
dialog.show();
if (settings.containsKey("okButtonTitle")) {
dialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setText(TiConvert.toString(settings, "okButtonTitle"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ti.modules.titanium.ui.widget.picker;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.DatePicker;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiUIHelper;

public class CustomDatePicker extends DatePicker
{

private TiViewProxy proxy = null;

public void setProxy(TiViewProxy value)
{
this.proxy = value;
}

public CustomDatePicker(Context context)
{
super(context);
}

public CustomDatePicker(Context context, AttributeSet attrs)
{
super(context, attrs);
}

public CustomDatePicker(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
if (this.proxy != null && changed) {
TiUIHelper.firePostLayoutEvent(this.proxy);
} else {
Log.w("Picker", "Proxy not assigned to a native view!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiRHelper.ResourceNotFoundException;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import android.app.Activity;
Expand All @@ -42,18 +41,11 @@ public TiUIDatePicker(final TiViewProxy proxy, Activity activity)
this(proxy);
Log.d(TAG, "Creating a date picker", Log.DEBUG_MODE);

DatePicker picker;
CustomDatePicker picker;
// If it is not API Level 21 (Android 5.0), create picker normally.
// If not, it will inflate a spinner picker to address a bug.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
picker = new DatePicker(activity) {
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
TiUIHelper.firePostLayoutEvent(proxy);
}
};
picker = new CustomDatePicker(activity);
} else {
// A bug where PickerCalendarDelegate does not send events to the
// listener on API Level 21 (Android 5.0) for TIMOB-19192
Expand All @@ -69,8 +61,9 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}
return;
}
picker = (DatePicker) activity.getLayoutInflater().inflate(datePickerSpinner, null);
picker = (CustomDatePicker) activity.getLayoutInflater().inflate(datePickerSpinner, null);
}
picker.setProxy(getProxy());
setNativeView(picker);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/Resources/ti.ui.picker.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe('Titanium.UI.Picker', function () {

it('DatePicker postlayout event in layout', function (finish) {
var win = Ti.UI.createWindow(),
dp = Ti.UI.createPicker({
type: Ti.UI.PICKER_TYPE_DATE
});

dp.addEventListener('postlayout', function () {
finish();
});
win.add(dp);
win.open();
});

});