Skip to content

Instantly share code, notes, and snippets.

@dpolishuk
Last active November 18, 2015 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpolishuk/c198c103d15eedd1baf4 to your computer and use it in GitHub Desktop.
Save dpolishuk/c198c103d15eedd1baf4 to your computer and use it in GitHub Desktop.
kotlinx android does not work for view ;(
import kotlinx.android.synthetic.view_weather_for_week.view.*
public class WeatherFor5DaysView : LinearLayout {
val dayNameViews: List<TextView> = arrayListOf(day_name_1, day_name_2, day_name_3, day_name_4, day_name_5)
val dayViews: List<ImageView> = arrayListOf(day_1, day_2, day_3, day_4, day_5)
val tempViews: List<TextView> = arrayListOf(temp_1, temp_2, temp_3, temp_4, temp_5)
lateinit var transformation: Transformation
var celsius: String? = null
var fahrenheit: String? = null
public constructor(context: Context) : super(context) {
init(context)
}
public constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(context)
}
private fun init(context: Context) {
LayoutInflater.from(context).inflate(R.layout.view_weather_for_week, this, true)
celsius = context.getString(R.string.celcius)
fahrenheit = context.getString(R.string.fahrenheit)
}
public fun setWeatherForWeek(weatherList: List<Weather>, useCelsius: Boolean, transformation: Transformation) {
this.transformation = transformation
for (i in weatherList.indices) {
val v = dayViews[i]
val weather = weatherList[i]
try {
val date = Date.valueOf(weather.date)
val weekDay = DateUtils.formatDateTime(context, date.time, DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_ABBREV_WEEKDAY)
dayNameViews[i].text = weekDay
} catch (e: IllegalArgumentException) {
dayNameViews[i].text = ""
}
if (useCelsius) {
tempViews[i].text = "${weather.tempMinC}-${weather.tempMaxC}${context!!.getString(R.string.celcius)}"
} else {
tempViews[i].text = "${weather.tempMinF}-${weather.tempMaxF}${context!!.getString(R.string.fahrenheit)}"
}
val urls = weather.weatherIconUrl
if (urls?.isNotEmpty() ?: false) {
val url = urls?.get(0)
if (!TextUtils.isEmpty(url?.value)) {
Picasso.with(context).load(url?.value).transform(transformation).into(v)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment