[TIMOB-26113] iOS: Ti.UI.convertUnits does not consult ti.ui.defaultunit property
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | Medium |
| Status | Closed |
| Resolution | Fixed |
| Resolution Date | 2018-11-14T15:35:52.000+0000 |
| Affected Version/s | Release 7.3.0 |
| Fix Version/s | Release 8.0.0 |
| Components | iOS |
| Labels | n/a |
| Reporter | Christopher Williams |
| Assignee | Christopher Williams |
| Created | 2018-06-08T14:34:40.000+0000 |
| Updated | 2018-11-21T15:02:02.000+0000 |
Description
Looks like iOS does not consult ti.ui.defaultunit property for converting when input units aren't specified. It assumes "dp", which is the default value placed in tiapp.xml.
[~cwilliams] This ticket says "In Review", but thats probably because it was cloned from another one (TIMOB-26077)? We will take a look.
So after looking into this a bit, there is one thing I am wondering about before digging deeper: The method only allows strings, so using it as
Ti.UI.convertUnits(20, Ti.UI.UNIT_PX)will cause a type error andTi.UI.convertUnits('20', Ti.UI.UNIT_PX)works fine. After that, the issue is [here](https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/TiDimension.m#L75), where we check for string types before even getting the chance to check for the ti.ui.defaultunit (which is only checked for Number types right now). I would look into Android to see how they prioritize input types and change our implementation based on that. *EDIT*: Found a solution that passes all local tests, adding a PR now. Will take the chance to add unit-tests for the API as well.PR: https://github.com/appcelerator/titanium_mobile/pull/10101 Unit-Tests coming after CR.
https://github.com/appcelerator/titanium_mobile/pull/10462
*Closing ticket* Verified fix in SDK version
8.0.0.v20181120171602. Tested using the following test case:*Note** Made sure to changeconst logicalDensityFactor = Ti.Platform.displayCaps.logicalDensityFactor; const dpi = Ti.Platform.displayCaps.dpi; // When ti.ui.defaultunit is px, test value as Number and String Ti.API.info('should be roughly 2.54: ' + Ti.UI.convertUnits(dpi + '', Ti.UI.UNIT_CM)); Ti.API.info('should be roughly 2.54: ' + Ti.UI.convertUnits(dpi, Ti.UI.UNIT_CM)); Ti.API.info('should be roughly 1: ' + Ti.UI.convertUnits(logicalDensityFactor + '', Ti.UI.UNIT_DIP)); Ti.API.info('should be roughly 1: ' + Ti.UI.convertUnits(logicalDensityFactor, Ti.UI.UNIT_DIP)); Ti.API.info('should be exactly 1: ' + Ti.UI.convertUnits(dpi + '', Ti.UI.UNIT_IN)); Ti.API.info('should be exactly 1: ' + Ti.UI.convertUnits(dpi, Ti.UI.UNIT_IN)); Ti.API.info('should be roughly 25.4: ' + Ti.UI.convertUnits(dpi + '', Ti.UI.UNIT_MM)); Ti.API.info('should be roughly 25.4: ' + Ti.UI.convertUnits(dpi, Ti.UI.UNIT_MM)); // When ti.ui.defaultunit is in, test value as Number and String Ti.API.info('should be roughly 2.54: ' + Ti.UI.convertUnits('1', Ti.UI.UNIT_CM)); Ti.API.info('should be roughly 2.54: ' + Ti.UI.convertUnits(1, Ti.UI.UNIT_CM)); Ti.API.info('should be roughly ' + (dpi / logicalDensityFactor) + ': ' + Ti.UI.convertUnits('1', Ti.UI.UNIT_DIP)); Ti.API.info('should be roughly ' + (dpi / logicalDensityFactor) + ': ' + Ti.UI.convertUnits(1, Ti.UI.UNIT_DIP)); Ti.API.info('should be roughly ' + dpi + ': ' + Ti.UI.convertUnits('1', Ti.UI.UNIT_PX)); Ti.API.info('should be roughly ' + dpi + ': ' + Ti.UI.convertUnits(1, Ti.UI.UNIT_PX));ti.ui.defaultunitin thetiapp.xmlto the correct unit being tested. *Test Environment*