Last active
November 18, 2019 23:42
-
-
Save marconvcm/7f70b6c4c47596f675d5eec341b292a5 to your computer and use it in GitHub Desktop.
View Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mysensor; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.LinearLayout; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.example.mysensor.model.Sensor; | |
import java.util.List; | |
import java.util.Random; | |
public class ListActivity extends AppCompatActivity implements SensorListDisplayLogic { | |
private SensorListViewModel viewModel; | |
private ProgressBar progressBar; | |
private LinearLayout linearLayout; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_list); | |
linearLayout = findViewById(R.id.linearLayout); | |
progressBar = findViewById(R.id.progressBar2); | |
viewModel = new MySensorListViewModel(this); | |
viewModel.requestSensorList(); | |
//setupCustomView(); | |
//setupRows(); | |
} | |
private void setupCustomView() { | |
View sensorView; | |
TextView textView; | |
for (int i = 0; i < 10; i++) { | |
sensorView = getLayoutInflater().inflate(R.layout.view_sensor, linearLayout, false); | |
textView = sensorView.findViewById(R.id.tvSensor); | |
textView.setText("Sensor " + i); | |
linearLayout.addView(sensorView); | |
} | |
} | |
private void setupRows() { | |
Random random = new Random(); | |
for (int i = 0; i < 100; i++) { | |
TextView blue = new TextView(this); | |
blue.setText("World" + i); | |
blue.setBackgroundColor( | |
Color.rgb( | |
random.nextInt(255), random.nextInt(255), random.nextInt(255))); | |
blue.setTextSize(30); | |
linearLayout.addView(blue, | |
LinearLayout.LayoutParams.MATCH_PARENT); | |
} | |
} | |
@Override | |
public void displaySensorList(List<Sensor> list) { | |
progressBar.setVisibility(View.GONE); | |
setTitle("MySensors"); | |
linearLayout.removeAllViews(); | |
View sensorView; | |
TextView textView; | |
for (Sensor sensor : list) { | |
sensorView = getLayoutInflater().inflate(R.layout.view_sensor, linearLayout, false); | |
textView = sensorView.findViewById(R.id.tvSensor); | |
textView.setText(String.format("Sensor %d", sensor.getId())); | |
linearLayout.addView(sensorView); | |
} | |
} | |
@Override | |
public void displayLoading() { | |
setTitle("Carregando..."); | |
progressBar.setVisibility(View.VISIBLE); | |
} | |
@Override | |
public void displayError(String error, int code) { | |
Toast.makeText(this, String.format("%s - %d", error, code), Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mysensor; | |
import android.os.Handler; | |
import com.example.mysensor.model.Sensor; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class MySensorListViewModel implements SensorListViewModel { | |
private Handler handler = new Handler(); | |
private List<Sensor> sensors = new ArrayList(); | |
private SensorListDisplayLogic displayLogic; | |
MySensorListViewModel(SensorListDisplayLogic displayLogic) { | |
this.displayLogic = displayLogic; | |
sensors.add(new Sensor(1, "Cozinha")); | |
sensors.add(new Sensor(2, "Quarto")); | |
} | |
@Override | |
public void requestSensorList() { | |
final Random random = new Random(); | |
displayLogic.displayLoading(); | |
handler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
if(random.nextInt(4) == 3) { | |
displayLogic.displayError("Algo deu errado", 100); | |
} else { | |
displayLogic.displaySensorList(sensors); | |
} | |
} | |
}, 5000); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mysensor.model; | |
public class Sensor { | |
private final int id; | |
private final String tag; | |
public Sensor(int id, String tag) { | |
this.id = id; | |
this.tag = tag; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getTag() { | |
return tag; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mysensor; | |
import com.example.mysensor.model.Sensor; | |
import java.util.List; | |
public interface SensorListDisplayLogic { | |
void displaySensorList(List<Sensor> list); | |
void displayLoading(); | |
void displayError(String error, int code); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mysensor; | |
public interface SensorListViewModel { | |
void requestSensorList(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment