Skip to content

Commit

Permalink
Merge pull request #5 from kindnesswall/bugfix/upload-image-and-gallery
Browse files Browse the repository at this point in the history
implement compress image feature and make fullscreen gallery
  • Loading branch information
mreram authored and Mohammad Reza committed Aug 2, 2020
2 parents 970978a + 261f8e0 commit 3118e70
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 36 deletions.
4 changes: 2 additions & 2 deletions app/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################### APP_VERSION #######################
VERSION_CODE=5
VERSION_NAME=2.0.3
VERSION_CODE=6
VERSION_NAME=2.0.4
#######################################################
################## Web Api Url ########################
PRODUCTION_URL_WEBAPI_BASE_URL="http://kindnesswand.com/api/v1/"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import ir.kindnesswall.BuildConfig
import ir.kindnesswall.data.local.UserInfoPref
import ir.kindnesswall.data.model.BaseDataSource
import ir.kindnesswall.data.model.UploadImageResponse
import ir.kindnesswall.data.remote.network.UploadFileApi
import ir.kindnesswall.utils.extentions.reduceImageSizeAndSave
import ir.kindnesswall.utils.wrapInBearer
import net.gotev.uploadservice.data.UploadInfo
import net.gotev.uploadservice.network.ServerResponse
import net.gotev.uploadservice.observer.request.RequestObserverDelegate
import net.gotev.uploadservice.protocols.multipart.MultipartUploadRequest
import java.io.File


class FileUploadRepo(context: Context, private val uploadFileApi: UploadFileApi) :
class FileUploadRepo(context: Context) :
BaseDataSource(context) {

fun uploadFile(
Expand All @@ -29,7 +30,10 @@ class FileUploadRepo(context: Context, private val uploadFileApi: UploadFileApi)
val request: MultipartUploadRequest =
MultipartUploadRequest(context, serverUrl = "${BuildConfig.URL_WEBAPI}/image/upload")
.setMethod("POST")
.addFileToUpload(filePath = imagePath, parameterName = "image")
.addFileToUpload(
filePath = File(imagePath).reduceImageSizeAndSave(context)!!,
parameterName = "image"
)

request.addHeader("Authorization", wrapInBearer(UserInfoPref.bearerToken))

Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/ir/kindnesswall/di/AppInjector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,5 @@ val networkModule = module {
single {
get<Retrofit>(named(baseNetworkQualifier)).create(GiftApi::class.java)
}
single {
get<Retrofit>(named(baseNetworkQualifier)).create(UploadFileApi::class.java)
}
}

2 changes: 1 addition & 1 deletion app/src/main/java/ir/kindnesswall/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ val repositoryModule = module {
single { UserRepo(androidContext(), userApi = get()) }
single { ChatRepo(androidContext(), chatApi = get()) }
single { GiftRepo(androidContext(), giftApi = get()) }
single { FileUploadRepo(androidContext(), uploadFileApi = get()) }
single { FileUploadRepo(androidContext()) }

// single { LocationHandler(get()) }
}
56 changes: 56 additions & 0 deletions app/src/main/java/ir/kindnesswall/utils/extentions/File.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ir.kindnesswall.utils.extentions

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream

/**
* @return reduced size file path address
*/
fun File.reduceImageSizeAndSave(context: Context): String? {
return try {
// BitmapFactory options to downsize the image
val file = File(this.path)
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
options.inSampleSize = 4
// factor of downsizing the image
var inputStream = FileInputStream(file)
//Bitmap selectedBitmap = null;
BitmapFactory.decodeStream(inputStream, null, options)
inputStream.close()

// The new size we want to scale to
val reduceSizePercent = 75

// Find the correct scale value. It should be the power of 2.
var scale = 1
while (options.outWidth / scale / 2 >= reduceSizePercent &&
options.outHeight / scale / 2 >= reduceSizePercent
) {
scale *= 2
}
val newOptions = BitmapFactory.Options()
newOptions.inSampleSize = scale
inputStream = FileInputStream(file)
val selectedBitmap = BitmapFactory.decodeStream(inputStream, null, newOptions)
inputStream.close()

// here i override the original image file
val rootDir = File(context.filesDir.path + File.separator + "Photos")
val newFile =
File(rootDir.path + File.separator + file.name)
if (!rootDir.exists())
rootDir.mkdir()

newFile.createNewFile()
val outputStream = FileOutputStream(newFile)
selectedBitmap!!.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
return newFile.path
} catch (e: Exception) {
null
}
}
16 changes: 13 additions & 3 deletions app/src/main/java/ir/kindnesswall/utils/imageloader/ImageLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ fun loadImage(
options: RequestOptions? = null,
progressBar: ProgressBar? = null,
forceOriginalSize: Boolean = false,
centerCrop: Boolean = true,
diskCacheStrategy: DiskCacheStrategy = DiskCacheStrategy.AUTOMATIC,
callback: ((Bitmap?, Boolean) -> Unit)? = null
) {
val glideRequest = getGlideRequest(
imageViewToLoad.context,
imageUrl
imageUrl,
centerCrop
)
setPlaceHolder(glideRequest, placeHolderId)
setOptions(glideRequest, options)
Expand Down Expand Up @@ -109,8 +111,16 @@ private fun setPlaceHolder(glideRequest: GlideRequest<Bitmap>, placeHolderId: In
}
}

private fun getGlideRequest(context: Context, imageUrl: String?) =
GlideApp.with(context).asBitmap().load(imageUrl).centerCrop()
private fun getGlideRequest(
context: Context,
imageUrl: String?,
centerCrop: Boolean
): GlideRequest<Bitmap> {
return if (centerCrop)
GlideApp.with(context).asBitmap().load(imageUrl).centerCrop()
else
GlideApp.with(context).asBitmap().load(imageUrl)
}

fun circleCropTransform() = RequestOptions.circleCropTransform()
fun roundCornerTransform(cornerRadius: Int) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ class PhotoSliderAdapter(private var isZoomable: Boolean) :

override fun onBindViewHolder(holder: PhotoSliderViewHolder, position: Int) {
holder.image.isZoomable = isZoomable
val centerCrop = isZoomable

loadImage(
images[position],
holder.image,
0,
null,
holder.progressBar,
diskCacheStrategy = DiskCacheStrategy.ALL)
centerCrop,
diskCacheStrategy = DiskCacheStrategy.ALL
)

holder.itemView.setOnClickListener {
clickListener?.invoke(holder.adapterPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class GalleryActivity : BaseActivity(), OnGalleryButtonClickListener {
private fun setupPhotoSlider() {
binding.viewModel = viewModel

binding.photoSlider.layoutParams.height = getDeviceWidth(this)
binding.photoSlider.initialize(zoomable = true, showIndicator = false)
.show(viewModel.images)

Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/layout/activity_gallery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
android:layout_height="match_parent"
android:background="@color/black">


<ir.kindnesswall.utils.widgets.photoslider.PhotoSlider
android:id="@+id/photoSlider"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
Expand All @@ -35,6 +35,12 @@
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_arrow_back_white" />

<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#55000000"
app:layout_constraintTop_toTopOf="parent" />

<com.rbrooks.indefinitepagerindicator.IndefinitePagerIndicator
android:id="@+id/photoSliderIndicator"
android:layout_width="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_photo_slider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:id="@+id/slider_appImageView_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
android:scaleType="fitCenter" />

<ProgressBar
android:id="@+id/progressBar"
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout/photo_slider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
android:background="@color/place_holder_back_color"
android:gravity="center"
android:visibility="gone"
tools:layout_height="300dp"
tools:visibility="visible">

<ImageView
Expand All @@ -31,8 +30,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
android:orientation="horizontal"
tools:layout_height="300dp" />
android:orientation="horizontal" />

<com.rbrooks.indefinitepagerindicator.IndefinitePagerIndicator
android:id="@+id/recyclerPagerIndicator"
Expand Down

0 comments on commit 3118e70

Please sign in to comment.