Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to change font size with switch statement

Tags:

I have a method with a switch statement like this that gets called every time the size of the window gets changed:

int fontSup = someBoolean ? 18 : 12;
int fontSmall = someBoolean ? 22 : 15;
int fontNormal = someBoolean ? 26 : 20;
int fontTitle = someBoolean ? 28 : 22;

switch (Window.DeviceFamily)
{
    case DeviceFamily.Phone:
        fontSup = someBoolean ? 18 : 6;
        fontSmall = someBoolean ? 22 : 8;
        fontNormal = someBoolean ? 26 : 12;
        fontTitle = someBoolean ? 28 : 14;
        break;
    case DeviceFamily.Tablet:
        fontSup = someBoolean ? 18 : 8;
        fontSmall = someBoolean ? 22 : 12;
        fontNormal = someBoolean ? 26 : 15;
        fontTitle = someBoolean ? 28 : 17;
        break;
    case DeviceFamily.Desktop:
        fontSup = someBoolean ? 18 : 12;
        fontSmall = someBoolean ? 22 : 15;
        fontNormal = someBoolean ? 26 : 20;
        fontTitle = someBoolean ? 28 : 22;
        break;
}

I now thought that there would be a more beautiful and shorter solution for this using a private method. However, here I'm struggling. I could do something like this:

private void SetFontSize (ref int fontToChange, bool someBoolean, int firstValue, int secondValue)
{
    // Same switch statement as in the other method
}

I'd just have to call this method for every font I want to change. But I don't think this is the most beautiful and easiest way to do what I want. Maybe it's just too early in the morning to think logically.

I also tried this:

private int SetFontSize (bool someBoolean, int firstValue, int secondValue)
{
    return (someBoolean ? firstValue : secondValue);
}

But in my opinion this isn't really an improvement because I'd just have to change fontSup = someBoolean ? 18 : 6; to fontSup = SetFontSize(someBoolean, 18, 6);.

Could you please help me to get an appropriate solution?

like image 357
diiN__________ Avatar asked Jun 21 '16 06:06

diiN__________


1 Answers

You could do something like this:

Firstly, encapsulate the font size settings in a class:

public sealed class FontSize
{
    public int Sup;
    public int Small;
    public int Normal;
    public int Title;
}

Now you can initialise a static FontSize and a static Dictionary<DeviceFamily, FontSize> somewhere in your class as follows:

static readonly FontSize _defaultFontSize = new FontSize { Sup = 18, Small = 22, Normal = 26, Title = 28 };

static readonly Dictionary<DeviceFamily, FontSize> _fontSizeMap = new Dictionary<DeviceFamily, FontSize>
{
    { DeviceFamily.Phone,   new FontSize { Sup =  6, Small =  8, Normal = 12, Title = 14 } },
    { DeviceFamily.Tablet,  new FontSize { Sup =  8, Small = 12, Normal = 15, Title = 17 } },
    { DeviceFamily.Desktop, new FontSize { Sup = 12, Small = 15, Normal = 20, Title = 22 } }
};

Then implement a lookup method to return a FontSize from a bool and a DeviceFamily like so:

public static FontSize GetFontSize(DeviceFamily deviceFamily, bool someBoolean)
{
    if (!Enum.IsDefined(typeof(DeviceFamily), deviceFamily))
        throw new ArgumentOutOfRangeException(nameof(deviceFamily));

    if (someBoolean)
        return _defaultFontSize;

    return _fontSizeMap[deviceFamily];
}
like image 132
Matthew Watson Avatar answered Sep 28 '22 04:09

Matthew Watson