diff --git a/.gitignore b/.gitignore index 39fb081..f93a34c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /build /captures .externalNativeBuild +/projectFilesBackup diff --git a/app/.gitignore b/app/.gitignore index 67e07b8..f588c7d 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1,2 +1,4 @@ /build /release +/atlasWeather +/monoWeather \ No newline at end of file diff --git a/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/MainActivity.kt b/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/MainActivity.kt index 0100fe6..f6854d2 100644 --- a/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/MainActivity.kt +++ b/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/MainActivity.kt @@ -15,7 +15,7 @@ import com.google.android.material.bottomnavigation.BottomNavigationView import kotlinx.android.synthetic.atlasWeather.activity_main.* -class MainActivity : BaseActivity() { +class MainActivity : BaseActivity(){ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/home/HomeFragment.kt b/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/home/HomeFragment.kt index ec3ae05..ec3f633 100644 --- a/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/home/HomeFragment.kt +++ b/app/src/atlasWeather/java/com/appttude/h_mal/atlas_weather/atlasWeather/ui/home/HomeFragment.kt @@ -57,13 +57,13 @@ class HomeFragment : BaseFragment(), KodeinAware { adapter = recyclerAdapter } - getPermissionResult(Manifest.permission.ACCESS_COARSE_LOCATION, LOCATION_PERMISSION_REQUEST){ + getPermissionResult(Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_PERMISSION_REQUEST){ viewModel.fetchData() } swipe_refresh.apply { setOnRefreshListener { - getPermissionResult(Manifest.permission.ACCESS_COARSE_LOCATION, LOCATION_PERMISSION_REQUEST){ + getPermissionResult(Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_PERMISSION_REQUEST){ viewModel.fetchData() } isRefreshing = true diff --git a/app/src/atlasWeather/res/values/styles.xml b/app/src/atlasWeather/res/values/styles.xml index 79b99b4..9fcabf4 100644 --- a/app/src/atlasWeather/res/values/styles.xml +++ b/app/src/atlasWeather/res/values/styles.xml @@ -6,7 +6,6 @@ @android:color/black @color/colour_four @color/colour_one - @drawable/gradient sans-serif-light @color/colorAccent @@ -14,17 +13,14 @@ diff --git a/app/src/main/java/com/appttude/h_mal/atlas_weather/data/location/LocationProviderImpl.kt b/app/src/main/java/com/appttude/h_mal/atlas_weather/data/location/LocationProviderImpl.kt index 2c4c88c..a26356b 100644 --- a/app/src/main/java/com/appttude/h_mal/atlas_weather/data/location/LocationProviderImpl.kt +++ b/app/src/main/java/com/appttude/h_mal/atlas_weather/data/location/LocationProviderImpl.kt @@ -68,7 +68,7 @@ class LocationProviderImpl( @SuppressLint("MissingPermission") private suspend fun getAFreshLocation(): Location? { - return client.getCurrentLocation(PRIORITY_HIGH_ACCURACY, object : CancellationToken() { + return client.getCurrentLocation(PRIORITY_LOW_POWER, object : CancellationToken() { override fun isCancellationRequested(): Boolean = false override fun onCanceledRequested(p0: OnTokenCanceledListener): CancellationToken = this }).await() diff --git a/app/src/main/java/com/appttude/h_mal/atlas_weather/helper/ServicesHelper.kt b/app/src/main/java/com/appttude/h_mal/atlas_weather/helper/ServicesHelper.kt index 0207c56..f7b2196 100644 --- a/app/src/main/java/com/appttude/h_mal/atlas_weather/helper/ServicesHelper.kt +++ b/app/src/main/java/com/appttude/h_mal/atlas_weather/helper/ServicesHelper.kt @@ -11,8 +11,10 @@ import com.appttude.h_mal.atlas_weather.data.repository.SettingsRepository import com.appttude.h_mal.atlas_weather.data.room.entity.CURRENT_LOCATION import com.appttude.h_mal.atlas_weather.data.room.entity.EntityItem import com.appttude.h_mal.atlas_weather.model.weather.FullWeather +import com.appttude.h_mal.atlas_weather.model.widget.InnerWidgetCellData import com.appttude.h_mal.atlas_weather.model.widget.InnerWidgetData import com.appttude.h_mal.atlas_weather.model.widget.WidgetData +import com.appttude.h_mal.atlas_weather.model.widget.WidgetWeatherCollection import com.appttude.h_mal.atlas_weather.utils.toSmallDayName import com.squareup.picasso.Picasso import com.squareup.picasso.Target @@ -92,6 +94,36 @@ class ServicesHelper( } } + suspend fun getWidgetWeatherCollection(): WidgetWeatherCollection? { + return try { + val result = repository.loadSingleCurrentWeatherFromRoom(CURRENT_LOCATION) + + val widgetData = result.weather.let { + val bitmap = it.current?.icon + val location = locationProvider.getLocationNameFromLatLong(it.lat, it.lon) + val temp = it.current?.temp?.toInt().toString() + + WidgetData(location, bitmap, temp) + } + + val list = mutableListOf() + + result.weather.daily?.drop(1)?.dropLast(2)?.forEach { dailyWeather -> + val day = dailyWeather.dt?.toSmallDayName() + val icon = dailyWeather.icon + val temp = dailyWeather.max?.toInt().toString() + + val item = InnerWidgetCellData(day, icon, temp) + list.add(item) + } + list.toList() + + WidgetWeatherCollection(widgetData, list) + } catch (e: Exception) { + null + } + } + private suspend fun getBitmapFromUrl(imageAddress: String?): Bitmap? { return suspendCoroutine { cont -> Picasso.get().load(imageAddress).into(object : Target { diff --git a/app/src/main/java/com/appttude/h_mal/atlas_weather/model/widget/WidgetData.kt b/app/src/main/java/com/appttude/h_mal/atlas_weather/model/widget/WidgetData.kt index c13d006..ba20936 100644 --- a/app/src/main/java/com/appttude/h_mal/atlas_weather/model/widget/WidgetData.kt +++ b/app/src/main/java/com/appttude/h_mal/atlas_weather/model/widget/WidgetData.kt @@ -12,4 +12,16 @@ data class InnerWidgetData( val date: String?, val icon: Bitmap?, val highTemp: String? +) + + +data class InnerWidgetCellData( + val date: String?, + val icon: String?, + val highTemp: String? +) + +data class WidgetWeatherCollection( + val widgetData: WidgetData, + val forecast: List ) \ No newline at end of file diff --git a/app/src/main/res/drawable/location_permission_decl.png b/app/src/main/res/drawable/location_permission_decl.png new file mode 100644 index 0000000..2f9b653 Binary files /dev/null and b/app/src/main/res/drawable/location_permission_decl.png differ diff --git a/app/src/main/res/layout/weather_app_widget.xml b/app/src/main/res/layout/weather_app_widget.xml index 7c23e75..677fc44 100644 --- a/app/src/main/res/layout/weather_app_widget.xml +++ b/app/src/main/res/layout/weather_app_widget.xml @@ -1,25 +1,27 @@ - + android:layout_marginStart="12dp" + android:orientation="vertical"> + + style="@style/widget_light_home_text" + tools:text="Hammersmith Bridge" /> @@ -128,23 +123,172 @@ - - - + android:layout_weight="1" + android:columnCount="5" + android:rowCount="1"> + + + + + + + + - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/widget_item.xml b/app/src/main/res/layout/widget_item.xml index 49c7bad..dd91aa8 100644 --- a/app/src/main/res/layout/widget_item.xml +++ b/app/src/main/res/layout/widget_item.xml @@ -3,9 +3,8 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/widget_item_layout" android:layout_width="wrap_content" - android:layout_height="match_parent" - android:orientation="vertical" - android:minHeight="55dp"> + android:layout_height="wrap_content" + android:orientation="vertical"> @@ -35,6 +39,10 @@ android:gravity="center" android:textColor="#ffffff" android:textSize="12sp" + android:autoSizeMaxTextSize="100sp" + android:autoSizeMinTextSize="8sp" + android:autoSizeStepGranularity="2sp" + android:autoSizeTextType="uniform" tools:text="20" /> diff --git a/app/src/main/res/values-v26/styles.xml b/app/src/main/res/values-v26/styles.xml new file mode 100644 index 0000000..0a083af --- /dev/null +++ b/app/src/main/res/values-v26/styles.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 266bedc..7f0d76a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -5,5 +5,6 @@ Refer to App Widget Documentation for margin information http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout --> - + 16dp + 16dp diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 2b7eb93..b85a0a8 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -8,4 +8,23 @@ 12dp 2 + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/new_app_widget_info.xml b/app/src/main/res/xml/new_app_widget_info.xml index e5fd003..576478e 100644 --- a/app/src/main/res/xml/new_app_widget_info.xml +++ b/app/src/main/res/xml/new_app_widget_info.xml @@ -1,10 +1,11 @@ - + - - @@ -26,11 +26,16 @@ - + + + + + + @@ -43,17 +48,13 @@ android:resource="@xml/new_app_widget_info" /> - - - - - + \ No newline at end of file diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/BaseFragment.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/BaseFragment.kt index 641dfac..78df8fb 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/BaseFragment.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/BaseFragment.kt @@ -24,7 +24,7 @@ import org.kodein.di.android.x.kodein import org.kodein.di.generic.instance import kotlin.properties.Delegates -abstract class BaseFragment : Fragment(), KodeinAware { +abstract class BaseFragment() : Fragment(), KodeinAware { override val kodein by kodein() val factory by instance() diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/details/FurtherInfoFragment.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/details/FurtherInfoFragment.kt index afb2f2f..4ba7fa8 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/details/FurtherInfoFragment.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/details/FurtherInfoFragment.kt @@ -7,11 +7,9 @@ import android.view.ViewGroup import androidx.fragment.app.Fragment import com.appttude.h_mal.atlas_weather.R import com.appttude.h_mal.atlas_weather.model.forecast.Forecast -import com.appttude.h_mal.atlas_weather.monoWeather.ui.details.FurtherInfoFragmentArgs import kotlinx.android.synthetic.main.activity_further_info.* -private const val WEATHER = "param1" /** * A simple [Fragment] subclass. * Use the [FurtherInfoFragment.newInstance] factory method to diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/home/HomeFragment.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/home/HomeFragment.kt index b051eee..5902faa 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/home/HomeFragment.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/home/HomeFragment.kt @@ -24,7 +24,7 @@ import kotlinx.android.synthetic.main.fragment_home.* * A simple [Fragment] subclass. * create an instance of this fragment. */ -class HomeFragment : BaseFragment(){ +class HomeFragment : BaseFragment() { private val viewModel by getFragmentViewModel() @@ -49,6 +49,7 @@ class HomeFragment : BaseFragment(){ adapter = recyclerAdapter } + getPermissionResult(Manifest.permission.ACCESS_COARSE_LOCATION, LOCATION_PERMISSION_REQUEST){ viewModel.fetchData() } diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/widget/WidgetLocationPermissionActivity.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/widget/WidgetLocationPermissionActivity.kt new file mode 100644 index 0000000..32026b3 --- /dev/null +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/widget/WidgetLocationPermissionActivity.kt @@ -0,0 +1,86 @@ +package com.appttude.h_mal.atlas_weather.monoWeather.ui.widget + +import android.Manifest.permission.ACCESS_COARSE_LOCATION +import android.app.Activity +import android.appwidget.AppWidgetManager.* +import android.content.Intent +import android.content.pm.PackageManager.PERMISSION_GRANTED +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.ActivityCompat.checkSelfPermission +import com.appttude.h_mal.atlas_weather.R +import com.appttude.h_mal.atlas_weather.utils.displayToast +import kotlinx.android.synthetic.monoWeather.permissions_declaration_dialog.* + +const val PERMISSION_CODE = 401 + +class WidgetLocationPermissionActivity : AppCompatActivity() { + private var mAppWidgetId = INVALID_APPWIDGET_ID + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // Set the result to CANCELED. This will cause the widget host to cancel + // out of the widget placement if the user presses the back button. + setResult(RESULT_CANCELED) + + // Find the widget id from the intent. + intent.extras?.let { + mAppWidgetId = it.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID) + } + + // If this activity was started with an intent without an app widget ID, finish with an error. + if (mAppWidgetId == INVALID_APPWIDGET_ID) { + finish() + return + } + + setContentView(R.layout.permissions_declaration_dialog) + + submit.setOnClickListener { + if (checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) { + requestPermissions(arrayOf(ACCESS_COARSE_LOCATION), PERMISSION_CODE) + } else { + submitWidget() + } + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (requestCode == PERMISSION_CODE) { + if (grantResults.isNotEmpty() && grantResults[0] == PERMISSION_GRANTED) { + submitWidget() + } else { + displayToast("Location Permission denied") + } + } + } + + private fun submitWidget() { + sendUpdateIntent() + finishCurrencyWidgetActivity() + } + + private fun finishCurrencyWidgetActivity() { + // Make sure we pass back the original appWidgetId + val resultValue = intent + resultValue.putExtra(EXTRA_APPWIDGET_ID, mAppWidgetId) + setResult(Activity.RESULT_OK, resultValue) + finish() + } + + private fun sendUpdateIntent() { + // It is the responsibility of the configuration activity to update the app widget + // Send update broadcast to widget app class + Intent(this@WidgetLocationPermissionActivity, + WidgetLocationPermissionActivity::class.java + ).apply { + action = ACTION_APPWIDGET_UPDATE + + // Put current app widget ID into extras and send broadcast + putExtra(EXTRA_APPWIDGET_IDS, intArrayOf(mAppWidgetId)) + sendBroadcast(this) + } + } +} \ No newline at end of file diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/world/WorldFragment.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/world/WorldFragment.kt index 9e8baef..f1a9198 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/world/WorldFragment.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/ui/world/WorldFragment.kt @@ -9,7 +9,6 @@ import androidx.lifecycle.observe import androidx.recyclerview.widget.LinearLayoutManager import com.appttude.h_mal.atlas_weather.R import com.appttude.h_mal.atlas_weather.monoWeather.ui.BaseFragment -import com.appttude.h_mal.atlas_weather.monoWeather.ui.WorldItemFragmentDirections import com.appttude.h_mal.atlas_weather.monoWeather.ui.world.WorldFragmentDirections.actionWorldFragmentToWorldItemFragment import com.appttude.h_mal.atlas_weather.utils.navigateTo import com.appttude.h_mal.atlas_weather.viewmodel.WorldViewModel diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/MyWidgetRemoteViewsFactory.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/MyWidgetRemoteViewsFactory.kt index 4aa064c..e90dc29 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/MyWidgetRemoteViewsFactory.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/MyWidgetRemoteViewsFactory.kt @@ -35,7 +35,6 @@ class MyWidgetRemoteViewsFactory( override fun getViewAt(i: Int): RemoteViews { val rv = RemoteViews(context.packageName, R.layout.widget_item) - if (list.isNullOrEmpty()) return rv list?.get(i)?.let { @@ -57,4 +56,5 @@ class MyWidgetRemoteViewsFactory( override fun getItemId(i: Int): Long = i.toLong() override fun hasStableIds(): Boolean = true + } \ No newline at end of file diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/NewAppWidget.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/NewAppWidget.kt index dd038a6..5cc55f8 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/NewAppWidget.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/NewAppWidget.kt @@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context import android.content.Intent +import android.os.Bundle import com.appttude.h_mal.atlas_weather.monoWeather.widget.WidgetJobServiceIntent.Companion.enqueueWork /** diff --git a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/WidgetJobServiceIntent.kt b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/WidgetJobServiceIntent.kt index 4f514b4..a5b6185 100644 --- a/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/WidgetJobServiceIntent.kt +++ b/app/src/monoWeather/java/com/appttude/h_mal/atlas_weather/monoWeather/widget/WidgetJobServiceIntent.kt @@ -1,19 +1,20 @@ package com.appttude.h_mal.atlas_weather.monoWeather.widget -import android.Manifest +import android.Manifest.permission.ACCESS_COARSE_LOCATION import android.annotation.SuppressLint import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.ComponentName import android.content.Context import android.content.Intent -import android.content.pm.PackageManager +import android.content.pm.PackageManager.PERMISSION_GRANTED import android.net.Uri import android.os.PowerManager import android.widget.RemoteViews -import androidx.core.app.ActivityCompat +import androidx.core.app.ActivityCompat.checkSelfPermission import com.appttude.h_mal.atlas_weather.R -import com.appttude.h_mal.atlas_weather.model.widget.WidgetData +import com.appttude.h_mal.atlas_weather.model.widget.InnerWidgetCellData +import com.appttude.h_mal.atlas_weather.model.widget.WidgetWeatherCollection import com.appttude.h_mal.atlas_weather.monoWeather.ui.MainActivity import com.appttude.h_mal.atlas_weather.utils.isInternetAvailable import com.appttude.h_mal.atlas_weather.utils.tryOrNullSuspended @@ -34,11 +35,7 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { // We have received work to do. The system or framework is already // holding a wake lock for us at this point, so we can just go. - val pm = getSystemService(POWER_SERVICE) as PowerManager - val isScreenOn = pm.isInteractive - - // If screen is on then update widget or do nothing - if (isScreenOn) executeWidgetUpdate() + executeWidgetUpdate() } private fun executeWidgetUpdate(){ @@ -48,23 +45,45 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { val thisAppWidget = ComponentName(packageName, NewAppWidget::class.java.name) val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget) - // Check if we have an active connection and permissions granted - if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) - != PackageManager.PERMISSION_GRANTED || !isInternetAvailable(this.applicationContext)) { - for (appWidgetId in appWidgetIds) { - setEmptyView(this, appWidgetManager, appWidgetId) - } - }else{ - CoroutineScope(Dispatchers.IO).launch { - val result = getWidgetWeather() + validateOperation()?.let { + if (it) updateWidget(appWidgetIds, appWidgetManager) + else updateErrorWidget(appWidgetIds, appWidgetManager) + } + } - for (appWidgetId in appWidgetIds) { - bindView(this@WidgetJobServiceIntent, appWidgetManager, appWidgetId, result) - } + private fun updateWidget(appWidgetIds: IntArray, appWidgetManager: AppWidgetManager){ + CoroutineScope(Dispatchers.IO).launch { + val result = getWidgetWeather() + + for (appWidgetId in appWidgetIds) { + bindView(this@WidgetJobServiceIntent, appWidgetManager, appWidgetId, result) } } } + private fun updateErrorWidget(appWidgetIds: IntArray, appWidgetManager: AppWidgetManager){ + for (appWidgetId in appWidgetIds) { + setEmptyView(this, appWidgetManager, appWidgetId) + } + } + + private fun validateOperation(): Boolean? { + val pm = getSystemService(POWER_SERVICE) as PowerManager + val isScreenOn = pm.isInteractive + val locationGranted = + checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED + val internetAvailable = isInternetAvailable(this.applicationContext) + + // no location return false + if (!locationGranted) return false + // internet is available lets go + if (internetAvailable) return true + // screen is off and no connection, do nothing + if (!isScreenOn && !internetAvailable) return null + + return if (isScreenOn && !internetAvailable) false else null + } + private fun createForecastListIntent( context: Context, appWidgetId: Int @@ -76,18 +95,17 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { } @SuppressLint("MissingPermission") - suspend fun getWidgetWeather(): WidgetData? { + suspend fun getWidgetWeather(): WidgetWeatherCollection? { return tryOrNullSuspended { helper.fetchData() - helper.getWidgetWeather() + helper.getWidgetWeatherCollection() } - } private fun setEmptyView(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) { try { - val error = if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) - != PackageManager.PERMISSION_GRANTED) { + val error = if (checkSelfPermission(context, ACCESS_COARSE_LOCATION) + != PERMISSION_GRANTED) { "No Permission" } else if (!isInternetAvailable(context.applicationContext)) { "No Connection" @@ -100,7 +118,6 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { } catch (e: Exception) { e.printStackTrace() } - } private fun bindEmptyView( @@ -128,18 +145,19 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, - weather: WidgetData?) { + collection: WidgetWeatherCollection?) { val views = createRemoteView(context, R.layout.weather_app_widget) setLastUpdated(views) views.setInt(R.id.whole_widget_view, "setBackgroundColor", helper.getWidgetBackground()) val clickingUpdatePendingIntent = createUpdatePendingIntent(NewAppWidget::class.java, context, appWidgetId) - val forecastListIntent = createForecastListIntent(context, appWidgetId) - if (weather != null) { + if (collection != null) { val clickPendingIntentTemplate = createClickingPendingIntent(context, MainActivity::class.java) views.apply { + val weather = collection.widgetData + setTextViewText(R.id.widget_main_temp, weather.currentTemp) setTextViewText(R.id.widget_feel_temp, "°C") setTextViewText(R.id.widget_current_location, weather.location) @@ -151,7 +169,8 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { setOnClickPendingIntent(R.id.widget_current_icon, clickingUpdatePendingIntent) setOnClickPendingIntent(R.id.widget_current_location, clickingUpdatePendingIntent) - setRemoteAdapter(R.id.widget_listview, forecastListIntent) + loadCells(appWidgetId, views, collection.forecast) +// setRemoteAdapter(R.id.widget_listview, forecastListIntent) } // Instruct the widget manager to update the widget @@ -163,6 +182,22 @@ class WidgetJobServiceIntent : BaseWidgetServiceIntentClass() { } + private fun loadCells(appWidgetId: Int, remoteViews: RemoteViews, weather: List){ + (0..4).forEach { i -> + val dayId: Int = resources.getIdentifier("widget_item_day_$i", "id", packageName) + val imageId: Int = resources.getIdentifier("widget_item_image_$i", "id", packageName) + val tempId: Int = resources.getIdentifier("widget_item_temp_high_$i", "id", packageName) + + val it = weather[i] + + remoteViews.setTextViewText(dayId, it.date) + remoteViews.setTextViewText(tempId, it.highTemp) + CoroutineScope(Dispatchers.Main).launch { + Picasso.get().load(it.icon).into(remoteViews, imageId, intArrayOf(appWidgetId)) + } + } + } + private fun setLastUpdated(views: RemoteViews){ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { val current = LocalDateTime.now() diff --git a/app/src/monoWeather/res/layout/permissions_declaration_dialog.xml b/app/src/monoWeather/res/layout/permissions_declaration_dialog.xml new file mode 100644 index 0000000..1f03448 --- /dev/null +++ b/app/src/monoWeather/res/layout/permissions_declaration_dialog.xml @@ -0,0 +1,51 @@ + + + + + + + +