Skip to content

Commit

Permalink
提高兼容性,支持minsdk21,优化使用体验
Browse files Browse the repository at this point in the history
  • Loading branch information
XJ-Up committed Jul 17, 2024
1 parent aae1a58 commit 77220ca
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 136 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![pk3f4HO.png](https://s21.ax1x.com/2024/05/30/pk3f4HO.png)](https://imgse.com/i/pk3f4HO)

### 一个让开发者快速完成上传功能的框架
### 一个让开发者快速完成上传功能的框架 支持java、kotlin

# 截图

Expand All @@ -26,10 +26,10 @@ allprojects {
```groovy
// build.gradle(Module:)
dependencies {
implementation 'com.github.XJ-Up:quickupload:1.2.0'
implementation 'com.github.XJ-Up:quickupload:1.2.1'
}
```

## 具体使用可参考demo[点我](https://github.com/XJ-Up/quickupload/tree/main/app/src/main/java/com/dh/updemo)
# 如何使用

#### 第一步 Application进行初始化配置
Expand All @@ -48,16 +48,16 @@ UploadConfiguration.initialize(

```kotlin

//第一步:构建数据类并继承 UploadObserverBase() ,其中fileName、filePath均为自定义内容
//第一步:构建数据类并继承 UploadObserverBase()且必须传入uploadId(你觉得任何唯一性的字符串) ,其中fileName、filePath均为自定义内容
data class FileItem(
val fileName: String,
val filePath: String,
) : UploadObserverBase()
override val uploadId: String,
) : UploadObserverBase(uploadId)


//第二步:创建数据类对象 赋值uploadId 添加至观察者
fileItem = FileItem(name(Uri.parse(path)), path)
fileItem?.uploadId = path //根据你的具体情况选择合适的值作为 uploadId
fileItem = FileItem(name(Uri.parse(path)), path, uploadId)
//添加至观察者
UploadService.observers.add(fileItem)

Expand Down Expand Up @@ -107,6 +107,8 @@ fileItem.stopUpload()
#### 第三步 获取上传详情

```kotlin
//注意: fileItem中已包含对应的数据(uploadStatus, uploadInfo, throwable, serverResponse)
// 这里的触发只是进行ui更新,详细使用可查看demo
fileItem.refresh { uploadStatus, uploadInfo, throwable, serverResponse ->
when (uploadStatus) {
UploadStatus.DEFAULT -> {
Expand All @@ -133,5 +135,5 @@ fileItem.stopUpload()
}
```
#### 如果你没有服务器上传接口,你可以下载服务器demo[点我](https://github.com/XJ-Up/TestServer)搭建自己的测试服务器,来体验quickupload
#### 具体使用可参考demo


23 changes: 11 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ plugins {

android {
namespace 'com.dh.updemo'
compileSdk 34
compileSdk compilesdk_version

defaultConfig {
applicationId "com.dh.updemo"
minSdk 21
targetSdk 34
minSdk minsdk_version
targetSdk targetsdk_version
versionCode 4
versionName "1.5.0"
versionName "1.0.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -36,13 +36,12 @@ android {
}

dependencies {
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7'
implementation 'androidx.navigation:navigation-ui-ktx:2.7.7'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation project(':quickupload')
// implementation 'com.github.XJ-Up:quickupload:1.5.0'
// implementation 'com.github.XJ-Up:quickupload:1.2.1'
}
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SingleFileUploadActivity"/>
<activity android:name=".MultipleSingleFileUploadsActivity"/>
<activity android:name=".UploadMultipleFilesSimultaneouslyActivity"/>
<activity android:name=".SingleFileUploadActivity"
android:exported="true"/>
<activity android:name=".MultipleSingleFileUploadsActivity"
android:exported="true"/>
<activity android:name=".UploadMultipleFilesSimultaneouslyActivity"
android:exported="true"/>
</application>

</manifest>
4 changes: 2 additions & 2 deletions app/src/main/java/com/dh/updemo/FileItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import com.dh.quickupload.observer.task.UploadObserverBase
data class FileItem(
val fileName: String,
val filePath: String,

) : UploadObserverBase()
override val uploadId: String,
) : UploadObserverBase(uploadId)
6 changes: 4 additions & 2 deletions app/src/main/java/com/dh/updemo/FilesItem.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dh.updemo

import com.dh.quickupload.observer.task.UploadObserverBase

/**
* 多个地址文件上传示例
* 使用:
Expand All @@ -9,5 +10,6 @@ import com.dh.quickupload.observer.task.UploadObserverBase
*/
data class FilesItem(
val fileName: String,
val filePath: MutableList<String>
): UploadObserverBase()
val filePath: MutableList<String>,
override val uploadId: String,
) : UploadObserverBase(uploadId)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MultipleSingleFileUploadsActivity : AppCompatActivity() {

recyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)

recyclerView.itemAnimator=null

uploadAdapter = UploadAdapter(fileList) {
val uploadStatus = fileList[it].status
Expand All @@ -53,6 +53,9 @@ class MultipleSingleFileUploadsActivity : AppCompatActivity() {
recyclerView.adapter = uploadAdapter
findViewById<Button>(R.id.uploadStart).setOnClickListener {
fileList.forEachIndexed { index, s ->
if (UploadService.taskList.contains(s.uploadId)){
return@forEachIndexed
}
val uploadRequest =
QuickUploadRequest(this, serverUrl = "http://192.168.30.137:8080/upload")
.setMethod("POST")
Expand Down Expand Up @@ -115,13 +118,10 @@ class MultipleSingleFileUploadsActivity : AppCompatActivity() {
filePath.addAll(path)

filePath.forEach {
val fileItem = FileItem(name(Uri.parse(it)), it)
fileItem.uploadId = it
val fileItem = FileItem(name(Uri.parse(it)), it, it)
fileItem.refresh { _, _, _, _ ->
runOnUiThread {
uploadAdapter.notifyDataSetChanged()
}

val indexOf = fileList.indexOf(fileItem)
uploadAdapter.notifyItemChanged(indexOf)
}
UploadService.observers.add(fileItem)
fileList.add(fileItem)
Expand Down
62 changes: 36 additions & 26 deletions app/src/main/java/com/dh/updemo/SingleFileUploadActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dh.updemo

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
Expand All @@ -14,6 +15,9 @@ import com.dh.quickupload.data.UploadStatus
import com.dh.quickupload.quick.QuickUploadRequest
import java.io.File

/**
* 单文件上传
*/
class SingleFileUploadActivity : AppCompatActivity() {
private lateinit var progressBar: ProgressBar
private lateinit var uploadStart: Button
Expand All @@ -25,7 +29,8 @@ class SingleFileUploadActivity : AppCompatActivity() {
companion object {
private const val READ_REQUEST_CODE = 4
}
private var fileItem:FileItem?=null

private var fileItem: FileItem? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.single_file_upload_layout)
Expand All @@ -36,22 +41,21 @@ class SingleFileUploadActivity : AppCompatActivity() {
uploadAddress = findViewById(R.id.uploadAddress)
selectFile = findViewById(R.id.selectFile)
uploadStart.setOnClickListener {

if (fileItem == null) {
Toast.makeText(this, "请选择文件", Toast.LENGTH_SHORT).show()
} else {

val request= QuickUploadRequest(this, serverUrl = "http://192.168.30.137:8080/upload")
.setMethod("POST")
.addFileToUpload(
filePath = fileItem!!.filePath,
parameterName = "files"
)
.setResumedFileStart(0)//如果需要断点续传调用此方法,默认情况下不需要调用
fileItem?.quickUploadRequest=request
fileItem?.startUpload()

fileItem?.let {
it.quickUploadRequest= QuickUploadRequest(this, serverUrl = "http://192.168.30.137:8080/upload")
.setMethod("POST")
.addFileToUpload(
filePath = it.filePath,
parameterName = "files"
)
.setResumedFileStart(0)
it.startUpload()
}
}

}
endOfUpload.setOnClickListener {
fileItem?.stopUpload()
Expand Down Expand Up @@ -82,40 +86,46 @@ class SingleFileUploadActivity : AppCompatActivity() {
}
}

@SuppressLint("SetTextI18n")
private fun onPickedFiles(path: String) {
fileItem = FileItem(name(Uri.parse(path)), path)
fileItem?.uploadId=path
fileItem = FileItem(name(Uri.parse(path)), path, path)
fileItem?.refresh { uploadStatus, uploadInfo, throwable, serverResponse ->
when(uploadStatus){
UploadStatus.DEFAULT->{
when (uploadStatus) {
UploadStatus.DEFAULT -> {

}
UploadStatus.Wait->{

UploadStatus.Wait -> {

}
UploadStatus.InProgress->{

UploadStatus.InProgress -> {
progressBar.progress = uploadInfo.progressPercent
uploadProgress.text = "已上传:${uploadInfo.progressPercent.toString()}%"
uploadProgress.text = "已上传:${uploadInfo.progressPercent}%"
}
UploadStatus.Success->{

UploadStatus.Success -> {
uploadProgress.text = "连接成功"
}
UploadStatus.Error->{
uploadProgress.text = "${throwable.toString()}"

UploadStatus.Error -> {
uploadProgress.text = throwable.toString()
}
UploadStatus.Completed->{

UploadStatus.Completed -> {
if (uploadInfo.progressPercent == 100) {
uploadProgress.text = "上传完成"
}
}
else->{}
}

else -> {}
}
}
UploadService.observers.add(fileItem!!)
uploadAddress.text = "本地文件地址:$path"

}

fun name(uri: Uri): String {
return contentResolver.query(uri, null, null, null, null)?.use {
if (it.moveToFirst()) {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/dh/updemo/UploadAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class UploadAdapter(
override fun onBindViewHolder(holder: UploadViewHolder, position: Int) {
holder.bind(fileList[position], position)
}

override fun getItemCount(): Int = fileList.size

inner class UploadViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val fileName: TextView = itemView.findViewById(R.id.file_name)
private val progressBar: ProgressBar = itemView.findViewById(R.id.progress_bar)
Expand All @@ -51,11 +49,13 @@ class UploadAdapter(
}

UploadStatus.Completed -> {
uploadButton.text = "上传完成"
if (fileItem.uploadInfo.progressPercent==100){
uploadButton.text = "上传完成"

}
}

else -> {

}
}
uploadButton.setOnClickListener {
Expand Down
Loading

0 comments on commit 77220ca

Please sign in to comment.