Skip to content

Commit

Permalink
新增切换城市和手动更新天气的功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony committed Oct 24, 2019
1 parent 7ab39f3 commit e3d9624
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.sunnyweather.android.R
import com.sunnyweather.android.logic.dao.PlaceDao
import com.sunnyweather.android.logic.model.Place
import com.sunnyweather.android.ui.weather.WeatherActivity
import kotlinx.android.synthetic.main.activity_weather.*

class PlaceAdapter(private val fragment: PlaceFragment, private val placeList: List<Place>) : RecyclerView.Adapter<PlaceAdapter.ViewHolder>() {

Expand All @@ -25,14 +24,23 @@ class PlaceAdapter(private val fragment: PlaceFragment, private val placeList: L
holder.itemView.setOnClickListener {
val position = holder.adapterPosition
val place = placeList[position]
val intent = Intent(parent.context, WeatherActivity::class.java).apply {
putExtra("location_lng", place.location.lng)
putExtra("location_lat", place.location.lat)
putExtra("place_name", place.name)
val activity = fragment.activity
if (activity is WeatherActivity) {
activity.drawerLayout.closeDrawers()
activity.viewModel.locationLng = place.location.lng
activity.viewModel.locationLat = place.location.lat
activity.viewModel.placeName = place.name
activity.refreshWeather()
} else {
val intent = Intent(parent.context, WeatherActivity::class.java).apply {
putExtra("location_lng", place.location.lng)
putExtra("location_lat", place.location.lat)
putExtra("place_name", place.name)
}
fragment.startActivity(intent)
activity?.finish()
}
fragment.viewModel.savePlace(place)
fragment.startActivity(intent)
fragment.activity?.finish()
}
return holder
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.sunnyweather.android.MainActivity
import com.sunnyweather.android.R
import com.sunnyweather.android.ui.weather.WeatherActivity
import kotlinx.android.synthetic.main.fragment_place.*
Expand All @@ -27,7 +28,7 @@ class PlaceFragment : Fragment() {

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (viewModel.isPlaceSaved()) {
if (activity is MainActivity && viewModel.isPlaceSaved()) {
val place = viewModel.getSavedPlace()
val intent = Intent(context, WeatherActivity::class.java).apply {
putExtra("location_lng", place.location.lng)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.sunnyweather.android.ui.weather

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.sunnyweather.android.R
Expand Down Expand Up @@ -50,16 +54,41 @@ class WeatherActivity : AppCompatActivity() {
Toast.makeText(this, "无法成功获取天气信息", Toast.LENGTH_SHORT).show()
result.exceptionOrNull()?.printStackTrace()
}
swipeRefresh.isRefreshing = false
})
swipeRefresh.setColorSchemeResources(R.color.colorPrimary)
refreshWeather()
swipeRefresh.setOnRefreshListener {
refreshWeather()
}
navBtn.setOnClickListener {
drawerLayout.openDrawer(GravityCompat.START)
}
drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
override fun onDrawerStateChanged(newState: Int) {}

override fun onDrawerSlide(drawerView: View, slideOffset: Float) {}

override fun onDrawerOpened(drawerView: View) {}

override fun onDrawerClosed(drawerView: View) {
val manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
manager.hideSoftInputFromWindow(drawerView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
})
}

fun refreshWeather() {
viewModel.refreshWeather(viewModel.locationLng, viewModel.locationLat)
swipeRefresh.isRefreshing = true
}

private fun showWeatherInfo(weather: Weather) {
placeName.text = viewModel.placeName
val realtime = weather.realtime
val daily = weather.daily
// 填充now.xml布局中数据
val currentTempText = "${realtime.temperature}"
val currentTempText = "${realtime.temperature.toInt()}"
currentTemp.text = currentTempText
currentSky.text = getSky(realtime.skycon).info
val currentPM25Text = "空气指数 ${realtime.airQuality.aqi.chn.toInt()}"
Expand Down
59 changes: 45 additions & 14 deletions app/src/main/res/layout/activity_weather.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/weatherLayout"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never"
android:visibility="invisible">
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent">

<include layout="@layout/now" />
<ScrollView
android:id="@+id/weatherLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none"
android:visibility="invisible">

<include layout="@layout/forecast" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<include layout="@layout/life_index" />
<include layout="@layout/now" />

</LinearLayout>
<include layout="@layout/forecast" />

</ScrollView>
<include layout="@layout/life_index" />

</LinearLayout>

</ScrollView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="true"
android:focusable="true"
android:background="@color/colorPrimary">

<fragment
android:id="@+id/placeFragment"
android:name="com.sunnyweather.android.ui.place.PlaceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"/>

</FrameLayout>

</androidx.drawerlayout.widget.DrawerLayout>
17 changes: 6 additions & 11 deletions app/src/main/res/layout/forecast_item.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp">
Expand All @@ -10,32 +9,28 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
tools:text="2019-10-13" />
android:layout_weight="4" />

<ImageView
android:id="@+id/skyIcon"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1"
android:src="@drawable/ic_clear_day" />
android:layout_weight="2" />

<TextView
android:id="@+id/skyInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
tools:text="" />
android:layout_weight="2"
android:gravity="start" />

<TextView
android:id="@+id/temperatureInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
tools:text="27~29" />
android:layout_weight="3"
android:gravity="end" />

</LinearLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_place.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="?android:windowBackground">

<ImageView
android:id="@+id/bgImageView"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/now.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
android:layout_height="70dp"
android:fitsSystemWindows="true">

<Button
android:id="@+id/navBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="15dp"
android:layout_gravity="center_vertical"
android:background="@drawable/ic_home" />

<TextView
android:id="@+id/placeName"
android:layout_width="wrap_content"
Expand Down

0 comments on commit e3d9624

Please sign in to comment.