aboutsummaryrefslogtreecommitdiff
path: root/lib/App/MediaControl/Model.rakumod
blob: 6d1c7716e9f147ce24837ca1f6d5c99a9742509f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use v6.d;
use App::MediaControl::FS;
use App::MediaControl::DB;
 
class App::MediaControl::Model {
    has App::MediaControl::FS $.fs is required;
    has App::MediaControl::DB $.db is required;
 
    method get-children-of($id{
        my @db-children = self.db.get-children-of($id);
        # [{id,path,name,is_dir,watched_time}] 
 
        my $path;
        if (@db-children{
            $path = @db-children[0]<path># they all have the same path 
        } elsif ($id.defined{
            my $entry = self.db.get-entry($id);
            $path = "{$entry<path>}{$entry<name>}";
        } else {
            $path = '/';
        }
 
        my @fs-children = self.fs.get-children-of($path);
        # [{name,is_dir}] 
 
        my ($db-idx, $fs-idx, $changed= 00False;
 
        sub add-to-db() {
            self.db.add-entry(
                :$path,
                name => @fs-children[$fs-idx]<name>,
                is-dir => @fs-children[$fs-idx].<is_dir>,
            );
            $changed=True;
            ++$fs-idx;
        }
        sub remove-from-db() {
            self.db.remove-entry(@db-children[$db-idx]<id>);
            $changed=True;
            ++$db-idx;
        }
        
        while ($db-idx < @db-children && $fs-idx < @fs-children{
            given @db-children[$db-idx]<name> leg @fs-children[$fs-idx]<name> {
                when Order::Same {
                    ++$db-idx++$fs-idx;
                }
 
                when Order::Less {
                    remove-from-db();
                }
 
                when Order::More {
                    add-to-db();
                }
            }
        }
 
        while ($db-idx < @db-children{
            remove-from-db();
        }
 
        while ($fs-idx < @fs-children{
            add-to-db();
        }
 
        if $changed {
            @db-children = self.db.get-children-of($id);
        }
 
        return @db-children;
    }
 
    method get-parents-of(Int:D() $id{
        return self.db.get-parents-of($id);
    }
 
    method get-entry(Int:D() $id{
        return self.db.get-entry($id);
    }
 
    method mark-entry-watched(Int:D() $id{
        return self.db.mark-entry-watched($id);
    }
 
    method get-recently-watched-folders(Int:D() $limit=20{
        my @db-folders = self.db.get-recently-watched-folders($limit);
        my $changed = False;
        for @db-folders -> $f {
            next if self.fs.exists("{$f<path>}{$f<name>}");
            self.db.remove-entry($f<id>);
            $changed = True;
        }
 
        if $changed {
            @db-folders = self.db.get-recently-watched-folders($limit);
        }
 
        return @db-folders;
    }
}