ソースを参照

Add proper migration for handling home_apps > 6 (#56)

Joshua Kuestersteffen 4 年 前
コミット
3a6025bd41

+ 11 - 1
app/src/main/java/com/sduduzog/slimlauncher/data/BaseDatabase.kt

@@ -8,7 +8,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
 import com.sduduzog.slimlauncher.models.HomeApp
 
 
-@Database(entities = [HomeApp::class], version = 8, exportSchema = false)
+@Database(entities = [HomeApp::class], version = 9, exportSchema = false)
 abstract class BaseDatabase : RoomDatabase() {
 
     abstract fun baseDao(): BaseDao
@@ -83,5 +83,15 @@ abstract class BaseDatabase : RoomDatabase() {
                 database.execSQL("ALTER TABLE home_apps_copy RENAME TO home_apps")
             }
         }
+        val MIGRATION_8_9 = object : Migration(8, 9) {
+            override fun migrate(database: SupportSQLiteDatabase) {
+                val userSerial = Process.myUserHandle().hashCode().toLong()
+                val statement =
+                    database.compileStatement("DELETE FROM home_apps WHERE user_serial = ? AND package_name NOT IN (SELECT package_name FROM home_apps WHERE user_serial = ? ORDER BY sorting_index ASC limit 6)")
+                statement.bindLong(1, userSerial)
+                statement.bindLong(2, userSerial)
+                statement.execute()
+            }
+        }
     }
 }

+ 2 - 1
app/src/main/java/com/sduduzog/slimlauncher/di/AppModule.kt

@@ -25,7 +25,8 @@ class AppModule {
                         BaseDatabase.MIGRATION_4_5,
                         BaseDatabase.MIGRATION_5_6,
                         BaseDatabase.MIGRATION_6_7,
-                        BaseDatabase.MIGRATION_7_8
+                        BaseDatabase.MIGRATION_7_8,
+                        BaseDatabase.MIGRATION_8_9
                 )
                 .build()
     }

+ 0 - 5
app/src/main/java/com/sduduzog/slimlauncher/models/MainViewModel.kt

@@ -25,9 +25,4 @@ class MainViewModel @Inject constructor(baseDao: BaseDao) : ViewModel() {
         val index = _apps.value!!.size
         _baseRepository.add(HomeApp.from(app, index))
     }
-
-    // This is needed temporarily so that the HomeFragment can transition the max number of home apps
-    fun remove(app: HomeApp) {
-        _baseRepository.remove(app)
-    }
 }

+ 0 - 9
app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt

@@ -4,7 +4,6 @@ import android.app.Activity
 import android.content.*
 import android.content.pm.LauncherApps
 import android.os.Bundle
-import android.os.Process
 import android.os.UserManager
 import android.provider.AlarmClock
 import android.provider.CalendarContract
@@ -19,7 +18,6 @@ import androidx.constraintlayout.motion.widget.MotionLayout
 import androidx.constraintlayout.motion.widget.MotionLayout.TransitionListener
 import androidx.lifecycle.Observer
 import androidx.navigation.Navigation
-import com.sduduzog.slimlauncher.BuildConfig
 import com.sduduzog.slimlauncher.R
 import com.sduduzog.slimlauncher.adapters.AddAppAdapter
 import com.sduduzog.slimlauncher.adapters.HomeAdapter
@@ -60,13 +58,6 @@ class HomeFragment(private val viewModel: MainViewModel) : BaseFragment(), OnLau
                 adapter2.setItems(apps.filter {
                     it.sortingIndex >= 3
                 })
-
-                // Since the app previously supported more than 6 apps, we need this as a transition to only
-                // allowing 6 home apps. This can be removed in the future when it is likely everyone has
-                // upgraded to a version that only supports 6 home apps.
-                if(apps.size > 6) {
-                    apps.subList(6, apps.size).forEach(viewModel::remove)
-                }
             }
         })