Ketch는 전적으로 Kotlin으로 구축된 간단하고 강력하며 맞춤설정 가능한 Android용 파일 다운로더 라이브러리입니다. WorkManager의 강력한 기능을 활용하여 Android 애플리케이션에서 파일을 다운로드하는 프로세스를 단순화합니다. Ketch는 애플리케이션 상태에 관계없이 다운로드를 보장합니다.
Ketch 라이브러리를 Android 프로젝트에 통합하려면 다음의 간단한 단계를 따르십시오.
dependencyResolutionManagement {
repositoriesMode . set( RepositoriesMode . FAIL_ON_PROJECT_REPOS )
repositories {
google()
mavenCentral()
maven { url ' https://jitpack.io ' } // this one
}
}
dependencies {
implementation ' com.github.khushpanchal:Ketch:2.0.2 ' // Use latest available version
}
Ketch를 사용하는 가장 간단한 방법:
onCreate 애플리케이션에서 Ketch 인스턴스를 생성합니다. (Ketch는 싱글톤 클래스이며 인스턴스는 처음 사용할 때 자동으로 생성됩니다)
private lateinit var ketch : Ketch
override fun onCreate () {
super .onCreate()
ketch = Ketch .builder().build( this )
}
download() 함수를 호출하고, url, fileName, path를 전달하고 다운로드 상태를 관찰하세요.
val id = ketch.download(url, fileName, path)
lifecycleScope.launch {
repeatOnLifecycle( Lifecycle . State . STARTED ) {
ketch.observeDownloadById(id)
.flowOn( Dispatchers . IO )
.collect { downloadModel ->
// use downloadModel
}
}
}
다운로드를 취소하려면
ketch.cancel(downloadModel.id) // other options: cancel(tag), cancelAll()
다운로드를 일시 중지하려면
ketch.pause(downloadModel.id) // other options: pause(tag), pauseAll()
다운로드를 재개하려면
ketch.resume(downloadModel.id) // other options: resume(tag), resumeAll()
다운로드를 다시 시도하려면
ketch.retry(downloadModel.id) // other options: retry(tag), retryAll()
다운로드를 삭제하려면
ketch.clearDb(downloadModel.id) // other options: clearDb(tag), clearAllDb(), clearDb(timeInMillis)
ketch.clearDb(downloadModel.id, false ) // Pass "false" to skip the actual file deletion (only clear entry from DB)
관찰: 다운로드 항목의 상태 흐름을 제공합니다(각 항목은 url, fileName, 경로, 태그, id, timeQueued, 상태, 진행률, 길이, 속도, lastModified, MetaData, failureReason, eTag와 같은 다운로드 정보를 전달함)
// To observe from Fragment
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle( Lifecycle . State . STARTED ) {
ketch.observeDownloads()
.flowOn( Dispatchers . IO )
.collect {
// set items to adapter
}
}
}
알림을 활성화하려면:
매니페스트 파일에 알림 권한을 추가합니다.
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
사용자에게 권한을 요청합니다(Android 13(API 수준 33)부터 필요). 참조용 샘플 앱을 확인하세요.
초기화하는 동안 알림 구성 전달
ketch = Ketch .builder().setNotificationConfig(
config = NotificationConfig (
enabled = true ,
smallIcon = R .drawable.ic_launcher_foreground // It is required to pass the smallIcon for notification.
)
).build( this )
네트워크 요청에 헤더를 제공합니다.
ketch.download(url, fileName, path,
headers = headers, // Default: Empty hashmap
)
태그: 추가 태그를 제공하여 다양한 다운로드를 그룹화합니다. (이 태그는 다운로드를 취소, 일시중지, 재개, 삭제하는 데에도 사용할 수 있습니다.)
ketch.download(url, fileName, path,
tag = tag, // Default: null
)
구성 다운로드: 사용자 정의 연결 및 읽기 시간 제한 제공
ketch = Ketch .builder().setDownloadConfig(
config = DownloadConfig (
connectTimeOutInMs = 20000L , // Default: 10000L
readTimeOutInMs = 15000L // Default: 10000L
)
).build( this )
사용자 정의 OKHttp: 사용자 정의 okhttp 클라이언트 제공
ketch = Ketch .builder().setOkHttpClient(
okHttpClient = OkHttpClient
. Builder ()
.connectTimeout( 10000L )
.readTimeout( 10000L )
.build()
).build( this )
알림 구성: 맞춤 알림 구성 제공
ketch = Ketch .builder().setNotificationConfig(
config = NotificationConfig (
enabled = true , // Default: false
channelName = channelName, // Default: "File Download"
channelDescription = channelDescription, // Default: "Notify file download status"
importance = importance, // Default: NotificationManager.IMPORTANCE_HIGH
smallIcon = smallIcon, // It is required
showSpeed = true , // Default: true
showSize = true , // Default: true
showTime = true // Default: true
)
).build( this )
Ketch(고수준 디자인)의 작업을 이해하려면 블로그를 확인하세요: https://medium.com/@khush.panchal123/ketch-android-file-downloader-library-7369f7b93bd1
자유롭게 피드백을 제공하거나, 문제를 보고하거나, Ketch에 기여해 주세요. GitHub 저장소로 이동하여 문제를 생성하거나 보류 중인 문제를 찾으세요. 모든 풀 리퀘스트를 환영하나요?
Copyright (C) 2024 Khush Panchal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.