feat: 添加删除收藏功能

This commit is contained in:
LUX-Timber 2024-04-24 17:05:27 +08:00
parent 5fb27f8d85
commit e4345888f2
13 changed files with 254 additions and 13 deletions

View File

@ -15,7 +15,7 @@
</deviceKey> </deviceKey>
</Target> </Target>
</targetSelectedWithDropDown> </targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-04-24T03:32:20.457610Z" /> <timeTargetWasSelectedWithDropDown value="2024-04-24T08:47:36.516818Z" />
</State> </State>
</entry> </entry>
</value> </value>

View File

@ -0,0 +1,30 @@
package com.timber.soft.mylivewallpaper.data
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
class CollectViewModel : ViewModel() {
private var wallpaperDataList: MutableLiveData<List<WallpaperData>> =
MutableLiveData<List<WallpaperData>>()
init {
viewModelScope.launch {
wallpaperDataList.value = AppDatabase.dataBase.getWallpaperDao().getCollectData()
}
}
fun update() {
viewModelScope.launch {
wallpaperDataList.value = AppDatabase.dataBase.getWallpaperDao().getCollectData()
}
}
fun getList() = wallpaperDataList
override fun onCleared() {
super.onCleared()
}
}

View File

@ -3,18 +3,23 @@ package com.timber.soft.mylivewallpaper.data
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Delete import androidx.room.Delete
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import androidx.room.Update import androidx.room.Update
@Dao @Dao
interface WallpaperDao { interface WallpaperDao {
@Query("delete from t_wallpaper")
suspend fun deleteAllData()
@Query("select * from t_wallpaper where isCollect = :collect ") @Query("select * from t_wallpaper where isCollect = :collect ")
suspend fun getCollectData(collect: Boolean = true): List<WallpaperData> suspend fun getCollectData(collect: Boolean = true): List<WallpaperData>
@Update @Update
suspend fun updateData(wallpaperData: WallpaperData) suspend fun updateData(wallpaperData: WallpaperData)
@Insert @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertData(wallpaperData: WallpaperData) suspend fun insertData(wallpaperData: WallpaperData)
@Delete @Delete

View File

@ -12,7 +12,7 @@ data class WallpaperData(
val title: String, val title: String,
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val id: Int = 0, var id: Int = 0,
val classId: String? = null, val classId: String? = null,
var isCollect: Boolean = false, var isCollect: Boolean = false,
var downloadUrl: String? = null var downloadUrl: String? = null

View File

@ -6,5 +6,6 @@ object AppFinalString {
const val DB_VERSION = 1 const val DB_VERSION = 1
const val TABLE_NAME_WALLPAPER = "t_wallpaper" const val TABLE_NAME_WALLPAPER = "t_wallpaper"
const val KEY_EXTRA = "KEY_EXTRA" const val KEY_EXTRA = "KEY_EXTRA"
const val ACTION_DATABASE_UPDATED = "ACTION_DATABASE_UPDATED"
} }

View File

@ -34,6 +34,7 @@ class MainActivity :
binding.mainViewpager.run { binding.mainViewpager.run {
adapter = MainViewPagerAdapter(viewPagerFragments, supportFragmentManager) adapter = MainViewPagerAdapter(viewPagerFragments, supportFragmentManager)
addOnPageChangeListener(this@MainActivity) addOnPageChangeListener(this@MainActivity)
setOffscreenPageLimit(0)
} }
setTabSelect(0) setTabSelect(0)
} }

View File

