Skip to content

Commit 85d59d5

Browse files
committedApr 19, 2024
added util module with two base classes for fragments and view models
1 parent 7064fec commit 85d59d5

File tree

11 files changed

+186
-0
lines changed

11 files changed

+186
-0
lines changed
 

‎app/build.gradle.kts

+9
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,14 @@ dependencies {
4646
androidTestImplementation(libs.androidx.junit)
4747
androidTestImplementation(libs.androidx.espresso.core)
4848

49+
implementation(libs.nav.ui)
50+
implementation(libs.nav.fragment)
51+
4952
implementation(project(":uikit"))
53+
implementation(project(":util"))
54+
implementation(project(":search"))
55+
implementation(project(":profile"))
56+
implementation(project(":recommendations"))
57+
implementation(project(":subscription"))
58+
implementation(project(":hotels"))
5059
}

‎settings.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ dependencyResolutionManagement {
2222
rootProject.name = "Flight Finder"
2323
include(":app")
2424
include(":uikit")
25+
include(":search")
26+
include(":profile")
27+
include(":subscription")
28+
include(":hotels")
29+
include(":recommendations")
30+
include(":util")

‎util/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

‎util/build.gradle.kts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
plugins {
2+
alias(libs.plugins.androidLibrary)
3+
alias(libs.plugins.jetbrainsKotlinAndroid)
4+
}
5+
6+
android {
7+
namespace = "com.github.lzaytseva.util"
8+
compileSdk = 34
9+
10+
defaultConfig {
11+
minSdk = 24
12+
13+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
14+
consumerProguardFiles("consumer-rules.pro")
15+
}
16+
17+
buildTypes {
18+
release {
19+
isMinifyEnabled = false
20+
proguardFiles(
21+
getDefaultProguardFile("proguard-android-optimize.txt"),
22+
"proguard-rules.pro"
23+
)
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility = JavaVersion.VERSION_1_8
28+
targetCompatibility = JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = "1.8"
32+
}
33+
34+
buildFeatures {
35+
viewBinding = true
36+
}
37+
}
38+
39+
dependencies {
40+
41+
implementation(libs.androidx.core.ktx)
42+
implementation(libs.androidx.appcompat)
43+
implementation(libs.material)
44+
testImplementation(libs.junit)
45+
androidTestImplementation(libs.androidx.junit)
46+
androidTestImplementation(libs.androidx.espresso.core)
47+
}

‎util/consumer-rules.pro

Whitespace-only changes.

‎util/proguard-rules.pro

+21
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.lzaytseva.util
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.github.lzaytseva.util.test", appContext.packageName)
23+
}
24+
}

‎util/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest>
3+
4+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.github.lzaytseva.util
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.fragment.app.Fragment
6+
import androidx.viewbinding.ViewBinding
7+
8+
typealias Inflate<T> = (LayoutInflater, ViewGroup?, Boolean) -> T
9+
10+
/**
11+
* Базовый фрагмент, позволяет привести к единому виду все реализации фрагментов, работает в связке с BaseViewModel.
12+
*
13+
* При реализации потребуется реализовать абстрактные методы для инициализации вью: configureViews() и слушатели событий: subscribe()
14+
* и абстрактное свойство viewModel
15+
*/
16+
abstract class BaseFragment<VB: ViewBinding, VM: BaseViewModel> (
17+
private val inflate: Inflate<VB>
18+
): Fragment() {
19+
20+
private var _binding: VB? = null
21+
protected val binding: VB get() = _binding!!
22+
23+
abstract val viewModel: VM
24+
25+
override fun onCreateView(
26+
inflater: LayoutInflater,
27+
container: ViewGroup?,
28+
savedInstanceState: android.os.Bundle?
29+
): android.view.View? {
30+
_binding = inflate.invoke(inflater, container, false)
31+
return binding.root
32+
}
33+
34+
override fun onViewCreated(view: android.view.View, savedInstanceState: android.os.Bundle?) {
35+
super.onViewCreated(view, savedInstanceState)
36+
onConfigureViews()
37+
onSubscribe()
38+
}
39+
40+
abstract fun onConfigureViews()
41+
42+
abstract fun onSubscribe()
43+
44+
override fun onDestroyView() {
45+
super.onDestroyView()
46+
_binding = null
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.lzaytseva.util
2+
3+
import androidx.lifecycle.ViewModel
4+
5+
/**
6+
* Базовая вью модель, необходима для реализации базового фрагмента
7+
*
8+
*/
9+
abstract class BaseViewModel : ViewModel()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.lzaytseva.util
2+
3+
import org.junit.Test
4+
5+
import org.junit.Assert.*
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* See [testing documentation](http://d.android.com/tools/testing).
11+
*/
12+
class ExampleUnitTest {
13+
@Test
14+
fun addition_isCorrect() {
15+
assertEquals(4, 2 + 2)
16+
}
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.