Skip to content

Instantly share code, notes, and snippets.

Avatar

Chelsea Troy chelseatroy

View GitHub Profile
View WeeklySurveyFragment.java
...
private String extractResponseFor(View view) {
RadioGroup options = (RadioGroup) view.findViewById(R.id.radio_group);
int selection = options.getCheckedRadioButtonId();
if (selection == -1) {
options.setBackgroundColor(getActivity().getResources().getColor(R.color.form_validation));
isFormValid = false;
return getString(R.string.unknown);
} else {
View WeeklySurveyFragment
public class WeeklySurveyFragment extends SmartFragment implements WeeklySurveyView, OnWebserviceResponse {
...
@Override
public void onResume() {
super.onResume();
appetiteDecreaseSeverityResponse = getView().findViewById(R.id.appetite_decrease_severity_response);
appetiteDecreaseInterferenceResponse = getView().findViewById(R.id.appetite_decrease_interference_response);
nauseaFrequencyResponse = getView().findViewById(R.id.nausea_frequency_response);
nauseaSeverityResponse = getView().findViewById(R.id.nausea_severity_response);
View severity_question.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
@chelseatroy
chelseatroy / fragment_weekly_survey.xml
Created Jul 26, 2020
Including a Custom layout in an Existing Layout
View fragment_weekly_survey.xml
...
<include android:id="@+id/appetite_decrease_severity_response"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/severity_question"/>
...
@chelseatroy
chelseatroy / server.py
Created Jul 26, 2020
Committing the Entry as the Leader
View server.py
...
else:
if self.leader:
self.current_operation = string_operation
if self.current_operation.split(" ")[0] in ["set", "delete"]:
key_value_store.write_to_log(string_operation, term_absent=True)
broadcast(self, with_return_address(self, "append_entries ['" + self.current_operation + "']"))
while not self.current_operation_committed:
@chelseatroy
chelseatroy / server.py
Created Jul 26, 2020
Conditional Statement—Committing for Followers
View server.py
...
elif string_operation.split(" ")[0] == "commit_entries":
# followers do this to update their logs.
stringified_logs_to_append = string_operation.replace("commit_entries ", "")
print("Preparing to commit: " + stringified_logs_to_append)
logs_to_append = ast.literal_eval(stringified_logs_to_append)
[key_value_store.write_to_state_machine(command, term_absent=True) for command in logs_to_append]
response = "Commit entries call successful!"
@chelseatroy
chelseatroy / server.py
Created Jul 26, 2020
Marking Follower Servers Updated
View server.py
...
def mark_updated(self, server_name):
self.followers_with_update_status[server_name] = True
trues = len(list(filter(lambda x: x is True, self.followers_with_update_status.values())))
falses = len(list(filter(lambda x: x is False, self.followers_with_update_status.values())))
if trues >= falses:
print("Committing entry: " + self.current_operation)
self.current_operation_committed = True
@chelseatroy
chelseatroy / server.py
Created Jul 26, 2020
Conditional Statement—Responding to Successful Log Replication Requests
View server.py
...
elif string_operation == "Append entries call successful!":
if self.leader:
self.mark_updated(server_name)
send_pending = False
@chelseatroy
chelseatroy / server.py
Last active Jul 26, 2020
Keeping Track of Followers
View server.py
class Server:
...
self.followers_with_update_status = {}
self.current_operation = ''
self.current_operation_committed = False
for server_name in other_server_names(name):
self.followers_with_update_status[server_name] = False
@chelseatroy
chelseatroy / server.py
Last active Jul 26, 2020
Conditional Statement—Responding to append_entries Requests
View server.py
...
if string_operation.split(" ")[0] == "append_entries":
# followers do this to update their logs.
stringified_logs_to_append = string_operation.replace("append_entries ", "")
print("Preparing to append: " + stringified_logs_to_append)
logs_to_append = ast.literal_eval(stringified_logs_to_append)
[key_value_store.write_to_log(log, term_absent=True) for log in logs_to_append]
print("State machine after appending: " + str(key_value_store.data))
response = "Append entries call successful!"