@ -0,0 +1,63 @@
package com.timber.soft.mylivewallpaper.ui.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.timber.soft.mylivewallpaper.R
import com.timber.soft.mylivewallpaper.data.WallpaperData
import com.timber.soft.mylivewallpaper.tools.AppFinalString
import com.timber.soft.mylivewallpaper.ui.activity.DetailActivity
class CollectAdapter(
private val context: Context,
) : RecyclerView.Adapter<CollectAdapter.CollectItemViewHolder>() {
private var wallpaperDataList: List<WallpaperData> = emptyList()
fun updateData(dataList: List<WallpaperData>) {
wallpaperDataList = dataList
notifyDataSetChanged()
}
inner class CollectItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val wallpaperItem = itemView.findViewById<ImageView>(R.id.item_home_img)
val rootView = itemView.findViewById<CardView>(R.id.item_home_root)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectItemViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_home_wallpaper, parent,false)
return CollectItemViewHolder(view)
}
override fun getItemCount(): Int {
return wallpaperDataList.size
}
override fun onBindViewHolder(holder: CollectItemViewHolder, position: Int) {
wallpaperDataList[position].run {
try {
Glide.with(context).load(thumbnail)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.transition(DrawableTransitionOptions.withCrossFade()).apply(
RequestOptions().placeholder(R.drawable.img_loading)
).error(R.drawable.img_loading_err).into(holder.wallpaperItem)
} catch (e: Exception) {
e.printStackTrace()
}
holder.rootView.setOnClickListener() {
context.startActivity(Intent(context, DetailActivity::class.java).apply {
putExtra(AppFinalString.KEY_EXTRA, this@run)
})
}
}
}
}

View File

@ -1,12 +1,75 @@
package com.timber.soft.mylivewallpaper.ui.fragment package com.timber.soft.mylivewallpaper.ui.fragment
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.timber.soft.mylivewallpaper.data.CollectViewModel
import com.timber.soft.mylivewallpaper.databinding.FragmentCollectBinding import com.timber.soft.mylivewallpaper.databinding.FragmentCollectBinding
import com.timber.soft.mylivewallpaper.tools.AppFinalString
import com.timber.soft.mylivewallpaper.ui.adapter.CollectAdapter
class CollectFragment : BaseFragment() { class CollectFragment : BaseFragment() {
private lateinit var binding: FragmentCollectBinding private lateinit var binding: FragmentCollectBinding
private lateinit var collectAdapter: CollectAdapter
private lateinit var collectViewModel: CollectViewModel
override fun getFragmentContentView(): View { override fun getFragmentContentView(): View {
binding = FragmentCollectBinding.inflate(layoutInflater) binding = FragmentCollectBinding.inflate(layoutInflater)
return binding.root return binding.root
} }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val filter = IntentFilter(AppFinalString.ACTION_DATABASE_UPDATED)
LocalBroadcastManager.getInstance(requireContext())
.registerReceiver(mDatabaseUpdatedReceiver, filter)
}
override fun initViews() {
super.initViews()
collectAdapter = CollectAdapter(requireContext())
binding.recyclerCollect.run {
adapter = collectAdapter
layoutManager = StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL)
}
}
override fun onResume() {
super.onResume()
collectViewModel.update()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
collectViewModel = ViewModelProvider(this)[CollectViewModel::class.java]
collectViewModel.getList().observe(viewLifecycleOwner, Observer {
collectAdapter.updateData(it)
})
}
override fun onDestroyView() {
super.onDestroyView()
LocalBroadcastManager.getInstance(requireContext())
.unregisterReceiver(mDatabaseUpdatedReceiver)
}
private val mDatabaseUpdatedReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent != null) {
if (intent.action.equals(AppFinalString.ACTION_DATABASE_UPDATED)) {
collectViewModel.update()
}
}
}
}
} }

View File

