Bimba.git

ref: d00bd3785dc9612da192dbed7a1d4f77976ebd0f

app/src/main/java/xyz/apiote/bimba/czwek/onboarding/FirstRunActivity.kt


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.onboarding

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.preference.PreferenceManager
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.dashboard.MainActivity
import xyz.apiote.bimba.czwek.repo.migrateDB
import xyz.apiote.bimba.czwek.settings.DownloadCitiesWorker
import xyz.apiote.bimba.czwek.settings.feeds.migrateFeedsSettings
import java.time.Instant
import java.time.temporal.ChronoUnit

class FirstRunActivity : AppCompatActivity() {
	override fun onCreate(savedInstanceState: Bundle?) {
		installSplashScreen()
		super.onCreate(savedInstanceState)

		val preferences = getSharedPreferences("shp", MODE_PRIVATE)

		migrateFeedsSettings(this)
		migrateDB(this)
		createNotificationChannels()

		val (updatesEnabled, weekPassed) = PreferenceManager.getDefaultSharedPreferences(this).let {
			arrayOf(
				it.getBoolean("autoupdate_cities_list", false),
				Instant.ofEpochSecond(it.getLong("cities_last_update", 0)).plus(7, ChronoUnit.DAYS)
					.isBefore(Instant.now())
			)
		}

		if (updatesEnabled && weekPassed) {
			WorkManager.getInstance(this)
				.enqueue(OneTimeWorkRequest.from(DownloadCitiesWorker::class.java))
		}

		val intent = if (preferences.getBoolean("firstRun", true)) {
			Intent(this, OnboardingActivity::class.java)
		} else {
			Intent(this, MainActivity::class.java)
		}
		startActivity(intent)
		finish()
	}

	private fun createNotificationChannels() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			val name = getString(R.string.cities_channel_name)
			val descriptionText = getString(R.string.cities_channel_description)
			val importance = NotificationManager.IMPORTANCE_LOW
			val channel = NotificationChannel("cities_channel", name, importance).apply {
				description = descriptionText
			}
			val notificationManager: NotificationManager =
				getSystemService(NOTIFICATION_SERVICE) as NotificationManager
			notificationManager.createNotificationChannel(channel)
		}
	}
}