|
| 1 | +/* |
| 2 | + * Copyright 2025 Clifford Liu |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cliuff.boundo.wear.conf |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.content.SharedPreferences |
| 21 | +import androidx.core.content.edit |
| 22 | + |
| 23 | +interface SelfUpdater { |
| 24 | + val maxVersion: Int |
| 25 | + fun apply(oldVersion: Int, prefSettings: SharedPreferences) |
| 26 | +} |
| 27 | + |
| 28 | +internal object MiscMain { |
| 29 | + private const val APPLICATION_V = "v" // int |
| 30 | + |
| 31 | + fun applyAppVersionUpgrade(context: Context, prefSettings: SharedPreferences) { |
| 32 | + val verDefault = -1 |
| 33 | + val verOri = prefSettings.getInt(APPLICATION_V, verDefault) |
| 34 | + val updaters = listOf(SelfUpdater25()) |
| 35 | + val ver = updaters.last().maxVersion |
| 36 | + // below: update registered version |
| 37 | + prefSettings.edit { putInt(APPLICATION_V, ver) } |
| 38 | + // below: app gone through update process |
| 39 | + if (verOri == ver) return |
| 40 | + // below: app in update process to the newest |
| 41 | + updaters.forEach { updater -> |
| 42 | + if (verOri in 0..<updater.maxVersion) updater.apply(verOri, prefSettings) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments