🔔 Çevrimdışı bildirim almak ister misiniz?
Bir konuya etiketlendiğinizde, özel mesaj aldığınızda, bir mesajınız alıntılandığında forumda olmasanız bile anlık bildirim almak için lütfen izni verin.
React Native Expo için uygulama açılışında asset indirme sistemi yapmıştım kendim için daha önce.
Kod: Kodu kopyalamak için üzerine çift tıklayın!
import * as FileSystem from 'expo-file-system';
export class AssetsDownloader
{
/**
* expo construactor
* @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]m {*} u -> url
* @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]m {*} n -> name
*/
downloadResumable = (u, n) => {
return FileSystem.createDownloadResumable(
u,
this.dir + n,
{},
this.callback
);
}
/**
*
* @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]m {*} downloadProgress -> from downloadResumable
*/
callback = downloadProgress => {
const progress = downloadProgress.totalBytesWritten / downloadProgress.totalByte***pectedToWrite;
return progress;
}
/**
*
* @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]m {*} fn -> Function that fetch the asset list
*/
constructor(fn) {
this.fn = fn;
this.assets = [];
this.dir = FileSystem.documentDirectory + "assets/";
}
/**
*
* @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]m {*} k -> Key name // ex: data.image (key: image)
* @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]m {*} b -> BASE_API_URL
*/
getFiles = async(k, b) => {
let res = await this.fn();
if (res.data?.length == 0 || res.data == undefined) this.assets = [];
const { data } = res;
data.forEach((item) => {
this.assets.push({
url: item[k] != null ? `${b}${item[k]}`: null,
name: item[k]
});
})
return this.assets;
}
downloadAssets = async(assets) => {
let dir = await FileSystem.getInfoAsync(this.dir);
if (!dir.exists) await FileSystem.makeDirectoryAsync(this.dir, { intermediates: true });
assets.forEach(async(item) => {
var info = await FileSystem.getInfoAsync(this.dir + item.name)
if (info.exists) return console.log(`Assets ${item.name} already downloaded!`)
this.downloadResumable(item.url, item.name).downloadAsync().then(({ uri }) => {
console.log(`Assets ${item.name} downloaded!`)
})
})
}
}
Kullanımı:
Kod: Kodu kopyalamak için üzerine çift tıklayın!
let url = "https://blabla.com/api/getImages/";
const getImages = async() => {
let res = await axios.get(url);
}
const assetDownloader = new AssetsDownloader(getImages);
assetDownloader.getFiles("name", url).then((res) => {
var filteredAssets = res.filter(x => x.name != null);
assetDownloader.downloadAssets(filteredAssets);
})
Paylaşım için teşekkürler @[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...] eline sağlık.
__________________
I'm a dreamer but it's getting hard to take it.
Some nights don't know if I'm gonna make it.
Heart is heavy but there's nothing that can break me down.