• Breaking News

    Tuesday, January 21, 2020

    Android Dev - A couple of questions regarding Clean + MVVM (big project rewrite)

    Android Dev - A couple of questions regarding Clean + MVVM (big project rewrite)


    A couple of questions regarding Clean + MVVM (big project rewrite)

    Posted: 21 Jan 2020 12:56 AM PST

    I'm planning to rewrite my app into a new architecture to make it scalable and testable. Currently, the majority of the code is in views and different manager classes because it's a 5+ year-old project and it's simply becoming too much to handle. It works well and the code is manageable, but this is a project that should last another 5+ years, so I need to make some decisions about scalability.

    I've read as much info about Clean + MVVM as I possibly could and even though I still don't feel like I understand it completely, I gave it a try and wanted to start rewriting it. I ran into some issues almost immediately. Now I need to decide between blindly following Clean architecture, come up with my own architecture which works best for our project, or make some sacrifices which will go against the main Clean principles.

    Here are the main points I want to discuss:

    • Our API is made to support our mobile client only, meaning all of the models are almost perfectly ready to be served to the UI. At this point, it simply doesn't make sense to have mappers. We don't cache anything other than "User" object for obvious purposes. Everything else is live data straight from API. App doesn't work without internet connection and it never will. However I still need GSON because I want to rename some (although rare) parameters when mapping from API response. This means I would need to use GSON dependency in the Domain layer. Is this acceptable?
    • I am running into the same issue with LiveData. I googled this issue and I noticed I'm not the only one with that problem. I want to have Observable pattern for streaming Data right off the bat from the Domain level. People are recommending to use Rx observable for domain and data layer, and only convert it into LiveData on the Presentation layer. This just seems like overhead work to me. I don't see how I would "break" the architecture if I simply decided to add LiveData as a dependency on Domain layer.
    • As far as I understand, UseCases are written on Domain layer. After all, they simply use Repositories to get data, handle it somehow and then pass it on to Presenters. But Dagger (DI) is usually on Presentation layer. How do I inject UseCases with Repositories in this case? This is one thing I seemed to have missed when researching.
    • How "specific" should use cases be? Like I said, most of our API calls (>90%) already serve the data exactly as we need it to, so the vast majority of our UseCases would be "useless". Now I know this is a common question when working with UseCases for the first time, but the vast majority of our use cases really would be simply calling the repository method. I can only think of a couple of instances where a UseCase would take 2 or maybe 3 repositories, make 3 separate calls and then serve the data to UI. (for example, when displaying our user profile, we make one call to GET user profile and then another one to GET some additional user info - this "ShowUserProfileUseCase" would simply make these 2 calls consecutively and then serve all data at once). Having said this, is it acceptable to ditch UseCases in my case?
    • Last point I want to discuss is simply what's the best approach to start rewriting architecture on a big project? Like I said, this is a 5+ years old (huge) project. Current architecture is simple. Network calls are grouped in "client" classes which access a singleton Retrofit instance. "Clients" are initialized directly on Activities/Fragments and once the network calls are made, the data is displayed on UI. It works, there are no bugs or performance issues. Considering the "old school" architecture, the project works great. Even the code is more scalable than it sounds. But it isn't testable at all, which is the main reason I want to do a rewrite. But it's hard to even start a rewrite on a project this big.
    submitted by /u/MaHcIn
    [link] [comments]

    Super Simple Bouncy RecyclerView in Kotlin

    Posted: 21 Jan 2020 03:40 AM PST

    https://github.com/IainS1986/SimpleBouncyRecyclerView

    To be honest, I did this project mainly to learn around producing Android aar libraries and how to share them (via Bintray -> JCenter), but thought some folks might be interested in seeing what I did.

    I'm a professional Xamarin developer by day but shifting to native development on the side. I did this in Xamarin originally so ported it over as a test.

    Its called "Simple" really because I 100% know there are much better solutions out there that are probably much safer (especially with regards to animating cells on add/remove - that might not work while over-scrolling :/ ). This is also completely *not* physics based, its just faked via animation and a simple dampening function on drag. But, some people might be interested in the code and the approach I took regardless.

    submitted by /u/iain_1986
    [link] [comments]

    Kotlin typealias: use alternate names for class or functions types without introducing a new type

    Posted: 20 Jan 2020 05:24 PM PST

    Concat XML strings at build time

    Posted: 21 Jan 2020 02:02 AM PST

    Using Google drive api on F-Droid app?

    Posted: 20 Jan 2020 09:29 PM PST

    I have created an app "Notes Sync". The app is open source on github. Many users have asked me to make the app available on F-Droid. I looked through the rules and found that google services isn't supported on F-Droid apps. My app uses Google OAuth and Google Drive API for saving notes and images to the user's Google Drive storage. So is it okay to submit my app to F-Droid? If not is there an alternative to the Google Drive API?

    submitted by /u/Inf190
    [link] [comments]

    Room: "top star projection"

    Posted: 21 Jan 2020 04:38 AM PST

    Does anyone know what does the word "top" mean in context of "top star projection"?

    I've found it here: https://developer.android.com/jetpack/androidx/releases/room#compiler-options

    room.expandProjection: Configures Room to rewrite queries such that their top star projection is expanded to only contain the columns defined in the DAO method return type.

    More details about Expanding Projections are here: https://developer.android.com/jetpack/androidx/releases/room#2.2.0

    Expanding Projections: A new experimental compiler option room.expandProjection was added that causes Room to rewrite a query with a star projection to only contain the columns in the returning type POJO. For example, for a DAO method with @'Query("SELECT * FROM Song") that returns a POJO named SongIdAndTitle with only two fields. Then Room will rewrite the query to SELECT id, title FROM Song such that the minimum set of columns to satisfy the return type are fetched. This essentially eliminates the CURSOR_MISMATCH warning that is presented when the query returns extra columns that do not match any field in the returning POJO type.

    but they don't mention word "top". Do you have any clue?

    submitted by /u/wzieba
    [link] [comments]

    Android Single Source of Truth (SSOT) with MVVM and LiveData

    Posted: 21 Jan 2020 02:18 AM PST

    Suggestion to how to centralize the write and read on file System

    Posted: 21 Jan 2020 08:02 AM PST

    Hi :)

    In all my app activities I have to read and write in specific folders on fileSystem. To centralize the access I have created an helper class 'FileSystemHelper' with different static method like:

     saveInCache(Context context, String txt) readFromCache(Context context) .. 

    the problem is that I have to pass to all this methods the app context.

    I have thought to and the solution is to use a sort of a repository pattern, trasform this 'FileSystemHelper' class from static to a simple non-static class , add a costructor with a appContext parameter

    FileSystemHelper(appContext:Context) 

    and at this point create a singleton factory FileSystemManager that return new instance of FileSystemHelper class, maybe with lazy instantiation.

    What do you think about it ? In what way do you resolve this centralization problem in your android app ? :)

    submitted by /u/aeroxr1
    [link] [comments]

    Asking if this is possible to do with maps api?

    Posted: 21 Jan 2020 03:15 AM PST

    So i'm thinking of tracking all the device which are in a certain route that i set on maps.If i start navigation from A to B i want the data of all devices which are currently in it.So is these any api that i can feed the latlang of the devices and the route to check if they are in the route?

    submitted by /u/carnav098
    [link] [comments]

    Jetpack compose: "Ability to draw debug layout bounds" merged

    Posted: 21 Jan 2020 02:25 AM PST

    I occasionally lurk around the AOSP Compose repo and this merged commit caught my attention. It adds the ability to draw debug layout bounds for Jetpack Compose components.

    Why is this needed? Well, as of today, most of the tooling around view debugging works with Views, as in it works with android.view.View. Examples of such tools are Android Studio's Layout Inspector and "Show Layout Bounds" option in Developer Options. I'm sure there are many more.

    But Jetpack Compose components are not necessarily Views. I mean, it is entirely possible that your entire screen is one android.view.View. So this means Google needs to teach existing tooling to understand Composables, or provide new tooling that does this.

    This commit seems to be a first step towards providing such tooling. This seems to be implemented in the Compose runtime. This is probably good enough for Android Studio tools. I'm not entirely sure how it will work with Developer Options though. After all, Developer Options cannot be back-ported.

    If I had to guess, I'd say that probably you would need a specific version of Android to use Developer Options with Jetpack Compose. Or, who knows, maybe Google can backport such functionality by using internal API's that are out of bounds (no pun intended) for us.

    submitted by /u/curioustechizen
    [link] [comments]

    How do you Test your Android apps mostly Android bases classes like Activity,Fragment with Robolectric or Mockito/JUnit ?

    Posted: 20 Jan 2020 12:42 PM PST

    Any sources for working with Tensorflow Lite on Android Studio ?

    Posted: 20 Jan 2020 08:11 PM PST

    Hey guys a newbie here. Recently I've been engaged in a project where I have to classify Bluetooth Signals though a machine learning model. A SVM classifier to be exact. And the python model is done but I got no clue how to run the model in Android and I failed to find a goos resource. Any ideas or good sources ?

    submitted by /u/rush_95
    [link] [comments]

    Writing tests for Android

    Posted: 20 Jan 2020 04:37 PM PST

    Simple question. I have learnt writing tests for my apps at the school of 'learning by doing'. Which means I write them wrong many times. I am looking for a more formal lesson on writing tests in Android. Can someone point me to the right place, please?

    submitted by /u/prateeksaraswat
    [link] [comments]

    Dependency Injection in a library project

    Posted: 20 Jan 2020 11:40 PM PST

    I've been tasked with creating a few libraries that will be used by Android devs within the organisation. One of these libraries is a wrapper for AppAuth with some extra functionality.

    I wish to know how to have a DI framework within these libraries without the need for the consumer of the library to have that framework as well. For example with Koin, the app using the library would have to perform `startKoin { ... } ` in their Application class in order to use it. Would it be the same if I used Dagger? I'm not too experienced with developing libraries and I don't wish for the library setup to be too cumbersome for the user but at the same time I need DI within the libraries.

    submitted by /u/LoyTheKing
    [link] [comments]

    Videoview doubt

    Posted: 21 Jan 2020 03:17 AM PST

    I have two different videoview playing at the same time now. So right now i have two different media controller that pops up depending on which videoview is in focus. I want to combine these two video controllers into one so two separate controllers doesn't pop up.

    submitted by /u/Correct_Classroom
    [link] [comments]

    MVVM combined with AAC is not so simple. At least for me. Here's why.

    Posted: 20 Jan 2020 07:36 PM PST

    One day I decided to avoid using Activities. Being skeptical about it. I liked the idea of Single Activity apps. But, it is a pretty difficult path. I tried to follow the concepts of MVVM, but when I decided to use only fragments, I understood that it is not so simple. I think using Fragments requires you to write good code. One of the main reasons for this is that Fragments cannot be in a stopped state like Activities. It means, if you have some code in your Fragment like viewModel.fetchNews(), you should be careful because this code might be executed whenever Fragment lifecycle methods are called. For instance, let's suppose you go from NewsListFragment (where fetchNewsis called) to NewsDetailFragment. When you go back to NewsListFragment, by getting it from the backstack, its view is created again and possibly, it fetches news again, which is not so efficient. As far as I understand, it is ok and a developer should be able to handle that. But how should I solve it?

    My first attempt was to avoid having code like viewModel.fetchNews()on my View, which is my Fragment. I think View should not know about it. Yes, I understand that ViewModel somehow fetches news(for example, from repository), but I suppose View should not tell ViewModel what to do, it should only notify about user actions and observe changes. It is ViewModel's responsibility to decide what to do based on actions. So, the reason why I believed the previous argument is that it solved my problem. I put fetchNews method in the init stage of my ViewModel:

    val news = MutableLiveData<List<News>>() init { fetchNews() } private fun fetchNews() { viewModelScope.launch { val newsResponse: Result<List<News>> = withContext(Dispatchers.IO) { repo.fetchNews() } /// handling some errors here //// eventually news.value = newsResponse.data } } 

    By doing this, I achieved fetchNews to be called only once. When my fragment is taken from the backstack, ViewModel does not get initialized again. This means the fetchNews
    method is not called again. But with the help of Android Architecture Components, my Fragment anyways gets the list of news, because it has subscribed to LiveData changes. The win is that network request is not made again. It was a pretty good achievement for me, but it is not the end.

    I was keeping to learn about the MVVM architectural pattern. One day, I knew that it is not a good idea to have IO scope in ViewModel, which is the Presentation layer. I don't know a lot about Clean architecture, but it was strange for me to have IO scope in ViewModel, which should be light and not so big. So, I decided to move the opening IO scope in my repository, which always makes IO operations. Here is how my Repository changed:

    suspend fun fetchNews(): Result<List<News>> { try { val response = withContext(Dispatchers.IO) { api.fetchNews().await() } return Result.success(data = response) } catch (t: Throwable) { return Result.Error(error = t) } } 

    Almost nothing changed, only scope opening moved to the repository. Here is how my ViewModel changed:

    private fun fetchNews() { viewModelScope.launch { newsUILiveData.value = NewsUI.Loading(true) val response = repo.fetchNews(newsCount) newsUILiveData.value = NewsUI.Loading(false) when (response) { is Result.Success -> { newsUILiveData.value = NewsUI.NewsLoaded(response.data) } is Result.Error -> { newsUILiveData.value = NewsUI.Error(Constants.CHECK_NETWORK_CONNECTION) } } } } 

    It seemed pretty good code. Here, I avoided using postValuemethod of LiveData, which is a good result and more secure. But the first disadvantage is I cannot show localized error in my View. That's why I have a simple Constants file where I have CHECK_NETWORK_CONNECTION like "Something went wrong. Check your internet connection". The reason why I couldn't localize it is the principle which tells that ViewModel should not know anything about Android. It shouldn't know about Resources or Context. I agree with that and I just couldn't show localized error message under those conditions. It was sad, but I kept living with that code. But it is not the end.

    Once there appeared some task where I should do some modification over my news list. It doesn't matter which kind of modification, it can be a simple mapping operation or validation stuff. And this task has ruined my code. I didn't know how to handle it. Where to do this kind of modification? In Repository or ViewModel?

    Suppose, the answer is Repository. But Repository, in my case, returns Result(sealed class). For repository, it is pretty difficult to return real data, because there can be some network errors and catch block may be executed. And I think Repository should only provide some data from wherever. It shouldn't care about modification stuff, for instance, mapping News to NewsPresentation.

    Suppose, the answer is ViewModel. But modification operations could be a pretty difficult operation to be executed in the main thread. Of course, I can open the IO scope in my ViewModel, but it completely ruins the argument that ViewModel should not know anything about IO stuff. I might be wrong, but opening coroutine scopes has its costs, even if they are pretty light.

    So, at that point, I understood that I don't know anything about MVVM. I don't know anything about how to use AAC efficiently. I looked at the Sunflower code of Google that was made using best practices, but it didn't give me some hints. They store LiveData's inside repository and store LiveData's inside ViewModel. LiveData's are duplicated. It didn't answer my questions. Most samples do not show how to properly handle errors or do modifications over data when MVVM combined with AAC is used. Maybe, I should learn about Clean architecture.

    I understand that a developer should handle all that stuff and come up with some good solutions. Google provides us with amazing tools and it is your responsibility how to use it. But, I tried it and failed. If you have some ideas about the problems above, please share.

    submitted by /u/minimalist07
    [link] [comments]

    How do you start off with making an app?

    Posted: 21 Jan 2020 01:30 AM PST

    Hi! this is a dumb question but where do i start off with creating an app and publishing it in google play? i have an idea of making an uber-esque app but have no idea how to start. suggestions like what application to use for programming would be appreciated

    submitted by /u/LockLess123
    [link] [comments]

    Need advice on testing

    Posted: 20 Jan 2020 10:53 PM PST

    Hi, I have recently joined a new company and there are no unit tests written (I have never written UT). I am thinking about introducing unit tests into the app but right now am very confused about where to begin. I am not even sure if the architecture is testable or not. So I need a little advice about how to begin or even tell me if the architecture is testable or not. We follow the following architecture:

    ActivityA wants to fetch list of data from api, display it on the screeen. (No caching/persistance needed).

    ActivityA:

    btn.setOnClickListener { viewmodel.fetchLists() } 

    we use AAC viewmodel and liveData (repo is constructor injected using vmfactory)

    ViewModelA:

    fun fetchLists() { _listState.value = State.loading() repo.fetchLists(object: CallBack<Response> { override fun onSuccess(response:Response) { val list = response.list.filter { it.isActive } // some business logic _listState.value = State.success(list) } override fun onError(throwable: Throwable) { val error = Utils.handleException(throwable) // returns ENUM code which is handled in activity/fragment and proper message is displayed _listState.value = State.error(error) } }) } 

    Now, in repository, we make api call using volley.

    Repository:

    fun fetchLists(callback: CallBack<Response>) { // adds headers,params,body etc // gets callbacks inside onSuccess/onFailure) // passes the callback to the viewmodel } 

    Any leads on how to begin testing/ how to make this more testable would be greatly appreciated.

    submitted by /u/jeevan_o11
    [link] [comments]

    Capturing the internal audio stream of device

    Posted: 20 Jan 2020 06:50 PM PST

    Say if the phone is playing music from Google play (or youtube), does anyone know if there is a way to capture this audio data to stream to other devices?

    So far, I have found the following classes, but am unsure if these are the way to go. The first one looks more oriented towards microphone capture. The second one actually gives you data related to audio, but its main purpose is for visualization.

    https://developer.android.com/reference/android/media/MediaRecorder https://developer.android.com/reference/android/media/audiofx/Visualizer

    In addition, has anyone streamed audio data before? So far, I have created a test app using the Visualizer class in the second link and am using WebSockets to attempt to stream the data to other devices. I have a test python script that receives data, but it appears to receive much less data (<= 200 bytes) than what was sent (1024 bytes).

    Any help would be really appreciated, thanks!

    submitted by /u/sammyalhashemi
    [link] [comments]

    Is there anyone from Spain with experience in Virtual POS (card payments) integration? Advice needed.

    Posted: 20 Jan 2020 09:33 AM PST

    What I need is way to offer users a way to buy physical goods through the app. Stripe and alike can't be used, so the question is about integrating sort of Virtual POS with Redsys, BBVA, Sabadel etc. Have anyone successfully integrated something of that kind? Would be very grateful for the advice.

    submitted by /u/DaDavajte
    [link] [comments]

    Looking for partner(s) for Kotlin portfolio project

    Posted: 20 Jan 2020 11:47 AM PST

    I'm an aspiring android dev and I've made a couple apps in the SDK using java. In order to get hired I think that I'll need to also be able to write in Kotlin so I'd like to make make a medium-sized app for portfolio purposes. Would anyone else be interested in joining me on this? It's always better to have another person to bounce ideas off of. I'm flexible with the type of app that's made since its characteristics are inherently not the purpose of this activity anyways.

    Also, I intend to incorporate some libraries that I frequently see posted in job requirements i.e. Dagger2, RxJava/Kotlin

    submitted by /u/MaxwellMDG
    [link] [comments]

    What do you recommend for live cloud testing?

    Posted: 20 Jan 2020 01:34 PM PST

    I have a popular Android piano app, and sometimes have users with devices like a Galaxy Note 10 that have a issue with the audio latency or hiccups. Since some of those devices are around 800 EUR or so, I can't just buy them and test them, although I used to do that for older ones. So I am looking for a cloud solution where I can select a device, and upload my APK and just mess around a bit to see what is the issue, if any. Most important requirement is that it support audio some way. I found bitbar, which seems quite responsive, but no audio. And not much else.

    Are there people who use such services and can recommend me one? (Automated test services like Firebase/Google's cloud are not useful for these kind of issues)

    submitted by /u/johneffort
    [link] [comments]

    [Question] Over Modularization Examples and Benchmarks

    Posted: 20 Jan 2020 01:26 PM PST

    I have seen more and more people talking about Modularization: blog posts, talks and git projects heavily modularized from beginning!

    In addition to that, in previous jobs I saw projects that went from a monolith to dozens and sometimes, hundreds of modules, in few months due to "developers believing this would solve all their (build) problems" - these projects vary from 20KLOC to 1000KLOC.

    Ironically, after that, some projects kept having issues and to face it they... created a brand new modularization strategy... and when it didn't work properly, they blamed "Dagger" and any annotation processor without any benchmark that could really prove that...

    Note that "they" in this case, sometimes - but not always - includes "me".

    All of that said, my question is: am I exaggerating, did I have bad luck/experiences or over Modularization is a thing now? Do we have good content against modularization for everything? Like benchmarks and when it is not that good idea? Maybe some talks about that?

    Any information is welcome, thank you in advance!

    submitted by /u/marcellogalhardo
    [link] [comments]

    No comments:

    Post a Comment