kandi background
Explore Kits

architecture-samples | showcase different architectural tools and patterns | Architecture library

 by   android Kotlin Version: Current License: Apache-2.0

 by   android Kotlin Version: Current License: Apache-2.0

Download this library from

kandi X-RAY | architecture-samples Summary

architecture-samples is a Kotlin library typically used in Architecture applications. architecture-samples has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can download it from GitHub.
A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • architecture-samples has a medium active ecosystem.
  • It has 40272 star(s) with 11045 fork(s). There are 2513 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 130 open issues and 284 have been closed. On average issues are closed in 110 days. There are 65 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of architecture-samples is current.
architecture-samples Support
Best in #Architecture
Average in #Architecture
architecture-samples Support
Best in #Architecture
Average in #Architecture

quality kandi Quality

  • architecture-samples has 0 bugs and 0 code smells.
architecture-samples Quality
Best in #Architecture
Average in #Architecture
architecture-samples Quality
Best in #Architecture
Average in #Architecture

securitySecurity

  • architecture-samples has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • architecture-samples code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
architecture-samples Security
Best in #Architecture
Average in #Architecture
architecture-samples Security
Best in #Architecture
Average in #Architecture

license License

  • architecture-samples is licensed under the Apache-2.0 License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
architecture-samples License
Best in #Architecture
Average in #Architecture
architecture-samples License
Best in #Architecture
Average in #Architecture

buildReuse

  • architecture-samples releases are not available. You will need to build from source code and install.
  • Installation instructions are not available. Examples and code snippets are available.
  • It has 4754 lines of code, 349 functions and 92 files.
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
architecture-samples Reuse
Best in #Architecture
Average in #Architecture
architecture-samples Reuse
Best in #Architecture
Average in #Architecture
Top functions reviewed by kandi - BETA

Coming Soon for all Libraries!

Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.

architecture-samples Key Features

A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

Opening a sample in Android Studio

copy iconCopydownload iconDownload
git clone git@github.com:googlesamples/android-architecture.git

License

copy iconCopydownload iconDownload
Copyright 2019 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.

Why is there a object class defined within sealed class when I use Kotlin?

copy iconCopydownload iconDownload
data class Loading : Result<Nothing>() <-- this isn't valid for a data class
 data class Success<out T>(val data: T) : Result<T>() <-- (val data: T)
 data class Error(val exception: Exception) : Result<Nothing>() <-- (val exception: Exception)
-----------------------
data class Loading : Result<Nothing>() <-- this isn't valid for a data class
 data class Success<out T>(val data: T) : Result<T>() <-- (val data: T)
 data class Error(val exception: Exception) : Result<Nothing>() <-- (val exception: Exception)

Android Room Pre-populated Data not visible first time

copy iconCopydownload iconDownload
// annotations omitted
fun getAllUser(): Flow<List<User>>
// annotations omitted
suspend fun insertUser(users: List<User>)
-----------------------
// annotations omitted
fun getAllUser(): Flow<List<User>>
// annotations omitted
suspend fun insertUser(users: List<User>)

Hilt Fragment has to be attached to @AndroidEntryPoint Activity, while testing (it is attached to @AndEntPoint marked activity)

copy iconCopydownload iconDownload
    hilt {
        enableTransformForLocalTests = true
    }

Android MVVM with servicelocator

copy iconCopydownload iconDownload
class DataSource(privat val retrofitService: RetrofitService) {

    /**
     * Consider [Value] as a type placeholder for this example
     */
    fun suspend fun getValue(): Result<Value> = suspendCoroutine { continuation ->
      retrofitService.getValue().enqueue(object : Callback<Value> {
        override fun onFailure(call: Call<List<ResponseEntity>>,
                               throwable: Throwable) {
          continuation.resume(Result.Failure(throwable)))
        }

        override fun onResponse(call: Call<List<ResponseEntity>>,
                                response: Response<List<ResponseEntity>>) {
          continuation.resume(Result.Success(response.body()))
        }
      }
   }
}
sealed class Result<out T> {

    /**
     * Indicates a success state.
     */
    data class Success<T>(val data: T) : Result<T>()

    /**
     * Indicates an error state.
     */
    data class Failure(val throwable: Throwable): Result<Nothing>
}
class TestViewModel(private val dataSource: DataSource) : ViewModel() {
    private val _value = MutableLiveData<Value>>()
    val value: LiveData<Value> = _value

    private val _error = MutableLiveData<String>()
    val error: LiveData = _error

    init {
        getValue()
    }

    private fun getValue() {
      viewModelScope.launch {
        val result: Result<Value> = dataSource.getValue()
        
        // check wether the result is of type Success or Failure
        when(result) {
          is Result.Success -> _value.postValue(result.data)
          is Result.Failure -> _error.value = throwable.message
        }
      }
    }
}
-----------------------
class DataSource(privat val retrofitService: RetrofitService) {

    /**
     * Consider [Value] as a type placeholder for this example
     */
    fun suspend fun getValue(): Result<Value> = suspendCoroutine { continuation ->
      retrofitService.getValue().enqueue(object : Callback<Value> {
        override fun onFailure(call: Call<List<ResponseEntity>>,
                               throwable: Throwable) {
          continuation.resume(Result.Failure(throwable)))
        }

        override fun onResponse(call: Call<List<ResponseEntity>>,
                                response: Response<List<ResponseEntity>>) {
          continuation.resume(Result.Success(response.body()))
        }
      }
   }
}
sealed class Result<out T> {

    /**
     * Indicates a success state.
     */
    data class Success<T>(val data: T) : Result<T>()

    /**
     * Indicates an error state.
     */
    data class Failure(val throwable: Throwable): Result<Nothing>
}
class TestViewModel(private val dataSource: DataSource) : ViewModel() {
    private val _value = MutableLiveData<Value>>()
    val value: LiveData<Value> = _value

    private val _error = MutableLiveData<String>()
    val error: LiveData = _error

    init {
        getValue()
    }

    private fun getValue() {
      viewModelScope.launch {
        val result: Result<Value> = dataSource.getValue()
        
        // check wether the result is of type Success or Failure
        when(result) {
          is Result.Success -> _value.postValue(result.data)
          is Result.Failure -> _error.value = throwable.message
        }
      }
    }
}
-----------------------
class DataSource(privat val retrofitService: RetrofitService) {

    /**
     * Consider [Value] as a type placeholder for this example
     */
    fun suspend fun getValue(): Result<Value> = suspendCoroutine { continuation ->
      retrofitService.getValue().enqueue(object : Callback<Value> {
        override fun onFailure(call: Call<List<ResponseEntity>>,
                               throwable: Throwable) {
          continuation.resume(Result.Failure(throwable)))
        }

        override fun onResponse(call: Call<List<ResponseEntity>>,
                                response: Response<List<ResponseEntity>>) {
          continuation.resume(Result.Success(response.body()))
        }
      }
   }
}
sealed class Result<out T> {

    /**
     * Indicates a success state.
     */
    data class Success<T>(val data: T) : Result<T>()

    /**
     * Indicates an error state.
     */
    data class Failure(val throwable: Throwable): Result<Nothing>
}
class TestViewModel(private val dataSource: DataSource) : ViewModel() {
    private val _value = MutableLiveData<Value>>()
    val value: LiveData<Value> = _value

    private val _error = MutableLiveData<String>()
    val error: LiveData = _error

    init {
        getValue()
    }

    private fun getValue() {
      viewModelScope.launch {
        val result: Result<Value> = dataSource.getValue()
        
        // check wether the result is of type Success or Failure
        when(result) {
          is Result.Success -> _value.postValue(result.data)
          is Result.Failure -> _error.value = throwable.message
        }
      }
    }
}

Why does a normal function need to be wrapped with viewModelScope.launch?

copy iconCopydownload iconDownload
public fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    // the below line is doing the magic
    block: suspend CoroutineScope.() -> Unit
): Job {
    val newContext = newCoroutineContext(context)
    val coroutine = if (start.isLazy)
        LazyStandaloneCoroutine(newContext, block) else
        StandaloneCoroutine(newContext, active = true)
    coroutine.start(start, coroutine, block)
    return coroutine
}
-----------------------
viewModelScope.launch(Dispatchers.Default)

Android: Variable gets uninitialized in ViewModel after being initialized in the Fragment

copy iconCopydownload iconDownload
override onActivityCreated(@Nullable final Bundle savedInstanceState){
    searchMovieFragmentViewModel = ViewModelProvider(this).get(SearchMovieFragmentViewModel::class.java)
}

Can I replace _task.map with Transformations.map in Kotlin?

copy iconCopydownload iconDownload
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
inline fun <X, Y> LiveData<X>.map(crossinline transform: (X) -> Y): LiveData<Y> =
        Transformations.map(this) { transform(it) }
-----------------------
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
inline fun <X, Y> LiveData<X>.map(crossinline transform: (X) -> Y): LiveData<Y> =
        Transformations.map(this) { transform(it) }

Android kotlin type inheritance failed

