Skip to content

Commit

Permalink
v1,0: Initial commit. Starter code for Jetpack-MVVM codelab
Browse files Browse the repository at this point in the history
  • Loading branch information
jespitiaa committed Oct 5, 2021
1 parent 2f1bb48 commit 5a0b7d5
Show file tree
Hide file tree
Showing 50 changed files with 1,306 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
52 changes: 52 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdk 31
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.vinyls_jetpack_application"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
// Network invocations lib
implementation 'com.android.volley:volley:1.1.1'

testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.vinyls_jetpack_application

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.vinyls_jetpack_application", appContext.packageName)
}
}
25 changes: 25 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vinyls_jetpack_application">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Vinyls_Jetpack_Application">
<activity
android:name=".ui.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Vinyls_Jetpack_Application.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.example.vinyls_jetpack_application.network

import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONArray
import org.json.JSONObject
import java.awt.font.NumericShaper

class NetworkServiceAdapter constructor(context: Context) {
companion object{
const val BASE_URL= "https://back-vinyls-populated.herokuapp.com/"
var instance: NetworkServiceAdapter? = null
fun getInstance(context: Context) =
instance ?: synchronized(this) {
instance ?: NetworkServiceAdapter(context).also {
instance = it
}
}
}
private val requestQueue: RequestQueue by lazy {
// applicationContext keeps you from leaking the Activity or BroadcastReceiver if someone passes one in.
Volley.newRequestQueue(context.applicationContext)
}
fun getAlbums( onComplete:(resp:List<Album>)->Unit , onError: (error:VolleyError)->Unit){
requestQueue.add(getRequest("albums",
Response.Listener<String> { response ->
val resp = JSONArray(response)
val list = mutableListOf<Album>()
for (i in 0 until resp.length()) {
val item = resp.getJSONObject(i)
list.add(i, Album(albumId = item.getInt("id"),name = item.getString("name"), cover = item.getString("cover"), recordLabel = item.getString("recordLabel"), releaseDate = item.getString("releaseDate"), genre = item.getString("genre"), description = item.getString("description")))
}
onComplete(list)
},
Response.ErrorListener {
onError(it)
}))
}
fun getCollectors( onComplete:(resp:List<Collector>)->Unit , onError: (error:VolleyError)->Unit) {
requestQueue.add(getRequest("collectors",
Response.Listener<String> { response ->
Log.d("tagb", response)
val resp = JSONArray(response)
val list = mutableListOf<Collector>()
for (i in 0 until resp.length()) {
val item = resp.getJSONObject(i)
list.add(i, Collector(collectorId = item.getInt("id"),name = item.getString("name"), telephone = item.getString("telephone"), email = item.getString("email")))
}
onComplete(list)
},
Response.ErrorListener {
onError(it)
}))
}
fun getComments( albumId:Int, onComplete:(resp:List<Comment>)->Unit , onError: (error:VolleyError)->Unit) {
requestQueue.add(getRequest("albums/$albumId/comments",
Response.Listener<String> { response ->
val resp = JSONArray(response)
val list = mutableListOf<Comment>()
var item:JSONObject? = null
for (i in 0 until resp.length()) {
item = resp.getJSONObject(i)
Log.d("Response", item.toString())
list.add(i, Comment(albumId = albumId, rating = item.getInt("rating").toString(), description = item.getString("description")))
}
onComplete(list)
},
Response.ErrorListener {
onError(it)
}))
}
fun postComment(body: JSONObject, albumId: Int, onComplete:(resp:JSONObject)->Unit , onError: (error:VolleyError)->Unit){
requestQueue.add(postRequest("albums/$albumId/comments",
body,
Response.Listener<JSONObject> { response ->
onComplete(response)
},
Response.ErrorListener {
onError(it)
}))
}
private fun getRequest(path:String, responseListener: Response.Listener<String>, errorListener: Response.ErrorListener): StringRequest {
return StringRequest(Request.Method.GET, BASE_URL+path, responseListener,errorListener)
}
private fun postRequest(path: String, body: JSONObject, responseListener: Response.Listener<JSONObject>, errorListener: Response.ErrorListener ):JsonObjectRequest{
return JsonObjectRequest(Request.Method.POST, BASE_URL+path, body, responseListener, errorListener)
}
private fun putRequest(path: String, body: JSONObject, responseListener: Response.Listener<JSONObject>, errorListener: Response.ErrorListener ):JsonObjectRequest{
return JsonObjectRequest(Request.Method.PUT, BASE_URL+path, body, responseListener, errorListener)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.vinyls_jetpack_application.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.example.vinyls_jetpack_application.R

/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class AlbumFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return null
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
}


override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val activity = requireNotNull(this.activity) {
"You can only access the viewModel after onActivityCreated()"
}
activity.actionBar?.title = getString(R.string.title_albums)
}

override fun onDestroyView() {
super.onDestroyView()
}

private fun onNetworkError() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.vinyls_jetpack_application.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.vinyls_jetpack_application.R

/**
* A simple [Fragment] subclass as the default destination in the navigation.
*/
class CollectorFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return null
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val activity = requireNotNull(this.activity) {
"You can only access the viewModel after onActivityCreated()"
}
activity.actionBar?.title = getString(R.string.title_collectors)
}
override fun onDestroyView() {
super.onDestroyView()
}

private fun onNetworkError() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.vinyls_jetpack_application.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.vinyls_jetpack_application.R

class CommentFragment: Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return view
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val activity = requireNotNull(this.activity) {
"You can only access the viewModel after onActivityCreated()"
}
activity.actionBar?.title = getString(R.string.title_comments)
}



override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
activity?.actionBar?.title = getString(R.string.title_comments)
}
override fun onDestroyView() {
super.onDestroyView()
}

private fun onNetworkError() {
}

}
Loading

0 comments on commit 5a0b7d5

Please sign in to comment.