├── .gitignore ├── .idea ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── net │ │ └── simplifiedcoding │ │ └── mvvmsampleapp │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── net │ │ │ └── simplifiedcoding │ │ │ └── mvvmsampleapp │ │ │ ├── MVVMApplication.kt │ │ │ ├── data │ │ │ ├── db │ │ │ │ ├── AppDatabase.kt │ │ │ │ ├── QuoteDao.kt │ │ │ │ ├── UserDao.kt │ │ │ │ └── entities │ │ │ │ │ ├── Quote.kt │ │ │ │ │ └── User.kt │ │ │ ├── network │ │ │ │ ├── MyApi.kt │ │ │ │ ├── NetworkConnectionInterceptor.kt │ │ │ │ ├── SafeApiRequest.kt │ │ │ │ └── responses │ │ │ │ │ ├── AuthResponse.kt │ │ │ │ │ └── QuotesResponse.kt │ │ │ ├── preferences │ │ │ │ └── PreferenceProvider.kt │ │ │ └── repositories │ │ │ │ ├── QuotesRepository.kt │ │ │ │ └── UserRepository.kt │ │ │ ├── ui │ │ │ ├── auth │ │ │ │ ├── AuthViewModel.kt │ │ │ │ ├── AuthViewModelFactory.kt │ │ │ │ ├── LoginActivity.kt │ │ │ │ └── SignupActivity.kt │ │ │ └── home │ │ │ │ ├── HomeActivity.kt │ │ │ │ ├── profile │ │ │ │ ├── ProfileFragment.kt │ │ │ │ ├── ProfileViewModel.kt │ │ │ │ └── ProfileViewModelFactory.kt │ │ │ │ └── quotes │ │ │ │ ├── QuoteItem.kt │ │ │ │ ├── QuotesFragment.kt │ │ │ │ ├── QuotesViewModel.kt │ │ │ │ └── QuotesViewModelFactory.kt │ │ │ └── util │ │ │ ├── Coroutines.kt │ │ │ ├── Delegates.kt │ │ │ ├── Exceptions.kt │ │ │ └── ViewUtils.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── edit_text_round_gray_background.xml │ │ ├── ic_app_logo.xml │ │ ├── ic_email.xml │ │ ├── ic_launcher_background.xml │ │ ├── ic_lock.xml │ │ └── ic_name.xml │ │ ├── layout │ │ ├── activity_home.xml │ │ ├── activity_login.xml │ │ ├── activity_signup.xml │ │ ├── item_quote.xml │ │ ├── profile_fragment.xml │ │ └── quotes_fragment.xml │ │ ├── menu │ │ └── nav_menu.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 │ │ ├── navigation │ │ └── nav_graph.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── net │ └── simplifiedcoding │ └── mvvmsampleapp │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── 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 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 | 116 | 118 |
119 |
-------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | //kotlin kapt and navigation safeargs plugin 8 | apply plugin: 'kotlin-kapt' 9 | 10 | apply plugin: "androidx.navigation.safeargs" 11 | 12 | android { 13 | compileSdkVersion 29 14 | defaultConfig { 15 | applicationId "net.simplifiedcoding.mvvmsampleapp" 16 | minSdkVersion 26 17 | targetSdkVersion 29 18 | versionCode 1 19 | versionName "1.0" 20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | dataBinding { 29 | enabled = true 30 | } 31 | } 32 | 33 | dependencies { 34 | implementation fileTree(dir: 'libs', include: ['*.jar']) 35 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 36 | implementation 'androidx.appcompat:appcompat:1.1.0' 37 | implementation 'androidx.core:core-ktx:1.2.0' 38 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 39 | implementation 'androidx.legacy:legacy-support-v4:1.0.0' 40 | implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' 41 | testImplementation 'junit:junit:4.12' 42 | androidTestImplementation 'androidx.test:runner:1.2.0' 43 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 44 | 45 | //Retrofit and GSON 46 | implementation 'com.squareup.retrofit2:retrofit:2.6.2' 47 | implementation 'com.squareup.retrofit2:converter-gson:2.6.2' 48 | 49 | //Kotlin Coroutines 50 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3" 51 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3" 52 | 53 | // ViewModel and LiveData 54 | implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" 55 | 56 | //New Material Design 57 | implementation 'com.google.android.material:material:1.2.0-alpha06' 58 | 59 | //Kodein Dependency Injection 60 | implementation "org.kodein.di:kodein-di-generic-jvm:6.2.1" 61 | implementation "org.kodein.di:kodein-di-framework-android-x:6.2.1" 62 | 63 | //Android Room 64 | implementation "androidx.room:room-runtime:2.2.5" 65 | implementation "androidx.room:room-ktx:2.2.5" 66 | kapt "androidx.room:room-compiler:2.2.5" 67 | 68 | //Android Navigation Architecture 69 | implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha06" 70 | implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha06" 71 | 72 | implementation 'com.xwray:groupie:2.3.0' 73 | implementation 'com.xwray:groupie-kotlin-android-extensions:2.3.0' 74 | implementation 'com.xwray:groupie-databinding:2.3.0' 75 | 76 | implementation "androidx.preference:preference-ktx:1.1.1" 77 | } 78 | -------------------------------------------------------------------------------- /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 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/net/simplifiedcoding/mvvmsampleapp/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp 2 | 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.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.getTargetContext() 22 | assertEquals("net.simplifiedcoding.mvvmsampleapp", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/MVVMApplication.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp 2 | 3 | import android.app.Application 4 | import net.simplifiedcoding.mvvmsampleapp.data.db.AppDatabase 5 | import net.simplifiedcoding.mvvmsampleapp.data.network.MyApi 6 | import net.simplifiedcoding.mvvmsampleapp.data.network.NetworkConnectionInterceptor 7 | import net.simplifiedcoding.mvvmsampleapp.data.preferences.PreferenceProvider 8 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.QuotesRepository 9 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.UserRepository 10 | import net.simplifiedcoding.mvvmsampleapp.ui.auth.AuthViewModelFactory 11 | import net.simplifiedcoding.mvvmsampleapp.ui.home.profile.ProfileViewModelFactory 12 | import net.simplifiedcoding.mvvmsampleapp.ui.home.quotes.QuotesViewModelFactory 13 | import org.kodein.di.Kodein 14 | import org.kodein.di.KodeinAware 15 | import org.kodein.di.android.x.androidXModule 16 | import org.kodein.di.generic.bind 17 | import org.kodein.di.generic.instance 18 | import org.kodein.di.generic.provider 19 | import org.kodein.di.generic.singleton 20 | 21 | class MVVMApplication : Application(), KodeinAware { 22 | 23 | override val kodein = Kodein.lazy { 24 | import(androidXModule(this@MVVMApplication)) 25 | 26 | bind() from singleton { NetworkConnectionInterceptor(instance()) } 27 | bind() from singleton { MyApi(instance()) } 28 | bind() from singleton { AppDatabase(instance()) } 29 | bind() from singleton { PreferenceProvider(instance()) } 30 | bind() from singleton { UserRepository(instance(), instance()) } 31 | bind() from singleton { QuotesRepository(instance(), instance(), instance()) } 32 | bind() from provider { AuthViewModelFactory(instance()) } 33 | bind() from provider { ProfileViewModelFactory(instance()) } 34 | bind() from provider{ QuotesViewModelFactory(instance())} 35 | 36 | 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/db/AppDatabase.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.db 2 | 3 | import android.content.Context 4 | import androidx.room.Database 5 | import androidx.room.Room 6 | import androidx.room.RoomDatabase 7 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.Quote 8 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 9 | 10 | @Database( 11 | entities = [User::class, Quote::class], 12 | version = 1 13 | ) 14 | abstract class AppDatabase : RoomDatabase() { 15 | 16 | abstract fun getUserDao(): UserDao 17 | abstract fun getQuoteDao(): QuoteDao 18 | 19 | companion object { 20 | 21 | @Volatile 22 | private var instance: AppDatabase? = null 23 | private val LOCK = Any() 24 | 25 | operator fun invoke(context: Context) = instance ?: synchronized(LOCK) { 26 | instance ?: buildDatabase(context).also { 27 | instance = it 28 | } 29 | } 30 | 31 | private fun buildDatabase(context: Context) = 32 | Room.databaseBuilder( 33 | context.applicationContext, 34 | AppDatabase::class.java, 35 | "MyDatabase.db" 36 | ).build() 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/db/QuoteDao.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.db 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.room.Dao 5 | import androidx.room.Insert 6 | import androidx.room.OnConflictStrategy 7 | import androidx.room.Query 8 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.Quote 9 | 10 | @Dao 11 | interface QuoteDao { 12 | 13 | @Insert(onConflict = OnConflictStrategy.REPLACE) 14 | fun saveAllQuotes(quotes : List) 15 | 16 | @Query("SELECT * FROM Quote") 17 | fun getQuotes() : LiveData> 18 | 19 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/db/UserDao.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.db 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.room.Dao 5 | import androidx.room.Insert 6 | import androidx.room.OnConflictStrategy 7 | import androidx.room.Query 8 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.CURRENT_USER_ID 9 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 10 | 11 | @Dao 12 | interface UserDao{ 13 | 14 | @Insert(onConflict = OnConflictStrategy.REPLACE) 15 | suspend fun upsert(user: User) : Long 16 | 17 | @Query("SELECT * FROM user WHERE uid = $CURRENT_USER_ID") 18 | fun getuser() : LiveData 19 | 20 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/db/entities/Quote.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.db.entities 2 | 3 | import androidx.room.Entity 4 | import androidx.room.PrimaryKey 5 | 6 | @Entity 7 | data class Quote( 8 | @PrimaryKey(autoGenerate = false) 9 | val id: Int, 10 | val quote: String, 11 | val author: String, 12 | val thumbnail: String, 13 | val created_at: String?, 14 | val updated_at: String? 15 | ) -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/db/entities/User.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.db.entities 2 | 3 | import androidx.room.Entity 4 | import androidx.room.PrimaryKey 5 | 6 | const val CURRENT_USER_ID = 0 7 | 8 | @Entity 9 | data class User( 10 | var id: Int? = null, 11 | var name: String? = null, 12 | var email: String? = null, 13 | var password: String? = null, 14 | var email_verified_at: String? = null, 15 | var created_at: String? = null, 16 | var updated_at: String? = null 17 | ){ 18 | @PrimaryKey(autoGenerate = false) 19 | var uid: Int = CURRENT_USER_ID 20 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/network/MyApi.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.network 2 | 3 | import net.simplifiedcoding.mvvmsampleapp.data.network.responses.AuthResponse 4 | import net.simplifiedcoding.mvvmsampleapp.data.network.responses.QuotesResponse 5 | import okhttp3.OkHttpClient 6 | import okhttp3.ResponseBody 7 | import retrofit2.Call 8 | import retrofit2.Response 9 | import retrofit2.Retrofit 10 | import retrofit2.converter.gson.GsonConverterFactory 11 | import retrofit2.http.Field 12 | import retrofit2.http.FormUrlEncoded 13 | import retrofit2.http.GET 14 | import retrofit2.http.POST 15 | 16 | interface MyApi { 17 | 18 | @FormUrlEncoded 19 | @POST("login") 20 | suspend fun userLogin( 21 | @Field("email") email: String, 22 | @Field("password") password: String 23 | ) : Response 24 | 25 | @FormUrlEncoded 26 | @POST("signup") 27 | suspend fun userSignup( 28 | @Field("name") name: String, 29 | @Field("email") email: String, 30 | @Field("password") password: String 31 | ) : Response 32 | 33 | @GET("quotes") 34 | suspend fun getQuotes() : Response 35 | 36 | companion object{ 37 | operator fun invoke( 38 | networkConnectionInterceptor: NetworkConnectionInterceptor 39 | ) : MyApi{ 40 | 41 | val okkHttpclient = OkHttpClient.Builder() 42 | .addInterceptor(networkConnectionInterceptor) 43 | .build() 44 | 45 | return Retrofit.Builder() 46 | .client(okkHttpclient) 47 | .baseUrl("https://api.simplifiedcoding.in/course-apis/mvvm/") 48 | .addConverterFactory(GsonConverterFactory.create()) 49 | .build() 50 | .create(MyApi::class.java) 51 | } 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/network/NetworkConnectionInterceptor.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.network 2 | 3 | import android.content.Context 4 | import android.net.ConnectivityManager 5 | import android.net.NetworkCapabilities 6 | import net.simplifiedcoding.mvvmsampleapp.util.NoInternetException 7 | import okhttp3.Interceptor 8 | import okhttp3.Response 9 | 10 | class NetworkConnectionInterceptor( 11 | context: Context 12 | ) : Interceptor { 13 | 14 | private val applicationContext = context.applicationContext 15 | 16 | override fun intercept(chain: Interceptor.Chain): Response { 17 | if (!isInternetAvailable()) 18 | throw NoInternetException("Make sure you have an active data connection") 19 | return chain.proceed(chain.request()) 20 | } 21 | 22 | private fun isInternetAvailable(): Boolean { 23 | var result = false 24 | val connectivityManager = 25 | applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? 26 | connectivityManager?.let { 27 | it.getNetworkCapabilities(connectivityManager.activeNetwork)?.apply { 28 | result = when { 29 | hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true 30 | hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true 31 | else -> false 32 | } 33 | } 34 | } 35 | return result 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/network/SafeApiRequest.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.network 2 | 3 | import net.simplifiedcoding.mvvmsampleapp.util.ApiException 4 | import org.json.JSONException 5 | import org.json.JSONObject 6 | import retrofit2.Response 7 | 8 | abstract class SafeApiRequest { 9 | 10 | suspend fun apiRequest(call: suspend () -> Response) : T{ 11 | val response = call.invoke() 12 | if(response.isSuccessful){ 13 | return response.body()!! 14 | }else{ 15 | val error = response.errorBody()?.string() 16 | val message = StringBuilder() 17 | error?.let{ 18 | try{ 19 | message.append(JSONObject(it).getString("message")) 20 | }catch(e: JSONException){ } 21 | message.append("\n") 22 | } 23 | message.append("Error Code: ${response.code()}") 24 | throw ApiException(message.toString()) 25 | } 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/network/responses/AuthResponse.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.network.responses 2 | 3 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 4 | 5 | data class AuthResponse( 6 | val isSuccessful : Boolean?, 7 | val message: String?, 8 | val user: User? 9 | ) -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/network/responses/QuotesResponse.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.network.responses 2 | 3 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.Quote 4 | 5 | data class QuotesResponse ( 6 | val isSuccessful: Boolean, 7 | val quotes: List 8 | ) -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/preferences/PreferenceProvider.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.preferences 2 | 3 | import android.content.Context 4 | import android.content.SharedPreferences 5 | import androidx.preference.PreferenceManager 6 | 7 | private const val KEY_SAVED_AT = "key_saved_at" 8 | 9 | class PreferenceProvider( 10 | context: Context 11 | ) { 12 | 13 | private val appContext = context.applicationContext 14 | 15 | private val preference: SharedPreferences 16 | get() = PreferenceManager.getDefaultSharedPreferences(appContext) 17 | 18 | 19 | fun savelastSavedAt(savedAt: String) { 20 | preference.edit().putString( 21 | KEY_SAVED_AT, 22 | savedAt 23 | ).apply() 24 | } 25 | 26 | fun getLastSavedAt(): String? { 27 | return preference.getString(KEY_SAVED_AT, null) 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/repositories/QuotesRepository.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.repositories 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MutableLiveData 5 | import kotlinx.coroutines.Dispatchers 6 | import kotlinx.coroutines.withContext 7 | import net.simplifiedcoding.mvvmsampleapp.data.db.AppDatabase 8 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.Quote 9 | import net.simplifiedcoding.mvvmsampleapp.data.network.MyApi 10 | import net.simplifiedcoding.mvvmsampleapp.data.network.SafeApiRequest 11 | import net.simplifiedcoding.mvvmsampleapp.data.preferences.PreferenceProvider 12 | import net.simplifiedcoding.mvvmsampleapp.util.Coroutines 13 | import java.lang.Exception 14 | import java.time.LocalDateTime 15 | import java.time.temporal.ChronoUnit 16 | 17 | private val MINIMUM_INTERVAL = 6 18 | 19 | class QuotesRepository( 20 | private val api: MyApi, 21 | private val db: AppDatabase, 22 | private val prefs: PreferenceProvider 23 | ) : SafeApiRequest() { 24 | 25 | private val quotes = MutableLiveData>() 26 | 27 | init { 28 | quotes.observeForever { 29 | saveQuotes(it) 30 | } 31 | } 32 | 33 | suspend fun getQuotes(): LiveData> { 34 | return withContext(Dispatchers.IO) { 35 | fetchQuotes() 36 | db.getQuoteDao().getQuotes() 37 | } 38 | } 39 | 40 | private suspend fun fetchQuotes() { 41 | val lastSavedAt = prefs.getLastSavedAt() 42 | 43 | if (lastSavedAt == null || isFetchNeeded(LocalDateTime.parse(lastSavedAt))) { 44 | try { 45 | val response = apiRequest { api.getQuotes() } 46 | quotes.postValue(response.quotes) 47 | } catch (e: Exception) { 48 | e.printStackTrace() 49 | } 50 | } 51 | } 52 | 53 | private fun isFetchNeeded(savedAt: LocalDateTime): Boolean { 54 | return ChronoUnit.HOURS.between(savedAt, LocalDateTime.now()) > MINIMUM_INTERVAL 55 | } 56 | 57 | 58 | private fun saveQuotes(quotes: List) { 59 | Coroutines.io { 60 | prefs.savelastSavedAt(LocalDateTime.now().toString()) 61 | db.getQuoteDao().saveAllQuotes(quotes) 62 | } 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/data/repositories/UserRepository.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.data.repositories 2 | 3 | import net.simplifiedcoding.mvvmsampleapp.data.db.AppDatabase 4 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 5 | import net.simplifiedcoding.mvvmsampleapp.data.network.MyApi 6 | import net.simplifiedcoding.mvvmsampleapp.data.network.SafeApiRequest 7 | import net.simplifiedcoding.mvvmsampleapp.data.network.responses.AuthResponse 8 | import retrofit2.Response 9 | 10 | class UserRepository( 11 | private val api: MyApi, 12 | private val db: AppDatabase 13 | ) : SafeApiRequest() { 14 | 15 | suspend fun userLogin(email: String, password: String): AuthResponse { 16 | return apiRequest { api.userLogin(email, password) } 17 | } 18 | 19 | suspend fun userSignup( 20 | name: String, 21 | email: String, 22 | password: String 23 | ) : AuthResponse { 24 | return apiRequest{ api.userSignup(name, email, password)} 25 | } 26 | 27 | suspend fun saveUser(user: User) = db.getUserDao().upsert(user) 28 | 29 | fun getUser() = db.getUserDao().getuser() 30 | 31 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/auth/AuthViewModel.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.auth 2 | 3 | import androidx.lifecycle.ViewModel 4 | import kotlinx.coroutines.Dispatchers 5 | import kotlinx.coroutines.withContext 6 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 7 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.UserRepository 8 | 9 | 10 | class AuthViewModel( 11 | private val repository: UserRepository 12 | ) : ViewModel() { 13 | 14 | fun getLoggedInUser() = repository.getUser() 15 | 16 | suspend fun userLogin( 17 | email: String, 18 | password: String 19 | ) = withContext(Dispatchers.IO) { repository.userLogin(email, password) } 20 | 21 | suspend fun userSignup( 22 | name: String, 23 | email: String, 24 | password: String 25 | ) = withContext(Dispatchers.IO) { repository.userSignup(name, email, password) } 26 | 27 | suspend fun saveLoggedInUser(user: User) = repository.saveUser(user) 28 | 29 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/auth/AuthViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.auth 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.ViewModelProvider 5 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.UserRepository 6 | 7 | @Suppress("UNCHECKED_CAST") 8 | class AuthViewModelFactory( 9 | private val repository: UserRepository 10 | ) : ViewModelProvider.NewInstanceFactory() { 11 | 12 | override fun create(modelClass: Class): T { 13 | return AuthViewModel(repository) as T 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/auth/LoginActivity.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.auth 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import androidx.appcompat.app.AppCompatActivity 6 | import androidx.databinding.DataBindingUtil 7 | import androidx.lifecycle.* 8 | import kotlinx.android.synthetic.main.activity_login.* 9 | import kotlinx.coroutines.launch 10 | import net.simplifiedcoding.mvvmsampleapp.R 11 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 12 | import net.simplifiedcoding.mvvmsampleapp.databinding.ActivityLoginBinding 13 | import net.simplifiedcoding.mvvmsampleapp.ui.home.HomeActivity 14 | import net.simplifiedcoding.mvvmsampleapp.util.* 15 | import org.kodein.di.KodeinAware 16 | import org.kodein.di.android.kodein 17 | import org.kodein.di.generic.instance 18 | 19 | 20 | class LoginActivity : AppCompatActivity(), KodeinAware { 21 | 22 | override val kodein by kodein() 23 | private val factory: AuthViewModelFactory by instance() 24 | 25 | private lateinit var binding: ActivityLoginBinding 26 | private lateinit var viewModel: AuthViewModel 27 | 28 | override fun onCreate(savedInstanceState: Bundle?) { 29 | super.onCreate(savedInstanceState) 30 | 31 | binding = DataBindingUtil.setContentView(this, R.layout.activity_login) 32 | viewModel = ViewModelProvider(this, factory).get(AuthViewModel::class.java) 33 | 34 | 35 | viewModel.getLoggedInUser().observe(this, Observer { user -> 36 | if (user != null) { 37 | Intent(this, HomeActivity::class.java).also { 38 | it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK 39 | startActivity(it) 40 | } 41 | } 42 | }) 43 | 44 | binding.buttonSignIn.setOnClickListener { 45 | loginUser() 46 | } 47 | 48 | binding.textViewSignUp.setOnClickListener { 49 | startActivity(Intent(this, SignupActivity::class.java)) 50 | } 51 | } 52 | 53 | private fun loginUser() { 54 | val email = binding.editTextEmail.text.toString().trim() 55 | val password = binding.editTextPassword.text.toString().trim() 56 | 57 | lifecycleScope.launch { 58 | try { 59 | val authResponse = viewModel.userLogin(email, password) 60 | if (authResponse.user != null) { 61 | viewModel.saveLoggedInUser(authResponse.user) 62 | } else { 63 | binding.rootLayout.snackbar(authResponse.message!!) 64 | } 65 | } catch (e: ApiException) { 66 | e.printStackTrace() 67 | } catch (e: NoInternetException) { 68 | e.printStackTrace() 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/auth/SignupActivity.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.auth 2 | 3 | import android.content.Intent 4 | import androidx.appcompat.app.AppCompatActivity 5 | import android.os.Bundle 6 | import androidx.databinding.DataBindingUtil 7 | import androidx.lifecycle.Observer 8 | import androidx.lifecycle.ViewModelProvider 9 | import androidx.lifecycle.ViewModelProviders 10 | import androidx.lifecycle.lifecycleScope 11 | import kotlinx.android.synthetic.main.activity_login.* 12 | import kotlinx.coroutines.launch 13 | import net.simplifiedcoding.mvvmsampleapp.R 14 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.User 15 | import net.simplifiedcoding.mvvmsampleapp.databinding.ActivitySignupBinding 16 | import net.simplifiedcoding.mvvmsampleapp.ui.home.HomeActivity 17 | import net.simplifiedcoding.mvvmsampleapp.util.* 18 | import org.kodein.di.KodeinAware 19 | import org.kodein.di.android.kodein 20 | import org.kodein.di.generic.instance 21 | 22 | class SignupActivity : AppCompatActivity(), KodeinAware { 23 | 24 | override val kodein by kodein() 25 | private val factory: AuthViewModelFactory by instance() 26 | 27 | private lateinit var binding: ActivitySignupBinding 28 | private lateinit var viewModel: AuthViewModel 29 | 30 | override fun onCreate(savedInstanceState: Bundle?) { 31 | super.onCreate(savedInstanceState) 32 | 33 | binding = DataBindingUtil.setContentView(this, R.layout.activity_signup) 34 | viewModel = ViewModelProvider(this, factory).get(AuthViewModel::class.java) 35 | 36 | viewModel.getLoggedInUser().observe(this, Observer { user -> 37 | if (user != null) { 38 | Intent(this, HomeActivity::class.java).also { 39 | it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK 40 | startActivity(it) 41 | } 42 | } 43 | }) 44 | 45 | binding.buttonSignUp.setOnClickListener { 46 | userSignup() 47 | } 48 | } 49 | 50 | private fun userSignup() { 51 | val name = binding.editTextName.text.toString().trim() 52 | val email = binding.editTextEmail.text.toString().trim() 53 | val password = binding.editTextPassword.text.toString().trim() 54 | val password1 = binding.editTextPassword.text.toString().trim() 55 | 56 | //@todo add input validations 57 | 58 | lifecycleScope.launch { 59 | try { 60 | val authResponse = viewModel.userSignup(name, email, password) 61 | if (authResponse.user != null) { 62 | viewModel.saveLoggedInUser(authResponse.user) 63 | } else { 64 | binding.root.snackbar(authResponse.message!!) 65 | } 66 | } catch (e: ApiException) { 67 | e.printStackTrace() 68 | } catch (e: NoInternetException) { 69 | e.printStackTrace() 70 | } 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/HomeActivity.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.navigation.Navigation 6 | import androidx.navigation.ui.NavigationUI 7 | import kotlinx.android.synthetic.main.activity_home.* 8 | import net.simplifiedcoding.mvvmsampleapp.R 9 | 10 | class HomeActivity : AppCompatActivity() { 11 | 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | setContentView(R.layout.activity_home) 15 | 16 | setSupportActionBar(toolbar) 17 | 18 | val navController = Navigation.findNavController(this, R.id.fragment) 19 | NavigationUI.setupWithNavController(nav_view, navController) 20 | NavigationUI.setupActionBarWithNavController(this,navController, drawer_layout) 21 | } 22 | 23 | override fun onSupportNavigateUp(): Boolean { 24 | return NavigationUI.navigateUp( 25 | Navigation.findNavController(this, R.id.fragment), 26 | drawer_layout 27 | ) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/profile/ProfileFragment.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.profile 2 | 3 | import androidx.lifecycle.ViewModelProviders 4 | import android.os.Bundle 5 | import androidx.fragment.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import androidx.databinding.DataBindingUtil 10 | 11 | import net.simplifiedcoding.mvvmsampleapp.R 12 | import net.simplifiedcoding.mvvmsampleapp.databinding.ProfileFragmentBinding 13 | import org.kodein.di.android.x.kodein 14 | import org.kodein.di.KodeinAware 15 | import org.kodein.di.generic.instance 16 | 17 | class ProfileFragment : Fragment(), KodeinAware { 18 | 19 | override val kodein by kodein() 20 | 21 | private lateinit var viewModel: ProfileViewModel 22 | private val factory: ProfileViewModelFactory by instance() 23 | 24 | override fun onCreateView( 25 | inflater: LayoutInflater, container: ViewGroup?, 26 | savedInstanceState: Bundle? 27 | ): View? { 28 | val binding: ProfileFragmentBinding = 29 | DataBindingUtil.inflate(inflater, R.layout.profile_fragment, container, false) 30 | viewModel = ViewModelProviders.of(this, factory).get(ProfileViewModel::class.java) 31 | binding.viewmodel = viewModel 32 | binding.lifecycleOwner = this 33 | return binding.root 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/profile/ProfileViewModel.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.profile 2 | 3 | import androidx.lifecycle.ViewModel; 4 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.UserRepository 5 | 6 | class ProfileViewModel( 7 | repository: UserRepository 8 | ) : ViewModel() { 9 | 10 | val user = repository.getUser() 11 | 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/profile/ProfileViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.profile 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.ViewModelProvider 5 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.UserRepository 6 | 7 | @Suppress("UNCHECKED_CAST") 8 | class ProfileViewModelFactory( 9 | private val repository: UserRepository 10 | ) : ViewModelProvider.NewInstanceFactory() { 11 | 12 | override fun create(modelClass: Class): T { 13 | return ProfileViewModel(repository) as T 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/quotes/QuoteItem.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.quotes 2 | 3 | import com.xwray.groupie.databinding.BindableItem 4 | import net.simplifiedcoding.mvvmsampleapp.R 5 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.Quote 6 | import net.simplifiedcoding.mvvmsampleapp.databinding.ItemQuoteBinding 7 | 8 | class QuoteItem( 9 | private val quote: Quote 10 | ) : BindableItem(){ 11 | 12 | override fun getLayout() = R.layout.item_quote 13 | 14 | override fun bind(viewBinding: ItemQuoteBinding, position: Int) { 15 | viewBinding.setQuote(quote) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/quotes/QuotesFragment.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.quotes 2 | 3 | import androidx.lifecycle.ViewModelProviders 4 | import android.os.Bundle 5 | import androidx.fragment.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import androidx.lifecycle.Observer 10 | import androidx.recyclerview.widget.LinearLayoutManager 11 | import com.xwray.groupie.GroupAdapter 12 | import com.xwray.groupie.ViewHolder 13 | import kotlinx.android.synthetic.main.quotes_fragment.* 14 | 15 | import net.simplifiedcoding.mvvmsampleapp.R 16 | import net.simplifiedcoding.mvvmsampleapp.data.db.entities.Quote 17 | import net.simplifiedcoding.mvvmsampleapp.util.Coroutines 18 | import net.simplifiedcoding.mvvmsampleapp.util.hide 19 | import net.simplifiedcoding.mvvmsampleapp.util.show 20 | import net.simplifiedcoding.mvvmsampleapp.util.toast 21 | import org.kodein.di.KodeinAware 22 | import org.kodein.di.android.x.kodein 23 | import org.kodein.di.generic.instance 24 | 25 | class QuotesFragment : Fragment(), KodeinAware { 26 | 27 | override val kodein by kodein() 28 | 29 | private val factory: QuotesViewModelFactory by instance() 30 | 31 | private lateinit var viewModel: QuotesViewModel 32 | 33 | override fun onCreateView( 34 | inflater: LayoutInflater, container: ViewGroup?, 35 | savedInstanceState: Bundle? 36 | ): View? { 37 | return inflater.inflate(R.layout.quotes_fragment, container, false) 38 | } 39 | 40 | override fun onActivityCreated(savedInstanceState: Bundle?) { 41 | super.onActivityCreated(savedInstanceState) 42 | viewModel = ViewModelProviders.of(this, factory).get(QuotesViewModel::class.java) 43 | bindUI() 44 | } 45 | 46 | 47 | private fun bindUI() = Coroutines.main { 48 | progress_bar.show() 49 | viewModel.quotes.await().observe(this, Observer { 50 | progress_bar.hide() 51 | initRecyclerView(it.toQuoteItem()) 52 | }) 53 | } 54 | 55 | private fun initRecyclerView(quoteItem: List) { 56 | 57 | val mAdapter = GroupAdapter().apply { 58 | addAll(quoteItem) 59 | } 60 | 61 | recyclerview.apply { 62 | layoutManager = LinearLayoutManager(context) 63 | setHasFixedSize(true) 64 | adapter = mAdapter 65 | } 66 | 67 | } 68 | 69 | 70 | private fun List.toQuoteItem() : List{ 71 | return this.map { 72 | QuoteItem(it) 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/quotes/QuotesViewModel.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.quotes 2 | 3 | import androidx.lifecycle.ViewModel; 4 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.QuotesRepository 5 | import net.simplifiedcoding.mvvmsampleapp.util.lazyDeferred 6 | 7 | class QuotesViewModel( 8 | repository: QuotesRepository 9 | ) : ViewModel() { 10 | 11 | val quotes by lazyDeferred { 12 | repository.getQuotes() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/ui/home/quotes/QuotesViewModelFactory.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.ui.home.quotes 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.ViewModelProvider 5 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.QuotesRepository 6 | import net.simplifiedcoding.mvvmsampleapp.data.repositories.UserRepository 7 | 8 | @Suppress("UNCHECKED_CAST") 9 | class QuotesViewModelFactory( 10 | private val repository: QuotesRepository 11 | ) : ViewModelProvider.NewInstanceFactory() { 12 | 13 | override fun create(modelClass: Class): T { 14 | return QuotesViewModel(repository) as T 15 | } 16 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/util/Coroutines.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.util 2 | 3 | import kotlinx.coroutines.CoroutineScope 4 | import kotlinx.coroutines.Dispatchers 5 | import kotlinx.coroutines.launch 6 | 7 | object Coroutines { 8 | 9 | fun main(work: suspend (() -> Unit)) = 10 | CoroutineScope(Dispatchers.Main).launch { 11 | work() 12 | } 13 | 14 | fun io(work: suspend (() -> Unit)) = 15 | CoroutineScope(Dispatchers.IO).launch { 16 | work() 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/util/Delegates.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.util 2 | 3 | import kotlinx.coroutines.* 4 | 5 | fun lazyDeferred(block: suspend CoroutineScope.() -> T): Lazy>{ 6 | return lazy { 7 | GlobalScope.async(start = CoroutineStart.LAZY) { 8 | block.invoke(this) 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/util/Exceptions.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.util 2 | 3 | import java.io.IOException 4 | 5 | class ApiException(message: String) : IOException(message) 6 | class NoInternetException(message: String) : IOException(message) -------------------------------------------------------------------------------- /app/src/main/java/net/simplifiedcoding/mvvmsampleapp/util/ViewUtils.kt: -------------------------------------------------------------------------------- 1 | package net.simplifiedcoding.mvvmsampleapp.util 2 | 3 | import android.content.Context 4 | import android.view.View 5 | import android.widget.ProgressBar 6 | import android.widget.Toast 7 | import com.google.android.material.snackbar.Snackbar 8 | 9 | 10 | fun Context.toast(message: String){ 11 | Toast.makeText(this, message, Toast.LENGTH_LONG ).show() 12 | } 13 | 14 | fun ProgressBar.show(){ 15 | visibility = View.VISIBLE 16 | } 17 | 18 | fun ProgressBar.hide(){ 19 | visibility = View.GONE 20 | } 21 | 22 | fun View.snackbar(message: String){ 23 | Snackbar.make(this, message, Snackbar.LENGTH_LONG).also { snackbar -> 24 | snackbar.setAction("Ok") { 25 | snackbar.dismiss() 26 | } 27 | }.show() 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/edit_text_round_gray_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_app_logo.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_email.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_lock.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_name.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_home.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 21 | 22 | 29 | 30 | 31 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | 14 | 18 | 19 | 26 | 27 | 33 | 34 | 42 | 43 | 54 | 55 | 56 | 57 | 65 | 66 | 77 | 78 | 89 | 90 | 98 | 99 |