Permalink
Browse files

Step: Introduction to LiveData and ViewModels

Change-Id: I8ee27ac1759eb21a0805ce8c5dd9ae363e867676
  • Loading branch information...
ceruleanotter committed Aug 17, 2017
1 parent 42a4d4f commit 1d6ff9dc80f191147c1954a646283886b5401359
View
@@ -33,6 +33,9 @@ dependencies {
compile "com.android.support:preference-v7:$support_version"
compile "com.android.support.constraint:constraint-layout:$constraint_layout_version"
compile "com.firebase:firebase-jobdispatcher:$firebase_jobdispatcher_version"
compile "android.arch.lifecycle:runtime:$arch_version"
compile "android.arch.lifecycle:extensions:$arch_version"
annotationProcessor "android.arch.lifecycle:compiler:$arch_version"
compile "android.arch.persistence.room:runtime:$arch_version"
annotationProcessor "android.arch.persistence.room:compiler:$arch_version"
@@ -15,10 +15,12 @@
*/
package com.example.android.sunshine.ui.detail;
import android.arch.lifecycle.LifecycleActivity;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.android.sunshine.AppExecutors;
import com.example.android.sunshine.R;
import com.example.android.sunshine.data.database.WeatherEntry;
import com.example.android.sunshine.databinding.ActivityDetailBinding;
@@ -30,7 +32,7 @@
/**
* Displays single day's forecast
*/
public class DetailActivity extends AppCompatActivity {
public class DetailActivity extends LifecycleActivity {

This comment has been minimized.

Show comment
Hide comment
@Harsh18894

Harsh18894 Mar 29, 2018

LifecycleActivity not present anymore.

@Harsh18894

Harsh18894 Mar 29, 2018

LifecycleActivity not present anymore.

This comment has been minimized.

Show comment
Hide comment
@SerggioC

SerggioC Apr 1, 2018

LifecycleActivity It's embedded in AppCompatActivity and Fragment

@SerggioC

SerggioC Apr 1, 2018

LifecycleActivity It's embedded in AppCompatActivity and Fragment

This comment has been minimized.

Show comment
Hide comment
@simioluwatomi

simioluwatomi May 11, 2018

From the release notes here:

  • The deprecated LifecycleActivity and LifecycleFragment have now been removed - please useFragmentActivity, AppCompatActivity or support Fragment.
@simioluwatomi

simioluwatomi May 11, 2018

From the release notes here:

  • The deprecated LifecycleActivity and LifecycleFragment have now been removed - please useFragmentActivity, AppCompatActivity or support Fragment.
public static final String WEATHER_ID_EXTRA = "WEATHER_ID_EXTRA";
@@ -42,6 +44,7 @@
* programmatically without cluttering up the code with findViewById.
*/
private ActivityDetailBinding mDetailBinding;
private DetailActivityViewModel mViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -51,6 +54,30 @@ protected void onCreate(Bundle savedInstanceState) {
long timestamp = getIntent().getLongExtra(WEATHER_ID_EXTRA, -1);
Date date = new Date(timestamp);
mViewModel = ViewModelProviders.of(this).get(DetailActivityViewModel.class);
// Observers changes in the WeatherEntry with the id mId
mViewModel.getWeather().observe(this, weatherEntry -> {
// If the weather forecast details change, update the UI
if (weatherEntry != null) bindWeatherToUI(weatherEntry);
});
AppExecutors.getInstance().diskIO().execute(()-> {
try {
// Pretend this is the network loading data
Thread.sleep(4000);
Date today = SunshineDateUtils.getNormalizedUtcDateForToday();
WeatherEntry pretendWeatherFromDatabase = new WeatherEntry(1, 210, today,88.0,99.0,71,1030, 74, 5);
mViewModel.setWeather(pretendWeatherFromDatabase);
Thread.sleep(2000);
pretendWeatherFromDatabase = new WeatherEntry(1, 952, today,50.0,60.0,46,1044, 70, 100);
mViewModel.setWeather(pretendWeatherFromDatabase);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
private void bindWeatherToUI(WeatherEntry weatherEntry) {
@@ -1,42 +1,43 @@
///*
// * Copyright (C) 2017 The Android Open Source Project
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package com.example.android.sunshine.ui.detail;
//
//import android.arch.lifecycle.ViewModel;
//
//import com.example.android.sunshine.data.database.WeatherEntry;
//
///**
// * {@link ViewModel} for {@link DetailActivity}
// */
//public class DetailActivityViewModel extends ViewModel {
//
// // Weather forecast the user is looking at
// private WeatherEntry mWeather;
//
// public DetailActivityViewModel() {
//
// }
//
// public WeatherEntry getWeather() {
// return mWeather;
// }
//
// public void setWeather(WeatherEntry weatherEntry) {
// mWeather = weatherEntry;
// }
//}
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.sunshine.ui.detail;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import com.example.android.sunshine.data.database.WeatherEntry;
/**
* {@link ViewModel} for {@link DetailActivity}
*/
public class DetailActivityViewModel extends ViewModel {
// Weather forecast the user is looking at
private MutableLiveData<WeatherEntry> mWeather;
public DetailActivityViewModel() {
mWeather = new MutableLiveData<>();
}
public MutableLiveData<WeatherEntry> getWeather() {
return mWeather;
}
public void setWeather(WeatherEntry weatherEntry) {
mWeather.postValue(weatherEntry);
}
}

3 comments on commit 1d6ff9d

@gbielanski

This comment has been minimized.

Show comment
Hide comment
@gbielanski

gbielanski Sep 12, 2017

I should cast this to LifecycleOwner in line
mViewModel.getWeather().observe(this, weatherEntry -> {
so it looks
mViewModel.getWeather().observe((LifecycleOwner) this, weatherEntry -> {
It did not compile otherwise.

gbielanski replied Sep 12, 2017

I should cast this to LifecycleOwner in line
mViewModel.getWeather().observe(this, weatherEntry -> {
so it looks
mViewModel.getWeather().observe((LifecycleOwner) this, weatherEntry -> {
It did not compile otherwise.

@kht2199

This comment has been minimized.

Show comment
Hide comment
@kht2199

kht2199 Sep 23, 2017

@gbielanski your DetailActivity activity should extend LifecycleActivity or FrameActivity. then it works well.

but My detail activity extends v7 AppCompatActivity because of deprecated LifecycleActivity in my version.

kht2199 replied Sep 23, 2017

@gbielanski your DetailActivity activity should extend LifecycleActivity or FrameActivity. then it works well.

but My detail activity extends v7 AppCompatActivity because of deprecated LifecycleActivity in my version.

@ashikazeez

This comment has been minimized.

Show comment
Hide comment
@ashikazeez

ashikazeez Jan 16, 2018

issue : new MutableLiveData<>(); in DetailActivityViewModel line 33 "Cannot infer arguments" ,pls help

public DetailActivityViewModel() {
mWeather = new MutableLiveData<>();
}

ashikazeez replied Jan 16, 2018

issue : new MutableLiveData<>(); in DetailActivityViewModel line 33 "Cannot infer arguments" ,pls help

public DetailActivityViewModel() {
mWeather = new MutableLiveData<>();
}

Please sign in to comment.