Skip to content

Commit

Permalink
MVI in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
myofficework000 committed Jun 29, 2023
1 parent 1ab5e7e commit 79554b4
Show file tree
Hide file tree
Showing 44 changed files with 1,043 additions and 69 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ dependencies {
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.11'
//Moshi
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"

//Convertor factory by Gson
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.data.location
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.location

import android.Manifest
import android.app.Application
Expand All @@ -7,14 +7,12 @@ import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import androidx.core.content.ContextCompat
import com.abanoub.weather.domain.location.LocationTracker
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.location.LocationTracker
import com.google.android.gms.location.FusedLocationProviderClient
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import javax.inject.Inject
import kotlin.coroutines.resume

@OptIn(ExperimentalCoroutinesApi::class)
class DefaultLocationTracker @Inject constructor(
private val locationClient: FusedLocationProviderClient,
private val application: Application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.abanoub.weather.data.mappers
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.mappers

import android.os.Build
import androidx.annotation.RequiresApi
import com.abanoub.weather.data.remote.WeatherDataDto
import com.abanoub.weather.data.remote.WeatherDto
import com.abanoub.weather.domain.weather.WeatherData
import com.abanoub.weather.domain.weather.WeatherInfo
import com.abanoub.weather.domain.weather.WeatherType
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherData
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherInfo
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherType
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherDataDto
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherDto
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

Expand All @@ -15,7 +13,6 @@ private data class IndexedWeatherData(
val data: WeatherData
)

@RequiresApi(Build.VERSION_CODES.O)
fun WeatherDataDto.toWeatherDataMap(): Map<Int, List<WeatherData>> {
return time.mapIndexed { index, time ->
val temperature = temperatures[index]
Expand All @@ -41,7 +38,6 @@ fun WeatherDataDto.toWeatherDataMap(): Map<Int, List<WeatherData>> {
}
}

@RequiresApi(Build.VERSION_CODES.O)
fun WeatherDto.toWeatherInfo(): WeatherInfo {
val weatherDataMap = weatherData.toWeatherDataMap()
val now = LocalDateTime.now()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote

object Constant {
const val END_POINT_WEATHER_API =
"v1/forecast?hourly=temperature_2m,weathercode,relativehumidity_2m,windspeed_10m,pressure_msl"
const val BASE_URL_WEATHER_API = "https://api.open-meteo.com/"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.abanoub.weather.data.remote
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote

import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.Constant.END_POINT_WEATHER_API
import retrofit2.http.GET
import retrofit2.http.Query

interface WeatherApi {

@GET("v1/forecast?hourly=temperature_2m,weathercode,relativehumidity_2m,windspeed_10m,pressure_msl")
@GET(END_POINT_WEATHER_API)
suspend fun getWeather(
@Query("latitude") latitude: Double,
@Query("longitude") longitude: Double,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.data.remote
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote

import com.squareup.moshi.Json

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.abanoub.weather.data.remote
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote

import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherDataDto
import com.squareup.moshi.Json

data class WeatherDto(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.abanoub.weather.data.repository
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.repository

import android.os.Build
import androidx.annotation.RequiresApi
import com.abanoub.weather.data.mappers.toWeatherInfo
import com.abanoub.weather.data.remote.WeatherApi
import com.abanoub.weather.domain.repository.WeatherRepository
import com.abanoub.weather.domain.util.Resource
import com.abanoub.weather.domain.weather.WeatherInfo
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.mappers.toWeatherInfo
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.repository.WeatherRepository
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.util.Resource
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherInfo
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherApi
import javax.inject.Inject

class WeatherRepositoryImpl @Inject constructor(
private val api: WeatherApi
) : WeatherRepository {

@RequiresApi(Build.VERSION_CODES.O)
override suspend fun getWeatherData(lat: Double, long: Double): Resource<WeatherInfo> {
return try {
Resource.Success(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.di

import android.app.Application
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.Constant.BASE_URL_WEATHER_API
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.remote.WeatherApi
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.create
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

@Provides
@Singleton
fun provideWeatherApi(): WeatherApi {
return Retrofit.Builder()
.baseUrl(BASE_URL_WEATHER_API)
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create()
}

@Provides
@Singleton
fun provideFusedLocationProviderClient(application: Application): FusedLocationProviderClient {
return LocationServices.getFusedLocationProviderClient(application)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.di

import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.location.DefaultLocationTracker
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.location.LocationTracker
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class LocationModule {

@Binds
@Singleton
abstract fun bindLocationTracker(
defaultLocationTracker: DefaultLocationTracker
): LocationTracker
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.di

import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.data.repository.WeatherRepositoryImpl
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.repository.WeatherRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {

@Binds
@Singleton
abstract fun bindWeatherRepository(
weatherRepositoryImpl: WeatherRepositoryImpl
): WeatherRepository
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.domain.location
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.location

import android.location.Location

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.abanoub.weather.domain.repository
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.repository

import com.abanoub.weather.domain.util.Resource
import com.abanoub.weather.domain.weather.WeatherInfo
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.util.Resource
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherInfo

interface WeatherRepository {
suspend fun getWeatherData(lat: Double, long: Double): Resource<WeatherInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.domain.util
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.util

sealed class Resource<T>(val data: T? = null, val message: String? = null) {
class Success<T>(data: T?) : Resource<T>(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.domain.weather
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather

import java.time.LocalDateTime

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.domain.weather
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather

data class WeatherInfo(
val weatherDataPerDay: Map<Int, List<WeatherData>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.abanoub.weather.domain.weather
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather

import androidx.annotation.DrawableRes
import com.abanoub.weather.R
import com.example.jetpack_compose_all_in_one.R

sealed class WeatherType(
val weatherDesc: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.abanoub.weather.presentation
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.presentation

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -15,10 +13,9 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.abanoub.weather.domain.weather.WeatherData
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.domain.weather.WeatherData
import java.time.format.DateTimeFormatter

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun HourlyWeatherDisplay(
weatherData: WeatherData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.abanoub.weather.presentation
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.presentation

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -17,11 +23,11 @@ import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.presentation.WeatherState
import com.example.jetpack_compose_all_in_one.R
import com.example.jetpack_compose_all_in_one.ui.theme.dp_15
import java.time.format.DateTimeFormatter
import kotlin.math.roundToInt

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun WeatherCard(
state: WeatherState,
Expand All @@ -30,9 +36,9 @@ fun WeatherCard(
) {
state.weatherInfo?.currentWeatherData?.let { data ->
Card(
backgroundColor = backgroundColor,
shape = RoundedCornerShape(10.dp),
modifier = modifier.padding(16.dp)
modifier = modifier.then(Modifier.padding(dp_15)),
shape = RoundedCornerShape(dp_15),
elevation = CardDefaults.cardElevation(dp_15),
) {
Column(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.abanoub.weather.presentation
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.presentation

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.abanoub.weather.presentation
package com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.presentation

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material.Text
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.jetpack_compose_all_in_one.android_architectures.clean_code_with_mvi_mvvm.presentation.WeatherState

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun WeatherForecast(
state: WeatherState,
Expand Down
Loading

0 comments on commit 79554b4

Please sign in to comment.