Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26113] iOS: Ti.UI.convertUnits does not consult ti.ui.defaultunit property

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2018-11-14T15:35:52.000+0000
Affected Version/sRelease 7.3.0
Fix Version/sRelease 8.0.0
ComponentsiOS
Labelsn/a
ReporterChristopher Williams
AssigneeChristopher Williams
Created2018-06-08T14:34:40.000+0000
Updated2018-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.

Comments

  1. Hans Knöchel 2018-06-12

    [~cwilliams] This ticket says "In Review", but thats probably because it was cloned from another one (TIMOB-26077)? We will take a look.
  2. Hans Knöchel 2018-06-12

    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 and Ti.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.
  3. Hans Knöchel 2018-06-12

    PR: https://github.com/appcelerator/titanium_mobile/pull/10101 Unit-Tests coming after CR.
  4. Christopher Williams 2018-11-13

    https://github.com/appcelerator/titanium_mobile/pull/10462
  5. Samir Mohammed 2018-11-21

    *Closing ticket* Verified fix in SDK version 8.0.0.v20181120171602. Tested using the following test case:
       const 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));
       
    *Note** Made sure to change ti.ui.defaultunit in the tiapp.xml to the correct unit being tested. *Test Environment*
       APPC Studio: 5.1.0.201808080937
       iPhone 6 Sim (iOS 12.1)
       APPC CLI: 7.0.6
       Operating System Name: Mac OS Mojave
       Operating System Version: 10.14.1
       Node.js Version: 8.9.1
       Xcode 10.1
       

JSON Source