Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aaronskiba patch 1 #7

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
26 changes: 26 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Android CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
21 changes: 21 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
}
39 changes: 39 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"project_info": {
"project_number": "520311220155",
"project_id": "cmput301firestore",
"storage_bucket": "cmput301firestore.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:520311220155:android:c59c1e78e27e014953869e",
"android_client_info": {
"package_name": "com.example.simpleparadox.listycity"
}
},
"oauth_client": [
{
"client_id": "520311220155-nh05qmeo58vsv948in26lcdtpjcnu3i1.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCBO4K4e_EdJRN4ePt5WIwl29e_kBAbv5U"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "520311220155-nh05qmeo58vsv948in26lcdtpjcnu3i1.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class City {
private String city;
private String province;

City(String city, String province){
public City(String city, String province){
this.city = city;
this.province = province;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
return view;

}

/**
* this function will get the size of list
* @return
*/
public int getCount() {
return cities.size();
}

/**
* this function will add a city object into the list
* @param city
*/
public void addCity(City city) {
cities.add(city);
return;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package com.example.simpleparadox.listycity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

Expand All @@ -20,35 +36,89 @@ public class MainActivity extends AppCompatActivity {

CustomList customList;

final String TAG = "Sample";
Button addCityButton;
EditText addCityEditText;
EditText addProvinceEditText;
FirebaseFirestore db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cityList = findViewById(R.id.city_list);

String []cities ={"Edmonton", "Vancouver", "Toronto", "Hamilton", "Denver", "Los Angeles"};
String []provinces = {"AB", "BC", "ON", "ON", "CO", "CA"};
addCityButton = findViewById(R.id.add_city_button);
addCityEditText = findViewById(R.id.add_city_field);
addProvinceEditText = findViewById(R.id.add_province_edit_text);


cityDataList = new ArrayList<>();

for(int i=0;i<cities.length;i++){
cityDataList.add((new City(cities[i], provinces[i])));
}

cityAdapter = new CustomList(this, cityDataList);

cityList.setAdapter(cityAdapter);

// dataList = new ArrayList<>();
// dataList.addAll(Arrays.asList(cities));
//
// cityAdapter = new ArrayAdapter<>(this, R.layout.content, dataList);
//
// cityList.setAdapter(cityAdapter);


db = FirebaseFirestore.getInstance();

final CollectionReference collectionReference = db.collection("Cities");


addCityButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) { // or Firestore View?

final String cityName = addCityEditText.getText().toString();
final String provinceName = addProvinceEditText.getText().toString();

HashMap<String, String> data = new HashMap<>();

if (cityName.length()>0 && provinceName.length()>0) {
data.put("Province Name", provinceName);
collectionReference
.document(cityName)
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// These are a method which gets executed when the task is succeeded
Log.d(TAG, "Data has been added successfully!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// These are a method which gets executed if there’s any problem
Log.d(TAG, "Data could not be added!" + e.toString());

addCityEditText.setText("");
addProvinceEditText.setText("");
}
});
}

}
});

collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable
FirebaseFirestoreException error) {

// Clear the old list
cityDataList.clear();
for(QueryDocumentSnapshot doc: queryDocumentSnapshots)
{
Log.d(TAG, String.valueOf(doc.getData().get("Province Name")));
String city = doc.getId();
String province = (String) doc.getData().get("Province Name");
cityDataList.add(new City(city, province)); // Adding the cities and provinces from FireStore
}
cityAdapter.notifyDataSetChanged(); // Notifying the adapter to render any new data fetched
// from the cloud
}
});

}

Expand Down
26 changes: 23 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,32 @@
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/add_city_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="8">
</EditText>
<EditText
android:id="@+id/add_province_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7">
</EditText>
<Button
android:id="@+id/add_city_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add City">
</Button>
</LinearLayout>
<ListView
android:id="@+id/city_list"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>

</LinearLayout>
26 changes: 26 additions & 0 deletions app/src/test/java/TestListyCity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import com.example.simpleparadox.listycity.City;
import com.example.simpleparadox.listycity.CustomList;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.ArrayList;

public class TestListyCity {

private CustomList list;

@Before
public void createList() {
list = new CustomList(null, new ArrayList<City>());
}

@Test
public void addCityTest() {
int listSize = list.getCount();
list.addCity(new City("Halifax", "NS"));
assertEquals(list.getCount(), listSize+1);
}

}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'

classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip