Skip to content

Commit

Permalink
feat: download
Browse files Browse the repository at this point in the history
  • Loading branch information
yveskalume committed Apr 11, 2023
1 parent ef406ff commit b8a581d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 105 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Gif App is an Android application built using jetpack compose and other modern A
<p align="center">
<img src="/preview/preview1.gif" width="32%"/>
<img src="/preview/preview2.gif" width="32%"/>
<img src="/preview/preview3.gif" width="32%"/>
</p>

## Download
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,23 @@ fun CustomImageView(
modifier: Modifier = Modifier,
imageUrl: String,
contentScale: ContentScale = ContentScale.Crop,
onImageLoaded: (Bitmap?) -> Unit = {}
) {
val context = LocalContext.current
val dispatcher = Dispatchers.IO.limitedParallelism(5)

SubcomposeAsyncImage(
modifier = Modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 300.dp)
.padding(16.dp)
.clip(RoundedCornerShape(24.dp))
.then(modifier),
.fillMaxWidth()
.defaultMinSize(minHeight = 300.dp)
.padding(16.dp)
.clip(RoundedCornerShape(24.dp))
.then(modifier),
model = imageUrl,
loading = {
Box(
modifier = Modifier
.fillMaxSize()
.shimmer()
.fillMaxSize()
.shimmer()
)
},
imageLoader = ImageLoader.Builder(context)
Expand All @@ -64,9 +63,5 @@ fun CustomImageView(
contentDescription = null,
contentScale = contentScale,
alignment = Alignment.Center,
onSuccess = {
val bitmap = it.result.drawable.toBitmap()
onImageLoaded(bitmap)
}
)
}
81 changes: 36 additions & 45 deletions app/src/main/java/com/yvkalume/gifapp/ui/components/GifItem.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yvkalume.gifapp.ui.components

import android.graphics.Bitmap
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -12,59 +12,50 @@ import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Share
import androidx.compose.material.icons.rounded.Download
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.yvkalume.gifapp.domain.entity.Gif
import com.yvkalume.gifapp.util.saveAndShare
import com.yvkalume.gifapp.ui.util.downloadFile

@Composable
fun GifItem(gif: Gif, modifier: Modifier = Modifier, onFavoriteClick: (Gif) -> Unit) {

val context = LocalContext.current

var imageDrawable by remember {
mutableStateOf<Bitmap?>(null)
}

Column(modifier = Modifier.wrapContentHeight()) {
CustomImageView(
modifier = modifier,
imageUrl = gif.imageUrl,
contentScale = ContentScale.FillWidth,
onImageLoaded = { imageDrawable = it }
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
LikeIcon(
isChecked = gif.isFavorite,
onClick = { onFavoriteClick(gif) },
modifier = Modifier.size(52.dp)
)

IconButton(
onClick = {
imageDrawable.saveAndShare(context, gif.title)
}
) {
Icon(imageVector = Icons.Rounded.Share, contentDescription = null)
}

}
}

Divider(modifier = Modifier.padding(vertical = 16.dp))
val context = LocalContext.current

Column(modifier = Modifier.wrapContentHeight()) {
CustomImageView(
modifier = modifier,
imageUrl = gif.imageUrl,
contentScale = ContentScale.FillWidth,
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
LikeIcon(
isChecked = gif.isFavorite,
onClick = { onFavoriteClick(gif) },
modifier = Modifier.size(52.dp)
)

IconButton(
onClick = {
Toast.makeText(context,"Download started",Toast.LENGTH_LONG).show()
context.downloadFile(gif.imageUrl, gif.title)
}
) {
Icon(imageVector = Icons.Rounded.Download, contentDescription = null)
}
}
}

Divider(modifier = Modifier.padding(vertical = 16.dp))
}
82 changes: 36 additions & 46 deletions app/src/main/java/com/yvkalume/gifapp/ui/components/StickerItem.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yvkalume.gifapp.ui.components

import android.graphics.Bitmap
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -12,64 +12,54 @@ import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Share
import androidx.compose.material.icons.rounded.Download
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.yvkalume.gifapp.domain.entity.Sticker
import com.yvkalume.gifapp.util.saveAndShare
import com.yvkalume.gifapp.ui.util.downloadFile

@Composable
fun StickerItem(
sticker: Sticker,
modifier: Modifier = Modifier,
onFavoriteClick: (Sticker) -> Unit
sticker: Sticker,
modifier: Modifier = Modifier,
onFavoriteClick: (Sticker) -> Unit
) {

val context = LocalContext.current
val context = LocalContext.current

var imageDrawable by remember {
mutableStateOf<Bitmap?>(null)
}
Column(modifier = Modifier.wrapContentHeight()) {
CustomImageView(
modifier = modifier,
imageUrl = sticker.imageUrl,
contentScale = ContentScale.FillHeight,
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
LikeIcon(
isChecked = sticker.isFavorite,
onClick = { onFavoriteClick(sticker) },
modifier = Modifier.size(52.dp)
)

Column(modifier = Modifier.wrapContentHeight()) {
CustomImageView(
modifier = modifier,
imageUrl = sticker.imageUrl,
contentScale = ContentScale.FillHeight,
onImageLoaded = {
imageDrawable = it
}
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
LikeIcon(
isChecked = sticker.isFavorite,
onClick = { onFavoriteClick(sticker) },
modifier = Modifier.size(52.dp)
)
IconButton(
onClick = {
Toast.makeText(context,"Download started", Toast.LENGTH_LONG).show()
context.downloadFile(sticker.imageUrl, sticker.title)
}
) {
Icon(imageVector = Icons.Rounded.Download, contentDescription = null)
}
}
}

IconButton(
onClick = {
imageDrawable.saveAndShare(context, sticker.title)
}
) {
Icon(imageVector = Icons.Rounded.Share, contentDescription = null)
}
}
}

Divider(modifier = Modifier.padding(vertical = 16.dp))
Divider(modifier = Modifier.padding(vertical = 16.dp))
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun HomeScreen(homeViewModel: HomeViewModel = mavericksViewModel()) {
) {
Text(
text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.h1,
style = MaterialTheme.typography.h2,
modifier = Modifier.padding(top = 16.dp)
)

Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/yvkalume/gifapp/ui/util/Extension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yvkalume.gifapp.ui.util

import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Environment

fun Context.downloadFile(link: String,fileName: String): Long {
val request = DownloadManager.Request(Uri.parse(link))
val params = DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
request.setAllowedNetworkTypes(params)
.setTitle("Gif app")
.setDescription("Downloadin a gif")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
val manager = this.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

return manager.enqueue(request)
}
Binary file modified preview/preview1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified preview/preview2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed preview/preview3.gif
Binary file not shown.

0 comments on commit b8a581d

Please sign in to comment.