├── .gitignore ├── .idea ├── .gitignore ├── .name ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── compiler.xml ├── deploymentTargetDropDown.xml ├── dictionaries │ └── theapache64.xml ├── gradle.xml ├── jarRepositories.xml ├── markdown-navigator-enh.xml ├── markdown-navigator.xml ├── misc.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── theapache64 │ │ └── notes │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── theapache64 │ │ │ └── notes │ │ │ ├── NotesApp.kt │ │ │ ├── data │ │ │ ├── NotesRepo.kt │ │ │ └── remote │ │ │ │ ├── ApiInterface.kt │ │ │ │ ├── addnote │ │ │ │ └── AddNoteRequest.kt │ │ │ │ └── getnotes │ │ │ │ └── Note.kt │ │ │ ├── di │ │ │ └── modules │ │ │ │ └── NetworkModule.kt │ │ │ ├── features │ │ │ ├── addnote │ │ │ │ ├── AddNoteActivity.kt │ │ │ │ └── AddNoteViewModel.kt │ │ │ └── notes │ │ │ │ ├── NotesActivity.kt │ │ │ │ ├── NotesAdapter.kt │ │ │ │ └── NotesViewModel.kt │ │ │ └── utils │ │ │ ├── Const.kt │ │ │ ├── calladapter │ │ │ └── flow │ │ │ │ ├── FlowResourceCallAdapter.kt │ │ │ │ ├── FlowResourceCallAdapterFactory.kt │ │ │ │ └── Resource.kt │ │ │ └── livedata │ │ │ └── SingleLiveEvent.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_add_note.xml │ │ ├── activity_main.xml │ │ └── item_note.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── com │ └── theapache64 │ └── notes │ └── ExampleUnitTest.kt ├── build.gradle ├── demo.png ├── gpm.json ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── montage.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Notes -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 20 | 22 | 23 | 25 | 26 | 138 | 139 | 141 | 142 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/deploymentTargetDropDown.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/dictionaries/theapache64.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | retrosheet 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | 34 | 35 | 39 | 40 | -------------------------------------------------------------------------------- /.idea/markdown-navigator-enh.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 20 | 21 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # notes 🗒️ 2 | 3 | ![](demo.png) 4 | 5 | > A sample note taking app, to demonstrate the [Retrosheet](https://github.com/theapache64/retrosheet) library. 6 | 7 | ## Run 💻 8 | 9 | ### Data Source 10 | 11 | - [View Google Sheet](https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/) 12 | - [View Google Form](https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link) 13 | 14 | ![](https://i.imgur.com/spCDzb2.png) 15 | 16 | ### Output 17 | 18 | ![](montage.png) 19 | 20 | 21 | ## Author ✍️ 22 | 23 | - theapache64 24 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | id 'kotlin-kapt' 5 | id 'dagger.hilt.android.plugin' 6 | } 7 | 8 | android { 9 | compileSdkVersion 31 10 | 11 | defaultConfig { 12 | applicationId "com.theapache64.notes" 13 | minSdkVersion 16 14 | targetSdkVersion 31 15 | versionCode 1 16 | versionName "1.0" 17 | 18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | compileOptions { 28 | sourceCompatibility JavaVersion.VERSION_1_8 29 | targetCompatibility JavaVersion.VERSION_1_8 30 | } 31 | kotlinOptions { 32 | jvmTarget = '1.8' 33 | } 34 | 35 | buildFeatures{ 36 | dataBinding true 37 | } 38 | } 39 | 40 | dependencies { 41 | 42 | implementation 'androidx.core:core-ktx:1.6.0' 43 | implementation 'androidx.appcompat:appcompat:1.3.1' 44 | implementation 'com.google.android.material:material:1.4.0' 45 | implementation 'androidx.constraintlayout:constraintlayout:2.1.1' 46 | 47 | // Hilt 48 | implementation "com.google.dagger:hilt-android:$hilt_version" 49 | kapt "com.google.dagger:hilt-android-compiler:$hilt_version" 50 | implementation "androidx.hilt:hilt-lifecycle-viewmodel:$androidx_hilt_version" 51 | kapt "androidx.hilt:hilt-lifecycle-viewmodel:$androidx_hilt_version" 52 | kapt "androidx.hilt:hilt-compiler:$androidx_hilt_version" 53 | 54 | // Timber : No-nonsense injectable logging. 55 | implementation 'com.jakewharton.timber:timber:4.7.1' 56 | 57 | // Activity Kotlin Extensions : Kotlin extensions for 'activity' artifact 58 | implementation 'androidx.activity:activity-ktx:1.4.0-beta01' 59 | 60 | // Moshi : Moshi 61 | implementation 'com.squareup.moshi:moshi:1.12.0' 62 | 63 | // Retrofit : A type-safe HTTP client for Android and Java. 64 | implementation 'com.squareup.retrofit2:retrofit:2.9.0' 65 | 66 | // Retrosheet 67 | implementation 'com.github.theapache64:retrosheet:2.0.0-beta02' 68 | 69 | // Kotlinx Coroutines Core : Coroutines support libraries for Kotlin 70 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2' 71 | 72 | // LiveData Kotlin Extensions : Kotlin extensions for 'livedata' artifact 73 | implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0-rc01' 74 | 75 | // Moshi Kotlin Codegen : Moshi Kotlin Codegen 76 | kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.12.0' 77 | 78 | // Converter: Moshi : A Retrofit Converter which uses Moshi for serialization. 79 | implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' 80 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/theapache64/notes/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.theapache64.names", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/NotesApp.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes 2 | 3 | import android.app.Application 4 | import androidx.appcompat.app.AppCompatDelegate 5 | import dagger.hilt.android.HiltAndroidApp 6 | import timber.log.Timber 7 | 8 | /** 9 | * Created by theapache64 : Aug 29 Sat,2020 @ 10:04 10 | */ 11 | @HiltAndroidApp 12 | class NotesApp : Application() { 13 | override fun onCreate() { 14 | super.onCreate() 15 | 16 | Timber.plant(Timber.DebugTree()) 17 | AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/data/NotesRepo.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.data 2 | 3 | import com.theapache64.notes.data.remote.ApiInterface 4 | import com.theapache64.notes.data.remote.addnote.AddNoteRequest 5 | import javax.inject.Inject 6 | 7 | /** 8 | * Created by theapache64 : Aug 29 Sat,2020 @ 09:59 9 | */ 10 | class NotesRepo @Inject constructor( 11 | private val apiInterface: ApiInterface 12 | ) { 13 | fun getNotes() = apiInterface.getNotes() 14 | fun addNote(addNoteRequest: AddNoteRequest) = apiInterface.addNote(addNoteRequest) 15 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/data/remote/ApiInterface.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.data.remote 2 | 3 | import com.github.theapache64.retrosheet.annotations.Read 4 | import com.github.theapache64.retrosheet.annotations.Write 5 | import com.theapache64.notes.data.remote.addnote.AddNoteRequest 6 | import com.theapache64.notes.data.remote.getnotes.Note 7 | import com.theapache64.notes.utils.Const 8 | import com.theapache64.notes.utils.calladapter.flow.Resource 9 | import kotlinx.coroutines.flow.Flow 10 | import retrofit2.http.Body 11 | import retrofit2.http.GET 12 | import retrofit2.http.POST 13 | 14 | /** 15 | * Created by theapache64 : Aug 29 Sat,2020 @ 10:14 16 | */ 17 | interface ApiInterface { 18 | 19 | 20 | @Read("SELECT * ORDER BY created_at DESC") 21 | @GET("notes") 22 | fun getNotes(): Flow>> // you can also use suspend and return List 23 | 24 | @Write 25 | @POST(Const.ADD_NOTE_ENDPOINT) 26 | fun addNote(@Body note: AddNoteRequest): Flow> // you can also use suspend and return AddNoteRequest 27 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/data/remote/addnote/AddNoteRequest.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.data.remote.addnote 2 | 3 | import com.squareup.moshi.Json 4 | import com.squareup.moshi.JsonClass 5 | 6 | /** 7 | * Created by theapache64 : Aug 29 Sat,2020 @ 10:13 8 | */ 9 | @JsonClass(generateAdapter = true) 10 | data class AddNoteRequest( 11 | @Json(name = "Title") 12 | val title: String, 13 | @Json(name = "Description") 14 | val description: String 15 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/data/remote/getnotes/Note.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.data.remote.getnotes 2 | 3 | import com.squareup.moshi.Json 4 | import com.squareup.moshi.JsonClass 5 | 6 | /** 7 | * Created by theapache64 : Aug 29 Sat,2020 @ 10:13 8 | */ 9 | @JsonClass(generateAdapter = true) 10 | data class Note( 11 | @Json(name = "created_at") 12 | val createdAt: String?, 13 | @Json(name = "title") 14 | val title: String, 15 | @Json(name = "description") 16 | val description: String 17 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/di/modules/NetworkModule.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.di.modules 2 | 3 | import com.github.theapache64.retrosheet.RetrosheetInterceptor 4 | import com.theapache64.notes.data.remote.ApiInterface 5 | import com.theapache64.notes.utils.Const 6 | import com.theapache64.notes.utils.calladapter.flow.FlowResourceCallAdapterFactory 7 | import dagger.Module 8 | import dagger.Provides 9 | import dagger.hilt.InstallIn 10 | import dagger.hilt.components.SingletonComponent 11 | import okhttp3.OkHttpClient 12 | import retrofit2.Retrofit 13 | import retrofit2.converter.moshi.MoshiConverterFactory 14 | 15 | /** 16 | * Created by theapache64 : Aug 29 Sat,2020 @ 10:14 17 | */ 18 | @Module 19 | @InstallIn(SingletonComponent::class) 20 | class NetworkModule { 21 | 22 | @Provides 23 | fun provideRetrosheetInterceptor(): RetrosheetInterceptor { 24 | return RetrosheetInterceptor.Builder() 25 | // To Read 26 | .addSheet("notes", "created_at", "title", "description") 27 | // To Write 28 | .addForm( 29 | Const.ADD_NOTE_ENDPOINT, 30 | "https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link" 31 | ) 32 | .build() 33 | } 34 | 35 | @Provides 36 | fun provideOkHttpClient(retrosheetInterceptor: RetrosheetInterceptor): OkHttpClient { 37 | return OkHttpClient.Builder() 38 | .addInterceptor(retrosheetInterceptor) 39 | .build() 40 | } 41 | 42 | @Provides 43 | fun provideRetrofit( 44 | okHttpClient: OkHttpClient 45 | ): Retrofit { 46 | return Retrofit.Builder() 47 | .baseUrl("https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/") 48 | .client(okHttpClient) 49 | .addCallAdapterFactory(FlowResourceCallAdapterFactory()) 50 | .addConverterFactory(MoshiConverterFactory.create()) 51 | .build() 52 | } 53 | 54 | @Provides 55 | fun provideApi(retrofit: Retrofit): ApiInterface { 56 | return retrofit 57 | .create(ApiInterface::class.java) 58 | } 59 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/features/addnote/AddNoteActivity.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.features.addnote 2 | 3 | import android.app.Activity 4 | import android.content.Context 5 | import android.content.Intent 6 | import android.os.Bundle 7 | import android.view.MenuItem 8 | import android.widget.Toast 9 | import androidx.activity.viewModels 10 | import androidx.appcompat.app.AppCompatActivity 11 | import androidx.databinding.DataBindingUtil 12 | import androidx.lifecycle.Observer 13 | import com.theapache64.notes.R 14 | import com.theapache64.notes.databinding.ActivityAddNoteBinding 15 | import com.theapache64.notes.utils.calladapter.flow.Resource 16 | import dagger.hilt.android.AndroidEntryPoint 17 | 18 | @AndroidEntryPoint 19 | class AddNoteActivity : AppCompatActivity() { 20 | companion object { 21 | const val RQ_CODE = 2332 22 | fun getStartIntent(context: Context): Intent { 23 | return Intent(context, AddNoteActivity::class.java).apply { 24 | // data goes here 25 | } 26 | } 27 | } 28 | 29 | private val viewModel: AddNoteViewModel by viewModels() 30 | 31 | override fun onCreate(savedInstanceState: Bundle?) { 32 | super.onCreate(savedInstanceState) 33 | val binding = 34 | DataBindingUtil.setContentView(this, R.layout.activity_add_note) 35 | 36 | binding.viewModel = viewModel 37 | 38 | supportActionBar?.setDisplayHomeAsUpEnabled(true) 39 | 40 | viewModel.addNoteResponse.observe(this, Observer { 41 | when (it) { 42 | is Resource.Loading -> { 43 | binding.bAddNote.isEnabled = false 44 | } 45 | is Resource.Success -> { 46 | Toast.makeText(this, "Note added", Toast.LENGTH_SHORT).show(); 47 | binding.bAddNote.isEnabled = true 48 | setResult(Activity.RESULT_OK) 49 | finish() 50 | } 51 | is Resource.Error -> { 52 | binding.bAddNote.isEnabled = true 53 | Toast.makeText(this, it.errorData, Toast.LENGTH_SHORT).show(); 54 | } 55 | } 56 | }) 57 | } 58 | 59 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 60 | return if (item.itemId == android.R.id.home) { 61 | onBackPressed() 62 | true 63 | } else { 64 | super.onOptionsItemSelected(item) 65 | } 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/features/addnote/AddNoteViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.features.addnote 2 | 3 | import androidx.lifecycle.MutableLiveData 4 | import androidx.lifecycle.ViewModel 5 | import androidx.lifecycle.asLiveData 6 | import androidx.lifecycle.switchMap 7 | import com.theapache64.notes.data.NotesRepo 8 | import com.theapache64.notes.data.remote.addnote.AddNoteRequest 9 | import com.theapache64.notes.utils.calladapter.flow.Resource 10 | import dagger.hilt.android.lifecycle.HiltViewModel 11 | import kotlinx.coroutines.flow.onEach 12 | import javax.inject.Inject 13 | 14 | /** 15 | * Created by theapache64 : Aug 30 Sun,2020 @ 10:46 16 | */ 17 | @HiltViewModel 18 | class AddNoteViewModel @Inject constructor( 19 | private val notesRepo: NotesRepo 20 | ) : ViewModel() { 21 | 22 | var title = "" 23 | var description = "" 24 | 25 | private val _addNoteRequest = MutableLiveData() 26 | val addNoteResponse = _addNoteRequest.switchMap { it -> 27 | notesRepo.addNote(it) 28 | .onEach { resp -> 29 | if (resp is Resource.Success) { 30 | title = "" 31 | description = "" 32 | } 33 | } 34 | .asLiveData() 35 | } 36 | 37 | fun onAddNoteClicked() { 38 | _addNoteRequest.value = AddNoteRequest( 39 | title, 40 | description 41 | ) 42 | } 43 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/features/notes/NotesActivity.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.features.notes 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.view.View 7 | import android.widget.Toast 8 | import androidx.activity.viewModels 9 | import androidx.appcompat.app.AppCompatActivity 10 | import androidx.databinding.DataBindingUtil 11 | import androidx.lifecycle.Observer 12 | import com.theapache64.notes.R 13 | import com.theapache64.notes.databinding.ActivityMainBinding 14 | import com.theapache64.notes.features.addnote.AddNoteActivity 15 | import com.theapache64.notes.utils.calladapter.flow.Resource 16 | import dagger.hilt.android.AndroidEntryPoint 17 | 18 | @AndroidEntryPoint 19 | class NotesActivity : AppCompatActivity() { 20 | 21 | private val viewModel: NotesViewModel by viewModels() 22 | 23 | override fun onCreate(savedInstanceState: Bundle?) { 24 | super.onCreate(savedInstanceState) 25 | val binding = 26 | DataBindingUtil.setContentView( 27 | this, 28 | R.layout.activity_main 29 | ) 30 | 31 | binding.viewModel = viewModel 32 | 33 | viewModel.notes.observe(this, { 34 | when (it) { 35 | 36 | is Resource.Loading -> { 37 | Toast.makeText(this, "Loading...", Toast.LENGTH_SHORT).show(); 38 | binding.rvNames.visibility = View.INVISIBLE 39 | } 40 | 41 | is Resource.Success -> { 42 | val adapter = NotesAdapter(this, it.data) { position -> 43 | Toast.makeText(this, "Clicked on '$position'", Toast.LENGTH_SHORT).show(); 44 | } 45 | 46 | binding.rvNames.adapter = adapter 47 | binding.rvNames.visibility = View.VISIBLE 48 | } 49 | 50 | is Resource.Error -> { 51 | Toast.makeText(this, it.errorData, Toast.LENGTH_SHORT).show(); 52 | } 53 | } 54 | }) 55 | 56 | viewModel.shouldLaunchAddNote.observe(this, { 57 | if (it) { 58 | // TODO: 59 | startActivityForResult( 60 | AddNoteActivity.getStartIntent(this), 61 | AddNoteActivity.RQ_CODE 62 | ) 63 | } 64 | }) 65 | } 66 | 67 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 68 | if (requestCode == AddNoteActivity.RQ_CODE && resultCode == Activity.RESULT_OK) { 69 | // new note added so refresh 70 | viewModel.onNewNoteAdded() 71 | } else { 72 | super.onActivityResult(requestCode, resultCode, data) 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/features/notes/NotesAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.features.notes 2 | 3 | 4 | import android.content.Context 5 | import android.view.LayoutInflater 6 | import android.view.ViewGroup 7 | import androidx.recyclerview.widget.RecyclerView 8 | import com.theapache64.notes.data.remote.getnotes.Note 9 | import com.theapache64.notes.databinding.ItemNoteBinding 10 | 11 | /** 12 | * Created by theapache64 : Aug 29 Sat,2020 @ 16:37 13 | */ 14 | class NotesAdapter( 15 | context: Context, 16 | private val notes: List, 17 | private val callback: (position: Int) -> Unit 18 | ) : RecyclerView.Adapter() { 19 | 20 | private val inflater = LayoutInflater.from(context) 21 | 22 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { 23 | val binding = ItemNoteBinding.inflate(inflater, parent, false) 24 | return ViewHolder(binding) 25 | } 26 | 27 | override fun getItemCount(): Int = notes.size 28 | 29 | override fun onBindViewHolder(holder: ViewHolder, position: Int) { 30 | val note = notes[position] 31 | holder.binding.note = note 32 | } 33 | 34 | inner class ViewHolder(val binding: ItemNoteBinding) : RecyclerView.ViewHolder(binding.root) { 35 | init { 36 | binding.root.setOnClickListener { 37 | callback(layoutPosition) 38 | } 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/features/notes/NotesViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.features.notes 2 | 3 | import androidx.hilt.lifecycle.ViewModelInject 4 | import androidx.lifecycle.* 5 | import com.theapache64.notes.data.NotesRepo 6 | import com.theapache64.notes.data.remote.getnotes.Note 7 | import com.theapache64.notes.utils.calladapter.flow.Resource 8 | import com.theapache64.notes.utils.livedata.SingleLiveEvent 9 | import dagger.hilt.android.lifecycle.HiltViewModel 10 | import timber.log.Timber 11 | import javax.inject.Inject 12 | 13 | /** 14 | * Created by theapache64 : Aug 29 Sat,2020 @ 09:59 15 | */ 16 | @HiltViewModel 17 | class NotesViewModel @Inject constructor( 18 | private val notesRepo: NotesRepo 19 | ) : ViewModel() { 20 | 21 | private val _shouldLoadNotes = MutableLiveData() 22 | val notes: LiveData>> = _shouldLoadNotes.switchMap { 23 | notesRepo.getNotes().asLiveData() 24 | } 25 | 26 | private val _shouldLaunchAddNote = SingleLiveEvent() 27 | val shouldLaunchAddNote: LiveData = _shouldLaunchAddNote 28 | 29 | init { 30 | _shouldLoadNotes.value = true 31 | } 32 | 33 | fun onAddNameClicked() { 34 | Timber.d("onAddNameClicked: Add name clicked ") 35 | _shouldLaunchAddNote.value = true 36 | } 37 | 38 | fun onRefreshClicked() { 39 | Timber.d("onRefreshClicked: On refresh clicked") 40 | _shouldLoadNotes.value = true 41 | } 42 | 43 | fun onNewNoteAdded() { 44 | _shouldLoadNotes.value = true 45 | } 46 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/utils/Const.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.utils 2 | 3 | /** 4 | * Created by theapache64 : Aug 30 Sun,2020 @ 11:56 5 | */ 6 | object Const { 7 | const val ADD_NOTE_ENDPOINT = "add_note" 8 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/utils/calladapter/flow/FlowResourceCallAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.utils.calladapter.flow 2 | 3 | import com.theapache64.notes.utils.calladapter.flow.Resource.Error 4 | import com.theapache64.notes.utils.calladapter.flow.Resource.Success 5 | import kotlinx.coroutines.ExperimentalCoroutinesApi 6 | import kotlinx.coroutines.flow.Flow 7 | import kotlinx.coroutines.flow.catch 8 | import kotlinx.coroutines.flow.flow 9 | import retrofit2.Call 10 | import retrofit2.CallAdapter 11 | import retrofit2.awaitResponse 12 | import java.lang.reflect.Type 13 | 14 | /** 15 | * To convert retrofit response to Flow>. 16 | * Inspired from FlowCallAdapterFactory 17 | */ 18 | class FlowResourceCallAdapter( 19 | private val responseType: Type, 20 | private val isSelfExceptionHandling: Boolean 21 | ) : CallAdapter>> { 22 | 23 | override fun responseType() = responseType 24 | 25 | @ExperimentalCoroutinesApi 26 | override fun adapt(call: Call) = flow> { 27 | 28 | // Firing loading resource 29 | emit(Resource.Loading()) 30 | 31 | val resp = call.awaitResponse() 32 | 33 | if (resp.isSuccessful) { 34 | resp.body()?.let { data -> 35 | // Success 36 | emit(Success(null, data)) 37 | } ?: kotlin.run { 38 | // Error 39 | emit(Error("Response can't be null")) 40 | } 41 | } else { 42 | // Error 43 | val errorBody = resp.message() 44 | emit(Error(errorBody)) 45 | } 46 | 47 | }.catch { error: Throwable -> 48 | if (isSelfExceptionHandling) { 49 | emit(Error(error.message ?: "Something went wrong")) 50 | } else { 51 | throw error 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/utils/calladapter/flow/FlowResourceCallAdapterFactory.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.utils.calladapter.flow 2 | 3 | 4 | import kotlinx.coroutines.flow.Flow 5 | import retrofit2.CallAdapter 6 | import retrofit2.CallAdapter.Factory 7 | import retrofit2.Retrofit 8 | import java.lang.reflect.ParameterizedType 9 | import java.lang.reflect.Type 10 | 11 | class FlowResourceCallAdapterFactory( 12 | private val isSelfExceptionHandling: Boolean = true 13 | ) : Factory() { 14 | override fun get( 15 | returnType: Type, 16 | annotations: Array, 17 | retrofit: Retrofit 18 | ): CallAdapter<*, *>? { 19 | if (getRawType(returnType) != Flow::class.java) { 20 | return null 21 | } 22 | val observableType = getParameterUpperBound(0, returnType as ParameterizedType) 23 | val rawObservableType = getRawType(observableType) 24 | require(rawObservableType == Resource::class.java) { "type must be a resource" } 25 | require(observableType is ParameterizedType) { "resource must be parameterized" } 26 | val bodyType = getParameterUpperBound(0, observableType) 27 | return FlowResourceCallAdapter( 28 | bodyType, 29 | isSelfExceptionHandling 30 | ) 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/utils/calladapter/flow/Resource.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.utils.calladapter.flow 2 | 3 | /** 4 | * Created by theapache64 : Jul 26 Sun,2020 @ 13:22 5 | */ 6 | sealed class Resource { 7 | 8 | class Loading : Resource() 9 | 10 | data class Success( 11 | val message: String?, 12 | val data: T 13 | ) : Resource() 14 | 15 | data class Error( 16 | val errorData: String 17 | ) : Resource() 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/theapache64/notes/utils/livedata/SingleLiveEvent.kt: -------------------------------------------------------------------------------- 1 | package com.theapache64.notes.utils.livedata 2 | 3 | import android.util.Log 4 | import androidx.annotation.MainThread 5 | import androidx.lifecycle.LifecycleOwner 6 | import androidx.lifecycle.MutableLiveData 7 | import androidx.lifecycle.Observer 8 | import java.util.concurrent.atomic.AtomicBoolean 9 | 10 | /** 11 | * Normally LiveData can be fired at many times, but this special live data wrapper only fires once. 12 | */ 13 | open class SingleLiveEvent : MutableLiveData() { 14 | 15 | private val pending = AtomicBoolean(false) 16 | 17 | @MainThread 18 | override fun observe(owner: LifecycleOwner, observer: Observer) { 19 | if (hasActiveObservers()) { 20 | Log.w(TAG, "Multiple observers registered but only one will be notified of changes.") 21 | } 22 | 23 | // Observe the internal MutableLiveData 24 | super.observe(owner, Observer { t -> 25 | if (pending.compareAndSet(true, false)) { 26 | observer.onChanged(t) 27 | } 28 | }) 29 | } 30 | 31 | 32 | @MainThread 33 | override fun setValue(t: T?) { 34 | pending.set(true) 35 | super.setValue(t) 36 | } 37 | 38 | 39 | override fun postValue(value: T) { 40 | pending.set(true) 41 | super.postValue(value) 42 | } 43 | 44 | /** 45 | * Used for cases where T is Void, to make calls cleaner. 46 | */ 47 | @MainThread 48 | fun call() { 49 | value = null 50 | } 51 | 52 | companion object { 53 | private const val TAG = "SingleLiveEvent" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_add_note.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 17 | 18 | 19 | 28 | 29 | 34 | 35 | 36 | 37 | 46 | 47 | 52 | 53 | 54 |