Unverified Commit 0c726f03 authored by Dylan Jones's avatar Dylan Jones
Browse files

do some networking

parent 8ef821e8
Pipeline #4784 passed with stages
in 3 minutes and 50 seconds
...@@ -40,6 +40,11 @@ dependencies { ...@@ -40,6 +40,11 @@ dependencies {
// insert added libraries here // insert added libraries here
implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.12'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:runner:1.2.0'
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
<dist:module dist:instant="true" /> <dist:module dist:instant="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
......
package srct.whatsopen package srct.whatsopen
import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.TextView
import android.widget.Toast
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import srct.whatsopen.util.WhatsOpenService
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private var service: WhatsOpenService? = null
private var refreshLayout: SwipeRefreshLayout? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
fab.setOnClickListener { view -> val retrofit = Retrofit.Builder()
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .baseUrl("https://api.srct.gmu.edu/whatsopen/v2/")
.setAction("Action", null).show() .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
} .addConverterFactory(GsonConverterFactory.create())
.build()
service = retrofit.create(WhatsOpenService::class.java)
// setup refresh listener
refreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipe_refresh)
refreshLayout!!.setOnRefreshListener(this::refresh)
refresh()
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
...@@ -35,4 +53,25 @@ class MainActivity : AppCompatActivity() { ...@@ -35,4 +53,25 @@ class MainActivity : AppCompatActivity() {
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
} }
} }
@SuppressLint("CheckResult")
private fun refresh() {
refreshLayout!!.isRefreshing = true
service!!.getData()
.subscribeOn(Schedulers.io())
.doOnError { err ->
Toast.makeText(
applicationContext,
"Network Error: $err",
Toast.LENGTH_SHORT
).show()
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe { facilities ->
run {
findViewById<TextView>(R.id.main_text).text = facilities.toString()
refreshLayout!!.isRefreshing = false
}
}
}
} }
package srct.whatsopen.util
import io.reactivex.Observable
import retrofit2.http.GET
import srct.whatsopen.model.Facility
interface WhatsOpenService {
@GET("facilities")
fun getData(): Observable<List<Facility>>
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity"> tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout <androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar" android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="@dimen/app_bar_height"
android:layout_width="match_parent" android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay"> android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<com.google.android.material.appbar.CollapsingToolbarLayout <TextView
android:id="@+id/toolbar_layout"
android:fitsSystemWindows="true"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
app:toolbarId="@+id/toolbar" android:text="@string/app_name"
app:layout_scrollFlags="scroll|exitUntilCollapsed" android:textColor="@android:color/primary_text_dark"
app:contentScrim="?attr/colorPrimary"> android:textSize="18sp" />
</androidx.appcompat.widget.Toolbar>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_scrolling" /> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<com.google.android.material.floatingactionbutton.FloatingActionButton <ScrollView
android:id="@+id/fab" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="match_parent">
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin" <LinearLayout
app:layout_anchor="@id/app_bar" android:layout_width="match_parent"
app:layout_anchorGravity="bottom|end" android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_dialog_email" /> android:orientation="vertical" >
<TextView
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text" />
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
\ No newline at end of file </LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/large_text" />
</androidx.core.widget.NestedScrollView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="colorPrimary">#008577</color> <color name="colorPrimary">#006633</color>
<color name="colorPrimaryDark">#00574B</color> <color name="colorPrimaryDark">#006633</color>
<color name="colorAccent">#D81B60</color> <color name="colorAccent">#FBC02D</color>
</resources> </resources>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment