Skip to content

Commit bbb1860

Browse files
committed
Add download wallpaper screen and edit set wallpaper screen
1 parent fa89c39 commit bbb1860

File tree

28 files changed

+413
-82
lines changed

28 files changed

+413
-82
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
77
<uses-permission android:name="android.permission.SET_WALLPAPER" />
8+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
9+
android:maxSdkVersion="32"
10+
tools:ignore="ScopedStorage" />
11+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
12+
android:maxSdkVersion="32" />
813

914
<application
1015
android:name=".core.WalliesApp"

app/src/main/java/com/oguzdogdu/wallies/presentation/collections/CollectionsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.oguzdogdu.wallies.databinding.FragmentCollectionsBinding
1111
import com.oguzdogdu.wallies.presentation.main.MainActivity
1212
import com.oguzdogdu.wallies.util.CheckConnection
1313
import com.oguzdogdu.wallies.util.observe
14-
import com.oguzdogdu.wallies.util.setUp
14+
import com.oguzdogdu.wallies.util.setupRecyclerView
1515
import com.oguzdogdu.wallies.util.showToast
1616
import dagger.hilt.android.AndroidEntryPoint
1717
import kotlinx.coroutines.Dispatchers
@@ -32,7 +32,7 @@ class CollectionsFragment :
3232
override fun initViews() {
3333
super.initViews()
3434
binding.apply {
35-
recyclerViewCollections.setUp(
35+
recyclerViewCollections.setupRecyclerView(
3636
layoutManager = GridLayoutManager(requireContext(), 2),
3737
adapter = collectionListAdapter,
3838
true,

app/src/main/java/com/oguzdogdu/wallies/presentation/detail/DetailFragment.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.navigation.fragment.navArgs
88
import coil.load
99
import coil.request.CachePolicy
1010
import coil.transform.CircleCropTransformation
11+
import com.oguzdogdu.data.model.maindto.Urls
1112
import com.oguzdogdu.domain.model.favorites.FavoriteImages
1213
import com.oguzdogdu.domain.model.singlephoto.Photo
1314
import com.oguzdogdu.wallies.R
@@ -92,6 +93,13 @@ class DetailFragment : BaseFragment<FragmentDetailBinding>(FragmentDetailBinding
9293
showProfileInfos(it.detail)
9394
navigateToSetWallpaper(it.detail?.urls)
9495
sharePhoto(it.detail)
96+
navigateToDownloadWallpaper(
97+
raw = it.detail?.rawQuality,
98+
high = it.detail?.highQuality,
99+
medium = it.detail?.mediumQuality,
100+
low = it.detail?.lowQuality,
101+
imageTitle = it.detail?.desc
102+
)
95103
}
96104
}
97105

@@ -126,6 +134,19 @@ class DetailFragment : BaseFragment<FragmentDetailBinding>(FragmentDetailBinding
126134
}
127135
}
128136

137+
private fun navigateToDownloadWallpaper(raw:String?,high:String?,medium:String?,low:String?,imageTitle:String?){
138+
binding.buttonDownload.setOnClickListener {
139+
val arguments = Bundle().apply {
140+
putString("raw", raw)
141+
putString("high", high)
142+
putString("medium", medium)
143+
putString("low", low)
144+
putString("imageTitle", imageTitle)
145+
}
146+
navigate(R.id.toDownload, arguments)
147+
}
148+
}
149+
129150
private fun setItems(photo: Photo?) {
130151
with(binding) {
131152

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.oguzdogdu.wallies.presentation.downloadphoto
2+
3+
import android.app.DownloadManager
4+
import android.content.Context
5+
import android.net.Uri
6+
import android.os.Bundle
7+
import android.os.Environment
8+
import androidx.fragment.app.Fragment
9+
import android.view.LayoutInflater
10+
import android.view.View
11+
import android.view.ViewGroup
12+
import android.widget.Toast
13+
import androidx.navigation.fragment.navArgs
14+
import com.oguzdogdu.wallies.R
15+
import com.oguzdogdu.wallies.core.BaseBottomSheetDialogFragment
16+
import com.oguzdogdu.wallies.databinding.FragmentDownloadPhotoBinding
17+
import com.oguzdogdu.wallies.presentation.setwallpaper.SetWallpaperFragmentArgs
18+
import com.oguzdogdu.wallies.util.downloadImage
19+
import com.oguzdogdu.wallies.util.showToast
20+
import java.io.File
21+
22+
class DownloadPhotoFragment : BaseBottomSheetDialogFragment<FragmentDownloadPhotoBinding>(FragmentDownloadPhotoBinding::inflate) {
23+
24+
private val args: DownloadPhotoFragmentArgs by navArgs()
25+
26+
override fun initListeners() {
27+
super.initListeners()
28+
binding.apply {
29+
buttonRawQuality.setOnClickListener {
30+
downloadImageFromWeb(args.raw)
31+
}
32+
buttonFullQuality.setOnClickListener {
33+
downloadImageFromWeb(args.high)
34+
}
35+
buttonMediumQuality.setOnClickListener {
36+
downloadImageFromWeb(args.medium)
37+
}
38+
buttonLowQuality.setOnClickListener {
39+
downloadImageFromWeb(args.low)
40+
}
41+
}
42+
}
43+
44+
45+
private fun downloadImageFromWeb(url: String?) {
46+
val directory : String = requireContext().getString(R.string.app_name)
47+
val fileName = "${args.imageTitle}.jpg"
48+
if (url != null) {
49+
requireContext().downloadImage(url, directory, fileName)
50+
}
51+
}
52+
}

app/src/main/java/com/oguzdogdu/wallies/presentation/favorites/FavoritesFragment.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.oguzdogdu.wallies.presentation.favorites
22

33
import android.os.Bundle
44
import androidx.fragment.app.viewModels
5-
import androidx.lifecycle.lifecycleScope
65
import androidx.recyclerview.widget.GridLayoutManager
76
import androidx.recyclerview.widget.RecyclerView
87
import com.oguzdogdu.wallies.R
@@ -11,12 +10,9 @@ import com.oguzdogdu.wallies.databinding.FragmentFavoritesBinding
1110
import com.oguzdogdu.wallies.presentation.main.MainActivity
1211
import com.oguzdogdu.wallies.util.hide
1312
import com.oguzdogdu.wallies.util.observe
14-
import com.oguzdogdu.wallies.util.observeInLifecycle
15-
import com.oguzdogdu.wallies.util.setUp
13+
import com.oguzdogdu.wallies.util.setupRecyclerView
1614
import com.oguzdogdu.wallies.util.show
1715
import dagger.hilt.android.AndroidEntryPoint
18-
import kotlinx.coroutines.flow.onEach
19-
import kotlinx.coroutines.launch
2016

2117
@AndroidEntryPoint
2218
class FavoritesFragment :
@@ -29,7 +25,7 @@ class FavoritesFragment :
2925
override fun initViews() {
3026
super.initViews()
3127
binding.apply {
32-
recyclerViewFavorites.setUp(
28+
recyclerViewFavorites.setupRecyclerView(
3329
layoutManager = GridLayoutManager(requireContext(), 2),
3430
adapter = favoritesListAdapter,
3531
true, onScroll = {

app/src/main/java/com/oguzdogdu/wallies/presentation/latest/LatestFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.oguzdogdu.wallies.presentation.main.MainActivity
1212
import com.oguzdogdu.wallies.util.CheckConnection
1313
import com.oguzdogdu.wallies.util.hide
1414
import com.oguzdogdu.wallies.util.observe
15-
import com.oguzdogdu.wallies.util.setUp
15+
import com.oguzdogdu.wallies.util.setupRecyclerView
1616
import com.oguzdogdu.wallies.util.show
1717
import com.oguzdogdu.wallies.util.showToast
1818
import dagger.hilt.android.AndroidEntryPoint
@@ -33,7 +33,7 @@ class LatestFragment : BaseFragment<FragmentLatestBinding>(FragmentLatestBinding
3333
override fun initViews() {
3434
super.initViews()
3535
binding.apply {
36-
recyclerViewWallpapers.setUp(
36+
recyclerViewWallpapers.setupRecyclerView(
3737
layoutManager = GridLayoutManager(requireContext(), 2),
3838
adapter = latestWallpaperAdapter,
3939
true,

app/src/main/java/com/oguzdogdu/wallies/presentation/popular/PopularFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.oguzdogdu.wallies.presentation.main.MainActivity
1212
import com.oguzdogdu.wallies.util.CheckConnection
1313
import com.oguzdogdu.wallies.util.hide
1414
import com.oguzdogdu.wallies.util.observe
15-
import com.oguzdogdu.wallies.util.setUp
15+
import com.oguzdogdu.wallies.util.setupRecyclerView
1616
import com.oguzdogdu.wallies.util.show
1717
import com.oguzdogdu.wallies.util.showToast
1818
import dagger.hilt.android.AndroidEntryPoint
@@ -33,7 +33,7 @@ class PopularFragment : BaseFragment<FragmentPopularBinding>(FragmentPopularBind
3333
override fun initViews() {
3434
super.initViews()
3535
binding.apply {
36-
recyclerViewWallpapers.setUp(
36+
recyclerViewWallpapers.setupRecyclerView(
3737
layoutManager = GridLayoutManager(requireContext(), 2),
3838
adapter = popularWallpaperAdapter,
3939
true,

app/src/main/java/com/oguzdogdu/wallies/presentation/search/SearchFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.oguzdogdu.wallies.databinding.FragmentSearchBinding
1414
import com.oguzdogdu.wallies.util.CheckConnection
1515
import com.oguzdogdu.wallies.util.observe
1616
import com.oguzdogdu.wallies.util.observeInLifecycle
17-
import com.oguzdogdu.wallies.util.setUp
17+
import com.oguzdogdu.wallies.util.setupRecyclerView
1818
import com.oguzdogdu.wallies.util.showToast
1919
import com.oguzdogdu.wallies.util.textChanges
2020
import dagger.hilt.android.AndroidEntryPoint
@@ -37,7 +37,7 @@ class SearchFragment : BaseFragment<FragmentSearchBinding>(FragmentSearchBinding
3737
override fun initViews() {
3838
super.initViews()
3939
binding.apply {
40-
recyclerViewSearch.setUp(
40+
recyclerViewSearch.setupRecyclerView(
4141
layoutManager = GridLayoutManager(requireContext(), 2),
4242
adapter = searchWallpaperAdapter,
4343
true,

app/src/main/java/com/oguzdogdu/wallies/presentation/setwallpaper/SetWallpaperFragment.kt

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package com.oguzdogdu.wallies.presentation.setwallpaper
22

33
import android.app.WallpaperManager
44
import android.graphics.Bitmap
5+
import android.graphics.BitmapFactory
56
import android.graphics.drawable.BitmapDrawable
7+
import android.net.Uri
68
import android.os.Build
9+
import android.util.Base64
710
import androidx.annotation.RequiresApi
11+
import androidx.core.net.toUri
12+
import androidx.lifecycle.lifecycleScope
813
import androidx.navigation.fragment.navArgs
914
import coil.Coil
1015
import coil.executeBlocking
@@ -18,7 +23,13 @@ import com.oguzdogdu.wallies.R
1823
import com.oguzdogdu.wallies.core.BaseBottomSheetDialogFragment
1924
import com.oguzdogdu.wallies.databinding.FragmentSetWallpaperBinding
2025
import com.oguzdogdu.wallies.util.showToast
26+
import com.oguzdogdu.wallies.util.toBitmap
2127
import dagger.hilt.android.AndroidEntryPoint
28+
import kotlinx.coroutines.Dispatchers
29+
import kotlinx.coroutines.launch
30+
import java.io.ByteArrayOutputStream
31+
import java.net.HttpURLConnection
32+
import java.net.URL
2233

2334

2435
@AndroidEntryPoint
@@ -33,6 +44,7 @@ class SetWallpaperFragment :
3344
setWallpaperOptions(args.imageUrl)
3445
}
3546

47+
3648
@RequiresApi(Build.VERSION_CODES.N)
3749
private fun setWallpaperOptions(url: String?) {
3850
binding.apply {
@@ -56,60 +68,39 @@ class SetWallpaperFragment :
5668

5769
@RequiresApi(Build.VERSION_CODES.N)
5870
fun setWallpaperFromUrl(imageUrl: String?, isLockScreen: Boolean?) {
59-
Glide.with(requireContext())
60-
.asBitmap().load(imageUrl)
61-
.listener(object : RequestListener<Bitmap> {
62-
override fun onLoadFailed(
63-
e: GlideException?,
64-
model: Any?,
65-
target: com.bumptech.glide.request.target.Target<Bitmap>?,
66-
isFirstResource: Boolean
67-
): Boolean {
68-
return false
69-
}
70-
71-
@RequiresApi(Build.VERSION_CODES.N)
72-
override fun onResourceReady(
73-
resource: Bitmap?,
74-
model: Any?,
75-
target: com.bumptech.glide.request.target.Target<Bitmap>?,
76-
dataSource: DataSource?,
77-
isFirstResource: Boolean,
78-
): Boolean {
71+
lifecycleScope.launch(Dispatchers.IO) {
72+
val bitmap = imageUrl?.toBitmap()
73+
val wallpaperManager = WallpaperManager.getInstance(requireContext())
74+
/* TODO This field adjusts an image with a high size again according to the size of the phone.
75+
val displayMetrics = DisplayMetrics()
76+
val windowManager = context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
77+
windowManager.defaultDisplay.getMetrics(displayMetrics)
78+
val width = displayMetrics.widthPixels
79+
val height = displayMetrics.heightPixels
80+
val bitmap = Bitmap.createScaledBitmap(resource!!, width, height, true)*/
81+
try {
7982

80-
val wallpaperManager = WallpaperManager.getInstance(requireContext())
81-
/* TODO This field adjusts an image with a high size again according to the size of the phone.
82-
val displayMetrics = DisplayMetrics()
83-
val windowManager = context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
84-
windowManager.defaultDisplay.getMetrics(displayMetrics)
85-
val width = displayMetrics.widthPixels
86-
val height = displayMetrics.heightPixels
87-
val bitmap = Bitmap.createScaledBitmap(resource!!, width, height, true)*/
88-
try {
89-
resource.let {
90-
when (isLockScreen) {
91-
false -> wallpaperManager.setBitmap(
92-
it,
93-
null,
94-
true,
95-
WallpaperManager.FLAG_LOCK
96-
)
83+
when (isLockScreen) {
84+
false -> wallpaperManager.setBitmap(
85+
bitmap,
86+
null,
87+
true,
88+
WallpaperManager.FLAG_LOCK
89+
)
9790

98-
null -> wallpaperManager.setBitmap(it)
99-
true -> wallpaperManager.setBitmap(
100-
it,
101-
null,
102-
true,
103-
WallpaperManager.FLAG_SYSTEM
104-
)
105-
}
106-
}
91+
null -> wallpaperManager.setBitmap(bitmap)
92+
true -> wallpaperManager.setBitmap(
93+
bitmap,
94+
null,
95+
true,
96+
WallpaperManager.FLAG_SYSTEM
97+
)
10798

108-
} catch (e: Exception) {
109-
e.printStackTrace()
110-
}
111-
return false
11299
}
113-
}).submit()
100+
101+
} catch (e: Exception) {
102+
e.printStackTrace()
103+
}
104+
}
114105
}
115106
}

app/src/main/java/com/oguzdogdu/wallies/util/StringExtension.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.oguzdogdu.wallies.util
22

33
import android.annotation.SuppressLint
4+
import android.graphics.Bitmap
5+
import android.graphics.BitmapFactory
6+
import java.net.HttpURLConnection
7+
import java.net.URL
48
import java.text.SimpleDateFormat
59
import java.util.*
610

@@ -27,6 +31,15 @@ fun String.formatDate(
2731
}
2832
}
2933

34+
fun String.toBitmap(): Bitmap {
35+
val imageUrl = URL(this)
36+
val connection = imageUrl.openConnection() as HttpURLConnection
37+
connection.doInput = true
38+
connection.connect()
39+
val input = connection.inputStream
40+
return BitmapFactory.decodeStream(input)
41+
}
42+
3043
enum class DateFormats(val value: String) {
3144
INPUT_DATE_FORMAT("yyyy-MM-dd'T'HH:mm:ss"),
3245
OUTPUT_BASIC_DATE_FORMAT("MMMM yyyy"),

0 commit comments

Comments
 (0)