aboutsummaryrefslogtreecommitdiff
path: root/src/tile.js
blob: 044f08661f3f1e67b83b270a8bca70a910d0f44e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, { Component } from "react";
import { Image, View, ActivityIndicator } from "react-native";
import * as FileSystem from 'expo-file-system';

export default class Tile extends Component {
    constructor(props) {
        super(props)
        this.state = {
            loaded: false,
            uri: '',
        }
    }
    componentDidMount() {
        this._mounted = true;
        this.load()
    }
    load = async () => {
        const fileUri = FileSystem.cacheDirectory + 'tiles/'  + this.props.tileKey + '.png'
        let finfo = await FileSystem.getInfoAsync(fileUri)
        if (!finfo.exists) {
            finfo = await FileSystem.downloadAsync(this.props.source, fileUri)
        }
        if (this._mounted) {
            this.setState({ loaded: true, uri: finfo.uri })
        }
        this.props.onLoad && this.props.onLoad()
    }
      
      componentWillUnmount() {
        this._mounted = false;
      }
    render() {
        if (!this.state.loaded) {
            return null
        }
        return (<Image style={this.props.style} source={{ uri: this.state.uri }} resizeMethod={"scale"} />)
    }
}