Skip to content

Commit

Permalink
Feat : Connect a backend weather API tukcomCD2024#102 tukcomCD2024#85
Browse files Browse the repository at this point in the history
fix a bug user can't change schedule date when its original date is later than a date that user want to change
  • Loading branch information
ksh-g001 committed Jul 13, 2024
1 parent 3724165 commit 903b5d9
Show file tree
Hide file tree
Showing 26 changed files with 545 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.project.how.R
import com.project.how.data_class.dto.weather.GetWeeklyWeathersResponseElement
import com.project.how.data_class.dto.country.weather.GetWeeklyWeathersResponseElement
import com.project.how.databinding.WeekWeatherItemBinding
import kotlin.math.roundToInt

class WeekWeatherAdapter(private val data : List<GetWeeklyWeathersResponseElement>)
class WeekWeatherAdapter(private var data : List<GetWeeklyWeathersResponseElement>)
: RecyclerView.Adapter<WeekWeatherAdapter.ViewHolder>() {

inner class ViewHolder(val binding: WeekWeatherItemBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(data: GetWeeklyWeathersResponseElement){
binding.week.text = data.date
binding.temp.text = data.temp
val temp = data.temp.toDouble().roundToInt()
binding.week.text = data.date.removeRange(0,11)
binding.temp.text = "$temp"
Glide.with(binding.root)
.load(data.iconUrl)
.error(R.drawable.icon_question)
Expand All @@ -38,4 +40,9 @@ class WeekWeatherAdapter(private val data : List<GetWeeklyWeathersResponseElemen
holder.bind(data)
}

fun update(newData : List<GetWeeklyWeathersResponseElement>){
data = newData
notifyDataSetChanged()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.how.data_class.dto.country

import com.google.gson.annotations.SerializedName

data class GetCountryInfoRequest(
@SerializedName("country") val country : String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.project.how.data_class.dto.country.weather

import com.google.gson.annotations.SerializedName

data class GetCurrentWeatherResponse (
val main: String,
val description: String,
val temp: String,
val localTime: String,
val iconUrl: String,
@SerializedName("temp_min")
val tempMin: String,
@SerializedName("temp_max")
val tempMax: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.project.how.data_class.dto.country.weather

import com.google.gson.annotations.SerializedName

typealias GetWeeklyWeathersResponse = ArrayList<GetWeeklyWeathersResponseElement>

data class GetWeeklyWeathersResponseElement (
@SerializedName("date")
val date: String,
@SerializedName("temp")
val temp: String,
@SerializedName("iconUrl")
val iconUrl: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.project.how.interface_af
import com.project.how.data_class.recyclerview.DaysSchedule

interface OnDateTimeListener {
fun onSaveDate(d : DaysSchedule, date : String, position: Int)
fun onSaveDate(d: DaysSchedule, selectedDate: String, changedDate: String, position: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.project.how.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.project.how.data_class.dto.country.weather.GetCurrentWeatherResponse
import com.project.how.data_class.dto.country.weather.GetWeeklyWeathersResponse

class CountryRepository {
private var _currentWeatherLiveData : MutableLiveData<GetCurrentWeatherResponse> = MutableLiveData()
private var _weeklyWeathersLiveData : MutableLiveData<GetWeeklyWeathersResponse> = MutableLiveData()
val currentWeatherLiveData : LiveData<GetCurrentWeatherResponse>
get() = _currentWeatherLiveData
val weeklyWeathersLiveData : LiveData<GetWeeklyWeathersResponse>
get() = _weeklyWeathersLiveData

fun getCurrentWeather(data : GetCurrentWeatherResponse){
_currentWeatherLiveData.postValue(data)
}

fun getWeeklyWeathers(data : GetWeeklyWeathersResponse){
_weeklyWeathersLiveData.postValue(data)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ import java.time.temporal.ChronoUnit
import java.util.Calendar

class ScheduleRepository {
val today = Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}.time.time
private val _nearScheduleDayLiveData : MutableLiveData<GetFastestSchedulesResponse> = MutableLiveData()
private val _scheduleLiveData : MutableLiveData<Schedule> = MutableLiveData()
private val _scheduleListLiveData : MutableLiveData<GetScheduleListResponse> = MutableLiveData()
Expand Down Expand Up @@ -63,7 +57,7 @@ class ScheduleRepository {
aiSchedule.country,
aiSchedule.startDate,
aiSchedule.endDate,
0,
aiSchedule.budget,
getDailySchedule(aiSchedule.dailySchedule)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.project.how.network.api_interface

import com.project.how.data_class.dto.country.GetCountryInfoRequest
import com.project.how.data_class.dto.country.GetCountryLocationResponse
import com.project.how.data_class.dto.country.weather.GetCurrentWeatherResponse
import com.project.how.data_class.dto.country.weather.GetWeeklyWeathersResponse
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface CountryService {
@POST("countries/locations")
fun getCountryLocation(
@Body country : GetCountryInfoRequest
) : Call<GetCountryLocationResponse>

@POST("countries/weather/current")
fun getCurrentWeather(
@Body country: GetCountryInfoRequest
) : Call<GetCurrentWeatherResponse>

@POST("countries/weather/weekly")
fun getWeeklyWeather(
@Body country: GetCountryInfoRequest
) : Call<GetWeeklyWeathersResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.project.how.network.api_interface
import com.project.how.data_class.dto.schedule.CreateScheduleListRequest
import com.project.how.data_class.dto.schedule.CreateScheduleListResponse
import com.project.how.data_class.dto.schedule.CreateScheduleResponse
import com.project.how.data_class.dto.country.GetCountryLocationRequest
import com.project.how.data_class.dto.country.GetCountryInfoRequest
import com.project.how.data_class.dto.country.GetCountryLocationResponse
import com.project.how.data_class.dto.schedule.GetFastestSchedulesResponse
import com.project.how.data_class.dto.schedule.GetLatestSchedulesResponse
Expand Down Expand Up @@ -62,7 +62,7 @@ interface ScheduleService {

@POST("countries/locations")
fun getCountryLocation(
@Body country : GetCountryLocationRequest
@Body country : GetCountryInfoRequest
) : Call<GetCountryLocationResponse>

@GET("schedules/dday")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit
object BookingRetrofit {
private const val BASE_URL = BuildConfig.API_SERVER

fun getApiService() : BookingService? = BookingRetrofit.getInstance()
fun getApiService() : BookingService? = getInstance()
?.create(BookingService::class.java)

private fun getInstance() : Retrofit? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.project.how.network.client

import com.google.gson.GsonBuilder
import com.project.how.BuildConfig
import com.project.how.network.api_interface.CountryService
import com.project.how.network.converter_factory.NullOnEmptyConverterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

object CountryRetrofit {
private const val BASE_URL = BuildConfig.API_SERVER

fun getApiService() : CountryService? = getInstance()
?.create(CountryService::class.java)

private fun getInstance() : Retrofit? {
val gson = GsonBuilder().setLenient().create()

val httpClient = OkHttpClient.Builder()
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)

val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
httpClient.addInterceptor(loggingInterceptor)

return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(NullOnEmptyConverterFactory())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -47,6 +48,9 @@ class SplashActivity : AppCompatActivity() {
if (check == MemberViewModel.SUCCESS){
moveMain()
}else{
if (check == MemberViewModel.ON_FAILURE){
Toast.makeText(this@SplashActivity, getString(R.string.server_network_error), Toast.LENGTH_SHORT).show()
}
moveLogin()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.project.how.view.dialog.bottom_sheet_dialog.DesBottomSheetDialog
import com.project.how.view.dialog.bottom_sheet_dialog.PurposeBottomSheetDialog
import com.project.how.view.dialog.bottom_sheet_dialog.ActivityBottomSheetDialog
import com.project.how.view_model.AiScheduleViewModel
import com.project.how.view_model.CountryViewModel
import com.project.how.view_model.ScheduleViewModel
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
Expand All @@ -43,6 +44,7 @@ class AddAICalendarActivity :
private lateinit var binding : ActivityAddAicalendarBinding
private val viewModel : AiScheduleViewModel by viewModels()
private val scheduleViewModel : ScheduleViewModel by viewModels()
private val countryViewModel : CountryViewModel by viewModels()
private var destination : String? = null
private var purpose : MutableList<String>? = null
private var activities : MutableList<String>? = null
Expand Down Expand Up @@ -228,14 +230,14 @@ class AddAICalendarActivity :

override fun onDesListener(des: String) {
lifecycleScope.launch {
scheduleViewModel.getCountryLocation(des).collect{ location ->
countryViewModel.getCountryLocation(des).collect{ location ->
location?.let {
destination = des
binding.desOutput.text = des
binding.desOutput.visibility = View.VISIBLE
latLng = location
} ?: run {
scheduleViewModel.getCountryLocation(des).collect { newLocation ->
countryViewModel.getCountryLocation(des).collect { newLocation ->
newLocation?.let {
destination = des
binding.desOutput.text = des
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.project.how.view.dp.DpPxChanger
import com.project.how.view.map_helper.CameraOptionProducer
import com.project.how.view.map_helper.MarkerProducer
import com.project.how.view_model.CalendarViewModel
import com.project.how.view_model.CountryViewModel
import com.project.how.view_model.MemberViewModel
import com.project.how.view_model.ScheduleViewModel
import kotlinx.coroutines.launch
Expand All @@ -65,6 +66,7 @@ class CalendarEditActivity
private lateinit var binding : ActivityCalendarEditBinding
private val viewModel : ScheduleViewModel by viewModels()
private val calendarViewModel : CalendarViewModel by viewModels()
private val countryViewModel : CountryViewModel by viewModels()
private lateinit var data : Schedule
private var type: Int = FAILURE
private lateinit var adapter : DaysScheduleEditAdapter
Expand Down Expand Up @@ -351,7 +353,6 @@ class CalendarEditActivity
}

private suspend fun saveNewSchedule(){

viewModel.saveSchedule(this, MemberViewModel.tokensLiveData.value!!.accessToken, data).collect{check ->
when(check){
ScheduleViewModel.NETWORK_FAILED ->{
Expand Down Expand Up @@ -449,13 +450,13 @@ class CalendarEditActivity

override fun onDesListener(des: String) {
lifecycleScope.launch {
viewModel.getCountryLocation(des).collect{ location ->
countryViewModel.getCountryLocation(des).collect{ location ->
location?.let {
data.country = des
latitude = location.lat
longitude = location.lng
} ?: run {
viewModel.getCountryLocation(des).collect { newLocation ->
countryViewModel.getCountryLocation(des).collect { newLocation ->
newLocation?.let {
data.country = des
latitude = newLocation.lat
Expand All @@ -469,13 +470,23 @@ class CalendarEditActivity
}
}

override fun onSaveDate(d: DaysSchedule, date: String, position: Int) {
override fun onSaveDate(
d: DaysSchedule,
selectedDate: String,
changedDate: String,
position: Int
) {
val format = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val sd = format.parse(data.startDate)
val ssd = format.parse(date)
val sd = format.parse(selectedDate)
val ssd = format.parse(changedDate)
val diffMillies = abs(sd.time - ssd.time)
val diff = (diffMillies / (24 * 60 * 60 * 1000)).toInt()
data.dailySchedule[selectedDays+diff].add(d)
Log.d("getDateList", "onSaveDate\ndata.startDate : $sd\nchangeDate : ${ssd}\ndiff : $diff")
if (selectedDate>changedDate){
data.dailySchedule[selectedDays-diff].add(d)
}else{
data.dailySchedule[selectedDays+diff].add(d)
}
adapter.remove(position, true)
supportMapFragment.getMapAsync(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.project.how.interface_af.OnDesListener
import com.project.how.interface_af.OnYesOrNoListener
import com.project.how.view.dialog.YesOrNoDialog
import com.project.how.view.dialog.bottom_sheet_dialog.DesBottomSheetDialog
import com.project.how.view_model.CountryViewModel
import com.project.how.view_model.MemberViewModel
import com.project.how.view_model.ScheduleViewModel
import kotlinx.coroutines.launch
Expand All @@ -32,6 +33,7 @@ class CalendarListActivity
private lateinit var binding : ActivityCalendarListBinding
private lateinit var adapter : CalendarListAdapter
private val viewModel : ScheduleViewModel by viewModels()
private val countryViewModel : CountryViewModel by viewModels()
private var destination : String? = null
private var departureDate : String? = null
private var entranceDate : String? = null
Expand Down Expand Up @@ -110,9 +112,9 @@ class CalendarListActivity

override fun onDeleteButtonClickListener(data : GetScheduleListResponseElement, position : Int) {
val yesOrNoDialog = YesOrNoDialog(data.scheduleName, YesOrNoDialog.SCHEDULE_DELETE, position, this)
Log.d("onDelete", "position : ${position}")
yesOrNoDialog.show(supportFragmentManager, "YesOrNoDialog")
}

override fun onItemClickListener(id: Long, latitude : Double, longitude : Double) {
Log.d("onCreate", "onItemClickerListener\nid : ${id}\nlatitude : ${latitude}\tlongitude : ${longitude}")
val intent = Intent(this, CalendarActivity::class.java)
Expand All @@ -134,6 +136,7 @@ class CalendarListActivity
override fun onScheduleDeleteListener(position: Int) {
lifecycleScope.launch {
val data = adapter.getData(position)
Log.d("onDelete", "onScheduleDeleteListener\nposition: $position\tid : ${data.id}\ttitle : ${data.scheduleName}")
viewModel.deleteSchedule(this@CalendarListActivity, MemberViewModel.tokensLiveData.value!!.accessToken, data.id).collect{
when(it){
ScheduleViewModel.SUCCESS -> { adapter.remove(position) }
Expand All @@ -155,13 +158,13 @@ class CalendarListActivity

override fun onDesListener(des: String) {
lifecycleScope.launch {
viewModel.getCountryLocation(des).collect{ location ->
countryViewModel.getCountryLocation(des).collect{ location ->
location?.let {
destination = des
latLng = location
showCalendar()
} ?: run {
viewModel.getCountryLocation(des).collect { newLocation ->
countryViewModel.getCountryLocation(des).collect { newLocation ->
newLocation?.let {
destination = des
latLng = newLocation
Expand Down
Loading

0 comments on commit 903b5d9

Please sign in to comment.