@ -7,8 +7,15 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import android.widget.Toast
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.timber.soft.mylivewallpaper.R import com.timber.soft.mylivewallpaper.R
import com.timber.soft.mylivewallpaper.data.AppDatabase
import com.timber.soft.mylivewallpaper.databinding.FragmentSettingBinding import com.timber.soft.mylivewallpaper.databinding.FragmentSettingBinding
import com.timber.soft.mylivewallpaper.tools.AppFinalString
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class SettingFragment : BaseFragment() { class SettingFragment : BaseFragment() {
private lateinit var binding: FragmentSettingBinding private lateinit var binding: FragmentSettingBinding
@ -19,17 +26,11 @@ class SettingFragment : BaseFragment() {
override fun initViews() { override fun initViews() {
super.initViews() super.initViews()
binding.setLayoutRating.setOnClickListener { initButton()
RateFragment.newInstance(0, 0).show(childFragmentManager, "") initInfoCard()
} }
binding.setLayoutShare.setOnClickListener {
val url = getString(R.string.set_shop_link) + (activity?.packageName ?: "")
val intent = Intent(Intent.ACTION_SEND)
intent.setType("text/plain")
intent.putExtra(Intent.EXTRA_TEXT, url)
startActivity(intent)
}
private fun initInfoCard() {
val pInfo: PackageInfo val pInfo: PackageInfo
try { try {
pInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { pInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@ -45,4 +46,34 @@ class SettingFragment : BaseFragment() {
binding.setAppVersion.text = "Version: " + "" binding.setAppVersion.text = "Version: " + ""
} }
} }
private fun initButton() {
binding.setLayoutRating.setOnClickListener {
RateFragment.newInstance(0, 0).show(childFragmentManager, "")
}
binding.setLayoutShare.setOnClickListener {
val url = getString(R.string.set_shop_link) + (activity?.packageName ?: "")
val intent = Intent(Intent.ACTION_SEND)
intent.setType("text/plain")
intent.putExtra(Intent.EXTRA_TEXT, url)
startActivity(intent)
}
binding.setLayoutDelete.setOnClickListener() {
CoroutineScope(Dispatchers.IO).launch {
AppDatabase.dataBase.getWallpaperDao().deleteAllData()
}
sendDatabaseUpdatedBroadcast()
Toast.makeText(
requireActivity(), "Cleared all collections successfully.", Toast.LENGTH_SHORT
).show()
}
}
private fun sendDatabaseUpdatedBroadcast() {
val intent = Intent(AppFinalString.ACTION_DATABASE_UPDATED)
LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(intent)
}
} }

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M857.7,269.8L702.7,269.8C692.7,189 619.4,128 532.4,128S372.1,189 362,269.8L208.9,269.8c-21.1,0 -38.3,16 -38.3,35.8 0,19.8 17.2,35.8 38.3,35.8h55.5v518.8c0,19.8 17.2,35.8 38.3,35.8L762,896c21.2,0 38.3,-16 38.3,-35.8L800.3,341.4h57.4c21.1,0 38.3,-16 38.3,-35.8 0,-19.7 -17.2,-35.8 -38.3,-35.8zM532.4,198.2c45.4,0 84.7,29.9 93.7,71.6L438.6,269.8c9.1,-41.6 48.3,-71.6 93.8,-71.6zM417.5,735c0,19.8 -17.1,35.8 -38.3,35.8 -21.1,0 -38.3,-16 -38.3,-35.8v-286.3c0,-19.8 17.2,-35.8 38.3,-35.8 21.2,0 38.3,16 38.3,35.8v286.3zM570.7,735c0,19.8 -17.2,35.8 -38.3,35.8 -21.2,0 -38.3,-16 -38.3,-35.8v-286.3c0,-19.8 17.2,-35.8 38.3,-35.8 21.1,0 38.3,16 38.3,35.8v286.3zM723.8,735c0,19.8 -17.2,35.8 -38.3,35.8s-38.3,-16 -38.3,-35.8v-286.3c0,-19.8 17.2,-35.8 38.3,-35.8s38.3,16 38.3,35.8v286.3z"
android:fillColor="#2c2c2c"/>
</vector>

View File

@ -26,6 +26,8 @@
android:id="@+id/recycler_collect" android:id="@+id/recycler_collect"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="14dp"
android:layout_marginEnd="14dp"
android:paddingTop="4dp" /> android:paddingTop="4dp" />
</LinearLayout> </LinearLayout>

View File

@ -145,4 +145,39 @@
</androidx.cardview.widget.CardView> </androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="28dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="28dp"
app:cardCornerRadius="12dp">
<LinearLayout
android:id="@+id/set_layout_delete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/set_img_delete"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginStart="25dp"
android:src="@drawable/svg_delete" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="@string/set_setting_delete"
android:textColor="@color/black"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout> </LinearLayout>

View File

@ -12,6 +12,7 @@
<string name="set_rate_us_content">We hope this app is useful for you, if it does, would youplease give us a 5 sar and a mice revtew on Google Play, it really helps!</string> <string name="set_rate_us_content">We hope this app is useful for you, if it does, would youplease give us a 5 sar and a mice revtew on Google Play, it really helps!</string>
<string name="set_cancel">CANCEL</string> <string name="set_cancel">CANCEL</string>
<string name="set_rate_it">RATE IT</string> <string name="set_rate_it">RATE IT</string>
<string name="set_setting_delete">Clear All My Favorites</string>
<string name="collect_title">My Collection</string> <string name="collect_title">My Collection</string>