<commit_before>//
//  OARegionDownloadsViewController.m
//  OsmAnd
//
//  Created by <NAME> on 5/12/14.
//  Copyright (c) 2014 OsmAnd. All rights reserved.
//

#import "OARegionDownloadsViewController.h"

#import "OsmAndApp.h"
#import "OATableViewCellWithButton.h"
#include "Localization.h"

#define Item_Download OARegionDownloadsViewController__Item_Download
@interface Item_Download : NSObject
@property NSString* caption;
@property std::shared_ptr<const OsmAnd::ResourcesManager::ResourceInRepository> resourceInRepository;
@end
@implementation Item_Download
@end

typedef OsmAnd::ResourcesManager::ResourceType OsmAndResourceType;

@interface OARegionDownloadsViewController ()

@end

@implementation OARegionDownloadsViewController
{
    OsmAndAppInstance _app;

    OAWorldRegion* _worldRegion;

    NSInteger _subregionsSection;
    NSInteger _downloadsSection;

    NSMutableArray* _subregions;
    NSMutableArray* _downloadItems;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self ctor];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self ctor];
    }
    return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        [self ctor];
    }
    return self;
}

- (void)ctor
{
    _app = [OsmAndApp instance];

    _worldRegion = nil;

    _subregionsSection = -1;
    _downloadsSection = -1;

    _subregions = [[NSMutableArray alloc] init];
    _downloadItems = [[NSMutableArray alloc] init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForRegion:(OAWorldRegion*)region
{
    const auto& resourcesInRepository = _app.resourcesManager->getResourcesInRepository();

    _worldRegion = region;
    NSInteger lastUnallocatedSection = 0;

    // Set the title
    self.title = _worldRegion.localizedName;
    if (self.title == nil)
        self.title = _worldRegion.nativeName;

    // Get subregions sorted by alphabet
    [_subregions removeAllObjects];
    for(OAWorldRegion* subregion in _worldRegion.subregions)
    {
        // If subregions has no own subregions, ensure that it has at least something to download
        if ([subregion.subregions count] == 0)
        {
            const QString subregionId = QString::fromNSString(subregion.regionId);
            BOOL hasAtLeastOneDownload = NO;
            for(const auto& resourceInRepository : resourcesInRepository)
            {
                if (!resourceInRepository->id.startsWith(subregionId))
                    continue;

                hasAtLeastOneDownload = YES;
                break;
            }

            if (!hasAtLeastOneDownload)
                continue;
        }

        [_subregions addObject:subregion];
    }
    [_subregions sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        OAWorldRegion* region1 = obj1;
        NSString* title1 = region1.localizedName != nil ? region1.localizedName : region1.nativeName;
        OAWorldRegion* region2 = obj2;
        NSString* title2 = region2.localizedName != nil ? region2.localizedName : region2.nativeName;

        return [title1 localizedCaseInsensitiveCompare:title2];
    }];
    if ([_subregions count] > 0)
        _subregionsSection = lastUnallocatedSection++;
    else
        _subregionsSection = -1;

    // Get downloads
    const QString regionId = QString::fromNSString(_worldRegion.regionId);
    [_downloadItems removeAllObjects];
    for(const auto& resourceInRepository : resourcesInRepository)
    {
        const auto& resourceId = resourceInRepository->id;
        if (!resourceId.startsWith(regionId))
            continue;

        Item_Download* downloadItem = [[Item_Download alloc] init];
        downloadItem.resourceInRepository = resourceInRepository;
        switch(resourceInRepository->type)
        {
            case OsmAndResourceType::MapRegion:
                if ([_worldRegion.subregions count] > 0)
                    downloadItem.caption = OALocalizedString(@"Map of entire region");
                else
                    downloadItem.caption = OALocalizedString(@"Map of the region");
                break;

            default:
                downloadItem.caption = resourceId.toNSString();
        }

        [_downloadItems addObject:downloadItem];
    }
    [_downloadItems sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        Item_Download* downloadItem1 = obj1;
        Item_Download* downloadItem2 = obj2;

        return [downloadItem1.caption localizedCaseInsensitiveCompare:downloadItem2.caption];
    }];
    if ([_downloadItems count] > 0)
        _downloadsSection = lastUnallocatedSection++;
    else
        _downloadsSection = -1;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView<commit_msg>Tweak text<commit_after>//
//  OARegionDownloadsViewController.m
//  OsmAnd
//
//  Created by <NAME> on 5/12/14.
//  Copyright (c) 2014 OsmAnd. All rights reserved.
//

#import "OARegionDownloadsViewController.h"

#import "OsmAndApp.h"
#import "OATableViewCellWithButton.h"
#include "Localization.h"

#define Item_Download OARegionDownloadsViewController__Item_Download
@interface Item_Download : NSObject
@property NSString* caption;
@property std::shared_ptr<const OsmAnd::ResourcesManager::ResourceInRepository> resourceInRepository;
@end
@implementation Item_Download
@end

typedef OsmAnd::ResourcesManager::ResourceType OsmAndResourceType;

@interface OARegionDownloadsViewController ()

@end

@implementation OARegionDownloadsViewController
{
    OsmAndAppInstance _app;

    OAWorldRegion* _worldRegion;

    NSInteger _subregionsSection;
    NSInteger _downloadsSection;

    NSMutableArray* _subregions;
    NSMutableArray* _downloadItems;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self ctor];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self ctor];
    }
    return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        [self ctor];
    }
    return self;
}

- (void)ctor
{
    _app = [OsmAndApp instance];

    _worldRegion = nil;

    _subregionsSection = -1;
    _downloadsSection = -1;

    _subregions = [[NSMutableArray alloc] init];
    _downloadItems = [[NSMutableArray alloc] init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForRegion:(OAWorldRegion*)region
{
    const auto& resourcesInRepository = _app.resourcesManager->getResourcesInRepository();

    _worldRegion = region;
    NSInteger lastUnallocatedSection = 0;

    // Set the title
    self.title = _worldRegion.localizedName;
    if (self.title == nil)
        self.title = _worldRegion.nativeName;

    // Get subregions sorted by alphabet
    [_subregions removeAllObjects];
    for(OAWorldRegion* subregion in _worldRegion.subregions)
    {
        // If subregions has no own subregions, ensure that it has at least something to download
        if ([subregion.subregions count] == 0)
        {
            const QString subregionId = QString::fromNSString(subregion.regionId);
            BOOL hasAtLeastOneDownload = NO;
            for(const auto& resourceInRepository : resourcesInRepository)
            {
                if (!resourceInRepository->id.startsWith(subregionId))
                    continue;

                hasAtLeastOneDownload = YES;
                break;
            }

            if (!hasAtLeastOneDownload)
                continue;
        }

        [_subregions addObject:subregion];
    }
    [_subregions sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        OAWorldRegion* region1 = obj1;
        NSString* title1 = region1.localizedName != nil ? region1.localizedName : region1.nativeName;
        OAWorldRegion* region2 = obj2;
        NSString* title2 = region2.localizedName != nil ? region2.localizedName : region2.nativeName;

        return [title1 localizedCaseInsensitiveCompare:title2];
    }];
    if ([_subregions count] > 0)
        _subregionsSection = lastUnallocatedSection++;
    else
        _subregionsSection = -1;

    // Get downloads
    const QString regionId = QString::fromNSString(_worldRegion.regionId);
    [_downloadItems removeAllObjects];
    for(const auto& resourceInRepository : resourcesInRepository)
    {
        const auto& resourceId = resourceInRepository->id;
        if (!resourceId.startsWith(regionId))
            continue;

        Item_Download* downloadItem = [[Item_Download alloc] init];
        downloadItem.resourceInRepository = resourceInRepository;
        switch(resourceInRepository->type)
        {
            case OsmAndResourceType::MapRegion:
                if ([_worldRegion.subregions count] > 0)
                    downloadItem.caption = OALocalizedString(@"Full map of entire region");
                else
                    downloadItem.caption = OALocalizedString(@"Full map of the region");
                break;

            default:
                downloadItem.caption = resourceId.toNSString();
        }

        [_downloadItems addObject:downloadItem];
    }
    [_downloadItems sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        Item_Download* downloadItem1 = obj1;
        Item_Download* downloadItem2 = obj2;

        return [downloadItem1.caption localizedCaseInsensitiveCompare:downloadItem2.caption];
    }];
    if ([_downloadItems count] > 0)
        _downloadsSection = lastUnallocatedSection++;
    else
        _downloadsSection = -1;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{'fraction_non_alphanumeric': 0.09005043558000916, 'fraction_numerical': 0.007702888583218707, 'mean_word_length': 3.051263001485884, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 76, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896315', 'n_tokens_mistral': 3279, 'n_tokens_neox': 3048, 'n_words': 686}