Skip to content

Commit c86112b

Browse files
committed
add BaseFragmentSharedViewModel
1 parent 9d1ba42 commit c86112b

8 files changed

Lines changed: 90 additions & 163 deletions

File tree

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 116 deletions
This file was deleted.

.idea/gradle.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

.idea/runConfigurations.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
</h1>
44

55
<p align="center">
6-
<img src="https://img.shields.io/badge/Platform-Android%206.0-36da7e?logo=android" alt="platform" />
7-
<img src="https://img.shields.io/badge/Kotlin-1.3.61-orange?logo=kotlin" alt="language" />
8-
<a href="https://github.com/htdangkhoa/android-clean-architecture/releases">
9-
<img src="https://img.shields.io/github/v/release/htdangkhoa/android-clean-architecture" alt="version" />
6+
<a href="https://img.shields.io/badge/Platform-Android%206.0-36da7e?logo=android">
7+
<img src="https://img.shields.io/badge/Platform-Android%206.0-36da7e?logo=android" alt="platform" />
8+
</a>
9+
<a href="https://img.shields.io/badge/Kotlin-1.3.61-orange?logo=kotlin">
10+
<img src="https://img.shields.io/badge/Kotlin-1.3.61-orange?logo=kotlin" alt="language" />
1011
</a>
1112
</p>
1213

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.github.htdangkhoa.cleanarchitecture.base
2+
3+
import android.app.Activity
4+
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import androidx.annotation.CallSuper
9+
import androidx.annotation.LayoutRes
10+
import androidx.fragment.app.Fragment
11+
import androidx.lifecycle.ViewModel
12+
import com.afollestad.materialdialogs.MaterialDialog
13+
import com.github.htdangkhoa.cleanarchitecture.data.model.AuthModel
14+
import com.github.htdangkhoa.cleanarchitecture.data.model.ResponseExceptionModel
15+
import com.github.htdangkhoa.cleanarchitecture.ui.login.LoginActivity
16+
import com.pawegio.kandroid.startActivity
17+
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
18+
import retrofit2.HttpException
19+
import kotlin.reflect.KClass
20+
21+
abstract class BaseFragmentSharedViewModel<VM : ViewModel, A : BaseActivity<VM>>(
22+
clazz: KClass<VM>
23+
): Fragment() {
24+
@get:LayoutRes
25+
abstract val layoutResID: Int
26+
27+
protected val viewModel: VM by sharedViewModel(clazz)
28+
29+
override fun onCreateView(
30+
inflater: LayoutInflater,
31+
container: ViewGroup?,
32+
savedInstanceState: Bundle?
33+
): View? {
34+
return inflater.inflate(layoutResID, container, false)
35+
}
36+
37+
@CallSuper
38+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
39+
render(view, savedInstanceState)
40+
}
41+
42+
protected open fun render(view: View, savedInstanceState: Bundle?) = Unit
43+
44+
protected fun handleError(throwable: Throwable? = null, block: ((Throwable?) -> Unit)? = null) {
45+
return block?.invoke(throwable) ?: handleHttpError(throwable)
46+
}
47+
48+
protected fun handleHttpError(throwable: Throwable?) {
49+
when (throwable) {
50+
is HttpException -> {
51+
logout(throwable.code())
52+
}
53+
is ResponseExceptionModel -> {
54+
throwable.responseModel?.code?.let { logout(it) }
55+
}
56+
}
57+
}
58+
59+
protected fun logout(code: Int) {
60+
if (code == 401) {
61+
AuthModel.clear()
62+
63+
context?.let {
64+
if (it is Activity && it::class.simpleName != LoginActivity::class.simpleName) {
65+
it.startActivity<LoginActivity>()
66+
67+
it.finishAfterTransition()
68+
}
69+
}
70+
}
71+
}
72+
73+
protected fun showDialog(title: String? = "Info", message: String? = null): MaterialDialog {
74+
return MaterialDialog(context!!).show {
75+
title(text = title)
76+
77+
message(text = message)
78+
79+
positiveButton(text = "OK")
80+
}
81+
}
82+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ buildscript {
4343

4444
}
4545
dependencies {
46-
classpath "com.android.tools.build:gradle:3.5.3"
46+
classpath 'com.android.tools.build:gradle:3.6.3'
4747
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
4848
// NOTE: Do not place your application dependencies here; they belong
4949
// in the individual module build.gradle files
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Jan 24 00:14:38 ICT 2020
1+
#Thu May 21 17:13:07 ICT 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

0 commit comments

Comments
 (0)