mirror of
https://github.com/hmalik144/Weather-apps.git
synced 2025-12-10 02:05:20 +00:00
- mid commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.appttude.h_mal.atlas_weather.application
|
||||
|
||||
import com.appttude.h_mal.atlas_weather.service.notification.NotificationHelper
|
||||
import com.appttude.h_mal.atlas_weather.service.notification.NotificationService
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.kodein.di.generic.singleton
|
||||
|
||||
|
||||
open class AtlasApp : AppClass() {
|
||||
|
||||
private lateinit var notificationService: NotificationService
|
||||
|
||||
override val flavourModule = super.flavourModule.copy {
|
||||
bind() from singleton {
|
||||
NotificationHelper(
|
||||
instance(),
|
||||
instance(),
|
||||
)
|
||||
}
|
||||
|
||||
bind() from singleton {
|
||||
NotificationService(this@AtlasApp).let { notificationService = it }
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
notificationService.schedulePushNotifications()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.appttude.h_mal.atlas_weather.viewmodel
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Application
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.core.app.ActivityCompat
|
||||
import com.appttude.h_mal.atlas_weather.application.BaseAppClass
|
||||
import com.appttude.h_mal.atlas_weather.base.baseViewModels.BaseAndroidViewModel
|
||||
import com.appttude.h_mal.atlas_weather.data.WeatherSource
|
||||
import com.appttude.h_mal.atlas_weather.data.location.LocationProvider
|
||||
import com.appttude.h_mal.atlas_weather.data.repository.SettingsRepository
|
||||
import com.appttude.h_mal.atlas_weather.service.notification.NotificationService
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.Locale
|
||||
|
||||
class SettingsViewModel(
|
||||
application: Application,
|
||||
private val locationProvider: LocationProvider,
|
||||
private val weatherSource: WeatherSource,
|
||||
private val settingsRepository: SettingsRepository,
|
||||
private val notificationService: NotificationService
|
||||
) : BaseAndroidViewModel(application) {
|
||||
|
||||
private fun getContext() = getApplication<BaseAppClass>().applicationContext
|
||||
|
||||
fun refreshWeatherData() {
|
||||
onStart()
|
||||
job = CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
if (ActivityCompat.checkSelfPermission(
|
||||
getContext(),
|
||||
Manifest.permission.ACCESS_COARSE_LOCATION
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
// Get location
|
||||
val latLong = locationProvider.getCurrentLatLong()
|
||||
weatherSource.forceFetchWeather(latLong)
|
||||
}
|
||||
val units = settingsRepository.getUnitType().name.lowercase(Locale.ROOT)
|
||||
onSuccess("Units have been changes to $units")
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
onError(e.message ?: "Retrieving weather failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun toggleNotifications() {
|
||||
if (notificationService.areNotificationsEnabled()) {
|
||||
notificationService.unschedulePushNotifications()
|
||||
} else {
|
||||
notificationService.schedulePushNotifications()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
open class MonoApp : AppClass() {
|
||||
|
||||
override val flavourModule = super.flavourModule.copy {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user