Maps: Property 'OfflinePackDownloadState' does not exist on type 'typeof MapboxGL'.

Created on 13 Oct 2020  路  7Comments  路  Source: react-native-mapbox-gl/maps

Describe the bug
Typescript definition for OfflinePackDownloadState is missing.

To Reproduce

import MapboxGL from '@react-native-mapbox-gl/maps'

console.log('inactive', MapboxGL.OfflinePackDownloadState.Inactive)
console.log('active', MapboxGL.OfflinePackDownloadState.Active)
console.log('complete', MapboxGL.OfflinePackDownloadState.Complete)

Typescript error: TS2339: Property 'OfflinePackDownloadState' does not exist on type 'typeof MapboxGL'.

Expected behavior
index.d.ts should contain type definition for OfflinePackDownloadState. Problem seems to be that iOS and Android have a different integer values for Inactive, Active and Complete. Previous code logs values shifted by one.

// on iOS
inactive 1
active 2
complete 3

// on Android 
inactive 0
active 1
complete 2

Solution
Add type definition into index.d.ts file. Catch is how to deal with different integer values on Android and iOS.

export enum OfflinePackDownloadState {
    Inactive = 1, // or 0 on Android
    Active = 2, // or 1
    Complete = 3, // or 2
}

Workaround

export const OfflinePackDownloadState =
    Platform.OS === 'ios'
        ? {
              Inactive: 1,
              Active: 2,
              Complete: 3,
          }
        : {
              Inactive: 0,
              Active: 1,
              Complete: 2,
          }

Versions (please complete the following information):

  • Platform: Android, iOS
  • Device: iPhone8, Huawei P20
  • Emulator/ Simulator: yes/ yes
  • OS: iOS12
  • react-native-mapbox-gl Version 8.1.0-rc.4
  • React Native Version 0.63.3
bug help wanted wontfix

All 7 comments

Running into this issue as well. I had to verify the completion of offline packs in Android in a different way than iOS. Good suggestion. Is anyone opposed to trying to make these more uniform?

@systemlevel , for the types - there is some effort poured into switching the repo to Typescript.
With that we'll hopefully get more robust types.

As for the mismatch in this API, gotta check if this is something upstream (which I don't think) or something in our implementation (more likely).
If it's the later - gotta find time (or someone else) to look into it :D

Observations

iOS has following definition https://github.com/react-native-mapbox-gl/maps/blob/master/ios/RCTMGL/MGLModule.m#L201-L204

NSMutableDictionary *offlinePackDownloadState = [[NSMutableDictionary alloc] init];
[offlinePackDownloadState setObject:@(MGLOfflinePackStateInactive) forKey:@"Inactive"];
[offlinePackDownloadState setObject:@(MGLOfflinePackStateActive) forKey:@"Active"];
[offlinePackDownloadState setObject:@(MGLOfflinePackStateComplete) forKey:@"Complete"

Android has following defintions https://github.com/react-native-mapbox-gl/maps/blob/master/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLModule.java#L236-L240

// offline region download states
Map<String, Integer> offlinePackDownloadStates = new HashMap<>();
offlinePackDownloadStates.put("Inactive", RCTMGLOfflineModule.INACTIVE_REGION_DOWNLOAD_STATE);
offlinePackDownloadStates.put("Active", RCTMGLOfflineModule.ACTIVE_REGION_DOWNLOAD_STATE);
offlinePackDownloadStates.put("Complete", RCTMGLOfflineModule.COMPLETE_REGION_DOWNLOAD_STATE);

Which is defined here https://github.com/react-native-mapbox-gl/maps/blob/master/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLOfflineModule.java#L50-L52

public static final int INACTIVE_REGION_DOWNLOAD_STATE = OfflineRegion.STATE_INACTIVE;
public static final int ACTIVE_REGION_DOWNLOAD_STATE = OfflineRegion.STATE_ACTIVE;
public static final int COMPLETE_REGION_DOWNLOAD_STATE = 2;

According to Mapbox Android SDK documentation region can be either active or inactive. And region can be either complete or not complete. Those are two separate properties of region.
https://docs.mapbox.com/archive/android/maps/api/8.2.1/com/mapbox/mapboxsdk/offline/OfflineRegion.html

In contrast to Mapbox iOS SDK documentation where pack can be either _unknown_, _inactive_, _active_, _complete_, _invalid_. https://github.com/mapbox/mapbox-gl-native-ios/blob/ios-v6.2.1/platform/darwin/src/MGLOfflinePack.h#L14-L54

@RobertSasak, you might have fallen under the curse of the competent: Wanna open a PR for this? :)

@ferdicus More than happy to do that. I am not sure at what level and how to fix this. Any suggestions?

oh boy, after looking at it... I'm not really sure 馃

I guess easiest way would be to rejigger the return values for Android and align them to the iOS scheme.... that just feels wrong though 馃槥

I'm curious why no one else mentioned this before 馃槄

So, if you feel strong about this and want a unified API, feel free to come up with a cool way to align those.
That would also come with a Changelog though explaining the breaking changes in OfflinePackDownloadState.

Or we stay with the current implementation and simply add the typescript definition update.

I don't have a strong opinion on this topic - would be fine with both solutions.

Thanks for looking into this and digging in @RobertSasak 馃檱

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jayhaluska picture jayhaluska  路  5Comments

dorthwein picture dorthwein  路  3Comments

peterleng picture peterleng  路  4Comments

ivari picture ivari  路  3Comments

mustafaskyer picture mustafaskyer  路  3Comments