Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
allow to add remote files i.e. from network
Browse files Browse the repository at this point in the history
  • Loading branch information
m7mdra committed Oct 9, 2021
1 parent adb9233 commit 3182bbd
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 90 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.QuestionForm">
<activity android:name="com.m7mdra.questionForm.RecordAudioActivity"></activity>
<activity android:name=".CameraActivity"></activity>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
34 changes: 15 additions & 19 deletions app/src/main/java/com/example/questionform/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider.getUriForFile
import androidx.core.net.toFile
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.canhub.cropper.CropImage
Expand Down Expand Up @@ -133,23 +134,31 @@ class MainActivity : AppCompatActivity() {
ImageQuestion(
faker.hobbit().quote(), id = faker.crypto().md5(),
mandatory = faker.bool().bool(),
done = faker.bool().bool()
done = faker.bool().bool(),
value = if (faker.bool().bool()) listOf(
faker.avatar().image(),
faker.avatar().image(),
"https://storage.googleapis.com/gweb-uniblog-publish-prod/images/Chrome__logo.max-500x500.png",
""
).toMutableList() else mutableListOf<String>()

)
)
list.add(
VideoQuestion(
faker.backToTheFuture().quote(), id = faker.crypto().md5(),
mandatory = faker.bool().bool(),
done = faker.bool().bool()
done = faker.bool().bool(),
value = "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/Big_Buck_Bunny_720_10s_1MB.mp4"

)
)
list.add(
AudioQuestion(
title = faker.gameOfThrones().quote(), id = faker.crypto().md5(),
mandatory = faker.bool().bool(),
done = faker.bool().bool()
done = faker.bool().bool(),
value = "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_2MG.mp3"

)
)
Expand Down Expand Up @@ -247,7 +256,7 @@ class MainActivity : AppCompatActivity() {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 43) {
if (resultCode == RESULT_OK) {
questionAdapter.updatePickedVideo(videoFile)
questionAdapter.updatePickedVideo(videoFile.path)
}
}
if (requestCode == 42) {
Expand All @@ -259,24 +268,11 @@ class MainActivity : AppCompatActivity() {
if (resultCode == RESULT_OK) {
val recordFile: Uri? = data?.getParcelableExtra<Uri>("recordPath")
val position = data?.getIntExtra("position", -1) ?: -1
recordFile.log()
position.log()
questionAdapter.addRecordFile(recordFile, position)
questionAdapter.addRecordFile(recordFile?.path, position)

}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
val result = CropImage.getActivityResult(data)
if (resultCode == Activity.RESULT_OK) {
result?.also {
questionAdapter.updateImageAdapterAtPosition(
it.originalUri.toString()
)
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
val error = result!!.error
}
}

}

override fun onRequestPermissionsResult(
Expand Down
1 change: 0 additions & 1 deletion form/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation 'com.squareup.picasso:picasso:2.71828'
implementation ("com.github.CanHub:Android-Image-Cropper:3.1.1")

}
46 changes: 39 additions & 7 deletions form/src/main/java/com/m7mdra/questionForm/ImageAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.m7mdra.questionForm

import android.content.Context
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.ViewGroup
import android.webkit.URLUtil
import androidx.recyclerview.widget.RecyclerView
import com.m7mdra.questionForm.viewholder.RowImageViewHolder
import com.squareup.picasso.Cache
import com.squareup.picasso.LruCache
import com.squareup.picasso.OkHttp3Downloader
import com.squareup.picasso.Picasso
import java.io.File

class ImageAdapter(private val clickListener: (Int, String) -> Unit = { _, _ -> }) :
class ImageAdapter(
private val context: Context,
private val clickListener: (Int, String) -> Unit = { _, _ -> }
) :
RecyclerView.Adapter<RowImageViewHolder>() {

private val list = mutableListOf<String>()
Expand All @@ -29,20 +38,43 @@ class ImageAdapter(private val clickListener: (Int, String) -> Unit = { _, _ ->
return RowImageViewHolder(view)
}

private val picasso: Picasso by lazy {
Picasso.Builder(context)
.memoryCache(LruCache(context))
.loggingEnabled(BuildConfig.DEBUG)
.downloader(OkHttp3Downloader(context))
.build()
}


override fun onBindViewHolder(holder: RowImageViewHolder, position: Int) {
val imageSource: String = list[position]
holder.view.setOnClickListener {
clickListener.invoke(position, imageSource)
}
Picasso.get()
.load(File(imageSource))
.fit()
.centerInside()
.into(holder.selectedImageView)
if (URLUtil.isHttpUrl(imageSource) || URLUtil.isHttpsUrl(imageSource)) {
picasso.load(imageSource)
.fit()
.centerInside()
.error(R.drawable.placeholder_image)
.placeholder(R.drawable.placeholder_image)
.into(holder.selectedImageView)
} else if(URLUtil.isFileUrl(imageSource)) {
picasso
.load(File(imageSource))
.fit()
.error(R.drawable.placeholder_image)
.placeholder(R.drawable.placeholder_image)
.centerInside()
.into(holder.selectedImageView)
}else{
return
}


}

override fun getItemCount(): Int {
return list.size
}
}
}
Loading

0 comments on commit 3182bbd

Please sign in to comment.