FreeRDP
Loading...
Searching...
No Matches
HomeViewModel.java
1/*
2 HomeViewModel for HomeActivity
3
4 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.app.Application;
14
15import androidx.annotation.NonNull;
16import androidx.lifecycle.AndroidViewModel;
17import androidx.lifecycle.LiveData;
18import androidx.lifecycle.MutableLiveData;
19
20import com.freerdp.freerdpcore.data.AppDatabase;
21import com.freerdp.freerdpcore.data.HistoryDatabase;
22import com.freerdp.freerdpcore.domain.BookmarkBase;
23import com.freerdp.freerdpcore.services.ManualBookmarkGateway;
24import com.freerdp.freerdpcore.services.QuickConnectHistoryGateway;
25
26import java.util.ArrayList;
27import java.util.Collections;
28import java.util.List;
29import java.util.concurrent.ExecutorService;
30import java.util.concurrent.Executors;
31
32public class HomeViewModel extends AndroidViewModel
33{
34 private final MutableLiveData<List<BookmarkBase>> bookmarks =
35 new MutableLiveData<>(Collections.emptyList());
36 private String currentQuery = "";
37 private final ExecutorService executor = Executors.newSingleThreadExecutor();
38
39 private final ManualBookmarkGateway manualBookmarkGateway;
40 private final QuickConnectHistoryGateway quickConnectHistoryGateway;
41
42 public HomeViewModel(@NonNull Application application)
43 {
44 super(application);
45 manualBookmarkGateway =
46 new ManualBookmarkGateway(AppDatabase.getInstance(application).bookmarkDao());
47 quickConnectHistoryGateway =
48 new QuickConnectHistoryGateway(HistoryDatabase.getInstance(application).historyDao());
49 }
50
51 public LiveData<List<BookmarkBase>> getBookmarks()
52 {
53 return bookmarks;
54 }
55
56 public String getCurrentQuery()
57 {
58 return currentQuery;
59 }
60
61 public void loadBookmarks(String query)
62 {
63 currentQuery = query != null ? query : "";
64 executor.execute(() -> {
65 List<BookmarkBase> result = new ArrayList<>();
66 if (!currentQuery.isEmpty())
67 {
68 BookmarkBase qcBm = new BookmarkBase();
69 qcBm.setType(BookmarkBase.TYPE_QUICKCONNECT);
70 qcBm.setLabel(currentQuery);
71 qcBm.setHostname(currentQuery);
72 qcBm.setDirectConnect(true);
73 result.add(qcBm);
74 result.addAll(quickConnectHistoryGateway.findHistory(currentQuery));
75 result.addAll(manualBookmarkGateway.findByLabelOrHostnameLike(currentQuery));
76 }
77 else
78 {
79 result.addAll(manualBookmarkGateway.findAll());
80 }
81 bookmarks.postValue(result);
82 });
83 }
84
85 public void deleteBookmark(long id)
86 {
87 executor.execute(() -> {
88 manualBookmarkGateway.delete(id);
89 loadBookmarks(currentQuery);
90 });
91 }
92
93 @Override protected void onCleared()
94 {
95 super.onCleared();
96 executor.shutdown();
97 }
98}