copy iconCopydownload iconDownload
 override fun observeTasks(): LiveData<Result<List<TaskEntity>>> {
    return taskDao.observeTasks().map {
        Success(it)
    }
}
-----------------------
taskDao.observeTasks().map {
    Success(it) as Result<List<TaskEntity>>
}
fun <T> success(x: T): Result<T> = Success(x)

taskDao.observeTasks().map {
    success(it)
}
-----------------------
taskDao.observeTasks().map {
    Success(it) as Result<List<TaskEntity>>
}
fun <T> success(x: T): Result<T> = Success(x)

taskDao.observeTasks().map {
    success(it)
}

Why does the navigation file have two userMessage in Android Studio?

copy iconCopydownload iconDownload
    <argument
    android:name="userMessage"
    android:defaultValue="0" />

Why does the author add tasksRepository ?: again in the project?

copy iconCopydownload iconDownload
return tasksRepository ?: synchronized(this) { 
    tasksRepository ?: createTasksRepository(context) 
}

Community Discussions

Trending Discussions on architecture-samples
  • Why is there a object class defined within sealed class when I use Kotlin?
  • Android Room Pre-populated Data not visible first time
  • Why can't I write this particular assignment in Kotlin?
  • Hilt Fragment has to be attached to @AndroidEntryPoint Activity, while testing (it is attached to @AndEntPoint marked activity)
  • Android MVVM with servicelocator
  • Why does a normal function need to be wrapped with viewModelScope.launch?
  • Why does the author wrap tasksRepository.refreshTasks() with viewModelScope.launch?
  • Android: Variable gets uninitialized in ViewModel after being initialized in the Fragment
  • Can I replace _task.map with Transformations.map in Kotlin?
  • How to mock the view model with Hilt for unit testing fragments?
Trending Discussions on architecture-samples

QUESTION

Why is there a object class defined within sealed class when I use Kotlin?

Asked 2022-Mar-25 at 00:45

The Code A is from the official sample project.

Result<out R> is a sealed class, I'm very strange why Loading is defined as object class.

I think the Code B is more reasonable, is it right?

Code A

sealed class Result<out R> {
   data class Success<out T>(val data: T) : Result<T>()
   data class Error(val exception: Exception) : Result<Nothing>()
   object Loading : Result<Nothing>()

   override fun toString(): String {
      return when (this) {
         is Success<*> -> "Success[data=$data]"
         is Error -> "Error[exception=$exception]"
         Loading -> "Loading"
      }
   }
}

Code B

sealed class Result<out R> {
   data class Success<out T>(val data: T) : Result<T>()
   data class Error(val exception: Exception) : Result<Nothing>()
   data class Loading : Result<Nothing>()

   override fun toString(): String {
      return when (this) {
         is Success<*> -> "Success[data=$data]"
         is Error -> "Error[exception=$exception]"
         is Loading -> "Loading"
      }
   }
}

Added Content:

To Tenfour04 and a_local_nobody: Thanks!

The Android Studio can compile and run after I add is before Loading -> "Loading" in Code C.

1: What are differents between Code A and Code C?

2: And more, In my mind, all types whithin sealed class should be the same, maybe they are all data class, or they are all object. But Code A mix data class and object, and it's Ok, does it mean that I add even Interface whithin sealed class ?

Code C

sealed class Result<out R> {
   data class Success<out T>(val data: T) : Result<T>()
   data class Error(val exception: Exception) : Result<Nothing>()
   object Loading : Result<Nothing>()

   override fun toString(): String {
      return when (this) {
         is Success<*> -> "Success[data=$data]"
         is Error -> "Error[exception=$exception]"
         is Loading -> "Loading"     // I add "is"
      }
   }
}

ANSWER

Answered 2022-Mar-24 at 11:20

that won't work, because:

data class Loading : Result<Nothing>() <-- this isn't valid for a data class

Data classes must have at least one primary constructor parameter, presumably the author used an object there to avoid having to make use of a constructor value, compared to the others:

 data class Success<out T>(val data: T) : Result<T>() <-- (val data: T)
 data class Error(val exception: Exception) : Result<Nothing>() <-- (val exception: Exception)

which clearly require values

Source https://stackoverflow.com/questions/71601256

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install architecture-samples

You can download it from GitHub.

Support

For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

DOWNLOAD this Library from

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

Explore Related Topics

Share this Page

share link
Reuse Pre-built Kits with architecture-samples
Compare Architecture Libraries with Permissive License
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
over 430 million Knowledge Items
Find more libraries
Reuse Solution Kits and Libraries Curated by Popular Use Cases

Save this library and start creating your kit

  • © 2022 Open Weaver Inc.