Skip to content

Instantly share code, notes, and snippets.

View tpakis's full-sized avatar

Thanos Tsakiridis tpakis

View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseline_navigation"
app:startDestination="@id/step1Fragment">
<fragment
android:id="@+id/step1Fragment"
android:name="com.example.abnavigationexample.ui.abtest.fragments.Step1Fragment"
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseline_navigation"
app:startDestination="@id/step1Fragment">
<fragment
android:id="@+id/step1Fragment"
android:name="com.example.abnavigationexample.ui.abtest.fragments.Step1Fragment"
enum class ABTestVariation(val navGraphRes: Int) {
BASELINE(R.navigation.baseline_navigation),
VARIATION1(R.navigation.variant1_navigation),
VARIATION2(R.navigation.variant2_navigation),
}
private val abTestVariation: ABTestVariation by lazy {
intent?.getSerializableExtra(KEY) as? ABTestVariation ?: ABTestVariation.BASELINE
}
private void getRecipesFromWeb(){
Timber.d("getRecipesFromWeb");
recipeApi.getRecipesFromWeb().enqueue(new Callback<List<Recipe>>() {
@Override
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
if (response.isSuccessful()) {
setRecipesListObservableStatus(Status.SUCCESS,null);
addRecipesToDB(response.body());
} else {
// error case
@tpakis
tpakis / loadAllRecipesFromDB
Last active April 30, 2018 12:24
Async call to load from
private void loadAllRecipesFromDB() {
Timber.d("loadAllRecipesFromDB");
new AsyncTask<Void, Void, List<Recipe>>() {
@Override
protected List<Recipe> doInBackground(Void...a) {
return recipesDAO.getAllEntries();
}
@Override
protected void onPostExecute(List<Recipe> results) {
@tpakis
tpakis / gist:674e551600dc01c127864ffea9211a22
Last active April 30, 2018 12:23
Repository fetchData
public void fetchData(){
List<Recipe> loadingList = null;
if (recipesListObservable.getValue()!=null){
loadingList=recipesListObservable.getValue().data;
}
recipesListObservable.setValue(Resource.loading(loadingList));
loadAllRecipesFromDB();
getRecipesFromWeb();
}
public void getData(){
mRepository.fetchData();
}
//couple the activity with the view model
viewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
//subscribe to LiveData
viewModel.getRecipesListObservable().observe(MainActivity.this, new Observer<Resource<List<Recipe>>>() {
@Override
public void onChanged(@Nullable Resource<List<Recipe>> recipes) {
Timber.d(recipes.status.toString());
}
});
//ask the viewmodel for data (maybe an event happened?or onCreate?)
/*
* Copyright (C) 2017 The Android Open Source Project
*/
//a generic wrapper class that describes a data with a status
public class Resource<T> {
@NonNull
public final Status status;
@Nullable
public final T data;
@Nullable public final String message;
@tpakis
tpakis / MainActivity.java
Created February 3, 2017 23:41 — forked from udacityandroid/MainActivity.java
Android for Beginners : Menu Solution Code
package com.example.android.menu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {