Skip to content

Commit d7b122b

Browse files
Merge pull request #291 from algokelvin-373/production/v1.0.2 - Production/v1.0.2 to master version 1.0.2
Production/v1.0.2 to master
2 parents 1c39012 + 1d03487 commit d7b122b

File tree

555 files changed

+13524
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

555 files changed

+13524
-36
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
.idea
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild
9+
.cxx
10+
local.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
id 'kotlin-kapt'
5+
id 'kotlin-parcelize'
6+
}
7+
8+
android {
9+
compileSdkVersion 31
10+
buildToolsVersion "30.0.3"
11+
12+
defaultConfig {
13+
applicationId "com.algokelvin.app"
14+
minSdkVersion 21
15+
targetSdkVersion 31
16+
versionCode 1
17+
versionName "1.0"
18+
19+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
20+
}
21+
22+
buildTypes {
23+
release {
24+
minifyEnabled false
25+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26+
}
27+
}
28+
buildFeatures {
29+
viewBinding true
30+
}
31+
compileOptions {
32+
sourceCompatibility JavaVersion.VERSION_1_8
33+
targetCompatibility JavaVersion.VERSION_1_8
34+
}
35+
kotlinOptions {
36+
jvmTarget = '1.8'
37+
}
38+
}
39+
40+
dependencies {
41+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
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+
// Lifecycle
48+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"
49+
50+
// Room
51+
implementation "androidx.room:room-ktx:2.3.0"
52+
kapt "androidx.room:room-compiler:2.3.0"
53+
54+
// Coroutines
55+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
56+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.algokelvin.app">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.MVVM_CleanArch_App">
12+
<activity
13+
android:name=".ui.main.MainActivity"
14+
android:exported="true">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.algokelvin.app.base
2+
3+
import android.widget.Toast
4+
import androidx.appcompat.app.AppCompatActivity
5+
6+
open class BaseActivity: AppCompatActivity() {
7+
protected fun toast(message: String, isLong: Boolean = false) {
8+
Toast.makeText(this, message,
9+
if (isLong)
10+
Toast.LENGTH_LONG
11+
else
12+
Toast.LENGTH_SHORT
13+
).show()
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.algokelvin.app.base
2+
3+
import com.algokelvin.app.model.Resource
4+
import kotlinx.coroutines.CoroutineDispatcher
5+
import kotlinx.coroutines.flow.Flow
6+
import kotlinx.coroutines.flow.catch
7+
import kotlinx.coroutines.flow.flowOn
8+
9+
abstract class UseCase<P, R: Any>(private val dispatcher: CoroutineDispatcher) {
10+
11+
operator fun invoke(param: P): Flow<Resource<R>> {
12+
return execute(param).catch {
13+
emit(Resource.error(it.message ?: ""))
14+
}.flowOn(dispatcher)
15+
}
16+
17+
@Throws(RuntimeException::class)
18+
abstract fun execute(param: P): Flow<Resource<R>>
19+
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.algokelvin.app.data
2+
3+
import kotlinx.coroutines.flow.*
4+
5+
/*
6+
* This is the strategy util to make a data request
7+
* supported offline-mode using Flow.
8+
* */
9+
class NetworkBoundResource<T>(
10+
val query: () -> Flow<T>, // first, getting the data from local db
11+
val fetch: suspend () -> T, // then, fetch the data from network
12+
val saveFetchResult: (T) -> Unit, // after getting the data from network, save it into local db
13+
val onFetchFailed: (Throwable) -> Unit = {}, // handling fetch failed if any
14+
val isShouldFetch: (T) -> Boolean = { _ -> true } // is it need to fetch to network or not (default: true)
15+
) {
16+
17+
fun asFlow(): Flow<Resource<T>> = flow {
18+
val flow = query()
19+
.onStart { emit(Resource.loading(null)) }
20+
.flatMapConcat { data ->
21+
if (isShouldFetch(data)) {
22+
emit(Resource.loading(data))
23+
24+
try {
25+
saveFetchResult(fetch())
26+
query().map { Resource.success(it) }
27+
} catch (throwable: Throwable) {
28+
onFetchFailed(throwable)
29+
query().map {
30+
Resource.error(throwable.message?: "", null)
31+
}
32+
}
33+
} else {
34+
query().map { Resource.success(it) }
35+
}
36+
}
37+
38+
emitAll(flow)
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.algokelvin.app.data
2+
3+
data class Resource<out T> (val status: Status, val data: T?, val message: String?) {
4+
enum class Status {
5+
SUCCESS,
6+
ERROR,
7+
LOADING
8+
}
9+
10+
companion object {
11+
fun <T> success(data: T):Resource<T> {
12+
return Resource(Status.SUCCESS, data, null)
13+
}
14+
fun <T> error(message: String, data: T? = null):Resource<T> {
15+
return Resource(Status.ERROR, data, message)
16+
}
17+
fun <T> loading(data: T? = null):Resource<T> {
18+
return Resource(Status.LOADING, data, null)
19+
}
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.algokelvin.app.data.datasource.local
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import com.algokelvin.app.data.entity.Product
8+
import kotlinx.coroutines.flow.Flow
9+
10+
@Dao
11+
interface ProductDao {
12+
@Query("SELECT * FROM product")
13+
fun products(): Flow<List<Product>>
14+
15+
@JvmSuppressWildcards
16+
@Insert(onConflict = OnConflictStrategy.REPLACE)
17+
fun insertAll(products: List<Product>)
18+
19+
@Insert
20+
fun insert(product: Product)
21+
22+
@Query("SELECT * FROM product WHERE id=:id")
23+
fun productBy(id: Int): Flow<Product>
24+
}

0 commit comments

Comments
 (0)